buffer_sync.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /**
  2. * @file buffer_sync.c
  3. *
  4. * @remark Copyright 2002-2009 OProfile authors
  5. * @remark Read the file COPYING
  6. *
  7. * @author John Levon <levon@movementarian.org>
  8. * @author Barry Kasindorf
  9. * @author Robert Richter <robert.richter@amd.com>
  10. *
  11. * This is the core of the buffer management. Each
  12. * CPU buffer is processed and entered into the
  13. * global event buffer. Such processing is necessary
  14. * in several circumstances, mentioned below.
  15. *
  16. * The processing does the job of converting the
  17. * transitory EIP value into a persistent dentry/offset
  18. * value that the profiler can record at its leisure.
  19. *
  20. * See fs/dcookies.c for a description of the dentry/offset
  21. * objects.
  22. */
  23. #include <linux/file.h>
  24. #include <linux/mm.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/notifier.h>
  27. #include <linux/dcookies.h>
  28. #include <linux/profile.h>
  29. #include <linux/module.h>
  30. #include <linux/fs.h>
  31. #include <linux/oprofile.h>
  32. #include <linux/sched.h>
  33. #include <linux/gfp.h>
  34. #include "oprofile_stats.h"
  35. #include "event_buffer.h"
  36. #include "cpu_buffer.h"
  37. #include "buffer_sync.h"
  38. static LIST_HEAD(dying_tasks);
  39. static LIST_HEAD(dead_tasks);
  40. static cpumask_var_t marked_cpus;
  41. static DEFINE_SPINLOCK(task_mortuary);
  42. static void process_task_mortuary(void);
  43. /* Take ownership of the task struct and place it on the
  44. * list for processing. Only after two full buffer syncs
  45. * does the task eventually get freed, because by then
  46. * we are sure we will not reference it again.
  47. * Can be invoked from softirq via RCU callback due to
  48. * call_rcu() of the task struct, hence the _irqsave.
  49. */
  50. static int
  51. task_free_notify(struct notifier_block *self, unsigned long val, void *data)
  52. {
  53. unsigned long flags;
  54. struct task_struct *task = data;
  55. spin_lock_irqsave(&task_mortuary, flags);
  56. list_add(&task->tasks, &dying_tasks);
  57. spin_unlock_irqrestore(&task_mortuary, flags);
  58. return NOTIFY_OK;
  59. }
  60. /* The task is on its way out. A sync of the buffer means we can catch
  61. * any remaining samples for this task.
  62. */
  63. static int
  64. task_exit_notify(struct notifier_block *self, unsigned long val, void *data)
  65. {
  66. /* To avoid latency problems, we only process the current CPU,
  67. * hoping that most samples for the task are on this CPU
  68. */
  69. sync_buffer(raw_smp_processor_id());
  70. return 0;
  71. }
  72. /* The task is about to try a do_munmap(). We peek at what it's going to
  73. * do, and if it's an executable region, process the samples first, so
  74. * we don't lose any. This does not have to be exact, it's a QoI issue
  75. * only.
  76. */
  77. static int
  78. munmap_notify(struct notifier_block *self, unsigned long val, void *data)
  79. {
  80. unsigned long addr = (unsigned long)data;
  81. struct mm_struct *mm = current->mm;
  82. struct vm_area_struct *mpnt;
  83. down_read(&mm->mmap_sem);
  84. mpnt = find_vma(mm, addr);
  85. if (mpnt && mpnt->vm_file && (mpnt->vm_flags & VM_EXEC)) {
  86. up_read(&mm->mmap_sem);
  87. /* To avoid latency problems, we only process the current CPU,
  88. * hoping that most samples for the task are on this CPU
  89. */
  90. sync_buffer(raw_smp_processor_id());
  91. return 0;
  92. }
  93. up_read(&mm->mmap_sem);
  94. return 0;
  95. }
  96. /* We need to be told about new modules so we don't attribute to a previously
  97. * loaded module, or drop the samples on the floor.
  98. */
  99. static int
  100. module_load_notify(struct notifier_block *self, unsigned long val, void *data)
  101. {
  102. #ifdef CONFIG_MODULES
  103. if (val != MODULE_STATE_COMING)
  104. return 0;
  105. /* FIXME: should we process all CPU buffers ? */
  106. mutex_lock(&buffer_mutex);
  107. add_event_entry(ESCAPE_CODE);
  108. add_event_entry(MODULE_LOADED_CODE);
  109. mutex_unlock(&buffer_mutex);
  110. #endif
  111. return 0;
  112. }
  113. static struct notifier_block task_free_nb = {
  114. .notifier_call = task_free_notify,
  115. };
  116. static struct notifier_block task_exit_nb = {
  117. .notifier_call = task_exit_notify,
  118. };
  119. static struct notifier_block munmap_nb = {
  120. .notifier_call = munmap_notify,
  121. };
  122. static struct notifier_block module_load_nb = {
  123. .notifier_call = module_load_notify,
  124. };
  125. static void free_all_tasks(void)
  126. {
  127. /* make sure we don't leak task structs */
  128. process_task_mortuary();
  129. process_task_mortuary();
  130. }
  131. int sync_start(void)
  132. {
  133. int err;
  134. if (!zalloc_cpumask_var(&marked_cpus, GFP_KERNEL))
  135. return -ENOMEM;
  136. err = task_handoff_register(&task_free_nb);
  137. if (err)
  138. goto out1;
  139. err = profile_event_register(PROFILE_TASK_EXIT, &task_exit_nb);
  140. if (err)
  141. goto out2;
  142. err = profile_event_register(PROFILE_MUNMAP, &munmap_nb);
  143. if (err)
  144. goto out3;
  145. err = register_module_notifier(&module_load_nb);
  146. if (err)
  147. goto out4;
  148. start_cpu_work();
  149. out:
  150. return err;
  151. out4:
  152. profile_event_unregister(PROFILE_MUNMAP, &munmap_nb);
  153. out3:
  154. profile_event_unregister(PROFILE_TASK_EXIT, &task_exit_nb);
  155. out2:
  156. task_handoff_unregister(&task_free_nb);
  157. free_all_tasks();
  158. out1:
  159. free_cpumask_var(marked_cpus);
  160. goto out;
  161. }
  162. void sync_stop(void)
  163. {
  164. end_cpu_work();
  165. unregister_module_notifier(&module_load_nb);
  166. profile_event_unregister(PROFILE_MUNMAP, &munmap_nb);
  167. profile_event_unregister(PROFILE_TASK_EXIT, &task_exit_nb);
  168. task_handoff_unregister(&task_free_nb);
  169. barrier(); /* do all of the above first */
  170. flush_cpu_work();
  171. free_all_tasks();
  172. free_cpumask_var(marked_cpus);
  173. }
  174. /* Optimisation. We can manage without taking the dcookie sem
  175. * because we cannot reach this code without at least one
  176. * dcookie user still being registered (namely, the reader
  177. * of the event buffer). */
  178. static inline unsigned long fast_get_dcookie(struct path *path)
  179. {
  180. unsigned long cookie;
  181. if (path->dentry->d_flags & DCACHE_COOKIE)
  182. return (unsigned long)path->dentry;
  183. get_dcookie(path, &cookie);
  184. return cookie;
  185. }
  186. /* Look up the dcookie for the task's mm->exe_file,
  187. * which corresponds loosely to "application name". This is
  188. * not strictly necessary but allows oprofile to associate
  189. * shared-library samples with particular applications
  190. */
  191. static unsigned long get_exec_dcookie(struct mm_struct *mm)
  192. {
  193. unsigned long cookie = NO_COOKIE;
  194. struct file *exe_file;
  195. if (!mm)
  196. goto done;
  197. exe_file = get_mm_exe_file(mm);
  198. if (!exe_file)
  199. goto done;
  200. cookie = fast_get_dcookie(&exe_file->f_path);
  201. fput(exe_file);
  202. done:
  203. return cookie;
  204. }
  205. /* Convert the EIP value of a sample into a persistent dentry/offset
  206. * pair that can then be added to the global event buffer. We make
  207. * sure to do this lookup before a mm->mmap modification happens so
  208. * we don't lose track.
  209. *
  210. * The caller must ensure the mm is not nil (ie: not a kernel thread).
  211. */
  212. static unsigned long
  213. lookup_dcookie(struct mm_struct *mm, unsigned long addr, off_t *offset)
  214. {
  215. unsigned long cookie = NO_COOKIE;
  216. struct vm_area_struct *vma;
  217. down_read(&mm->mmap_sem);
  218. for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
  219. if (addr < vma->vm_start || addr >= vma->vm_end)
  220. continue;
  221. if (vma->vm_file) {
  222. cookie = fast_get_dcookie(&vma->vm_file->f_path);
  223. *offset = (vma->vm_pgoff << PAGE_SHIFT) + addr -
  224. vma->vm_start;
  225. } else {
  226. /* must be an anonymous map */
  227. *offset = addr;
  228. }
  229. break;
  230. }
  231. if (!vma)
  232. cookie = INVALID_COOKIE;
  233. up_read(&mm->mmap_sem);
  234. return cookie;
  235. }
  236. static unsigned long last_cookie = INVALID_COOKIE;
  237. static void add_cpu_switch(int i)
  238. {
  239. add_event_entry(ESCAPE_CODE);
  240. add_event_entry(CPU_SWITCH_CODE);
  241. add_event_entry(i);
  242. last_cookie = INVALID_COOKIE;
  243. }
  244. static void add_kernel_ctx_switch(unsigned int in_kernel)
  245. {
  246. add_event_entry(ESCAPE_CODE);
  247. if (in_kernel)
  248. add_event_entry(KERNEL_ENTER_SWITCH_CODE);
  249. else
  250. add_event_entry(KERNEL_EXIT_SWITCH_CODE);
  251. }
  252. static void
  253. add_user_ctx_switch(struct task_struct const *task, unsigned long cookie)
  254. {
  255. add_event_entry(ESCAPE_CODE);
  256. add_event_entry(CTX_SWITCH_CODE);
  257. add_event_entry(task->pid);
  258. add_event_entry(cookie);
  259. /* Another code for daemon back-compat */
  260. add_event_entry(ESCAPE_CODE);
  261. add_event_entry(CTX_TGID_CODE);
  262. add_event_entry(task->tgid);
  263. }
  264. static void add_cookie_switch(unsigned long cookie)
  265. {
  266. add_event_entry(ESCAPE_CODE);
  267. add_event_entry(COOKIE_SWITCH_CODE);
  268. add_event_entry(cookie);
  269. }
  270. static void add_trace_begin(void)
  271. {
  272. add_event_entry(ESCAPE_CODE);
  273. add_event_entry(TRACE_BEGIN_CODE);
  274. }
  275. static void add_data(struct op_entry *entry, struct mm_struct *mm)
  276. {
  277. unsigned long code, pc, val;
  278. unsigned long cookie;
  279. off_t offset;
  280. if (!op_cpu_buffer_get_data(entry, &code))
  281. return;
  282. if (!op_cpu_buffer_get_data(entry, &pc))
  283. return;
  284. if (!op_cpu_buffer_get_size(entry))
  285. return;
  286. if (mm) {
  287. cookie = lookup_dcookie(mm, pc, &offset);
  288. if (cookie == NO_COOKIE)
  289. offset = pc;
  290. if (cookie == INVALID_COOKIE) {
  291. atomic_inc(&oprofile_stats.sample_lost_no_mapping);
  292. offset = pc;
  293. }
  294. if (cookie != last_cookie) {
  295. add_cookie_switch(cookie);
  296. last_cookie = cookie;
  297. }
  298. } else
  299. offset = pc;
  300. add_event_entry(ESCAPE_CODE);
  301. add_event_entry(code);
  302. add_event_entry(offset); /* Offset from Dcookie */
  303. while (op_cpu_buffer_get_data(entry, &val))
  304. add_event_entry(val);
  305. }
  306. static inline void add_sample_entry(unsigned long offset, unsigned long event)
  307. {
  308. add_event_entry(offset);
  309. add_event_entry(event);
  310. }
  311. /*
  312. * Add a sample to the global event buffer. If possible the
  313. * sample is converted into a persistent dentry/offset pair
  314. * for later lookup from userspace. Return 0 on failure.
  315. */
  316. static int
  317. add_sample(struct mm_struct *mm, struct op_sample *s, int in_kernel)
  318. {
  319. unsigned long cookie;
  320. off_t offset;
  321. if (in_kernel) {
  322. add_sample_entry(s->eip, s->event);
  323. return 1;
  324. }
  325. /* add userspace sample */
  326. if (!mm) {
  327. atomic_inc(&oprofile_stats.sample_lost_no_mm);
  328. return 0;
  329. }
  330. cookie = lookup_dcookie(mm, s->eip, &offset);
  331. if (cookie == INVALID_COOKIE) {
  332. atomic_inc(&oprofile_stats.sample_lost_no_mapping);
  333. return 0;
  334. }
  335. if (cookie != last_cookie) {
  336. add_cookie_switch(cookie);
  337. last_cookie = cookie;
  338. }
  339. add_sample_entry(offset, s->event);
  340. return 1;
  341. }
  342. static void release_mm(struct mm_struct *mm)
  343. {
  344. if (!mm)
  345. return;
  346. mmput(mm);
  347. }
  348. static inline int is_code(unsigned long val)
  349. {
  350. return val == ESCAPE_CODE;
  351. }
  352. /* Move tasks along towards death. Any tasks on dead_tasks
  353. * will definitely have no remaining references in any
  354. * CPU buffers at this point, because we use two lists,
  355. * and to have reached the list, it must have gone through
  356. * one full sync already.
  357. */
  358. static void process_task_mortuary(void)
  359. {
  360. unsigned long flags;
  361. LIST_HEAD(local_dead_tasks);
  362. struct task_struct *task;
  363. struct task_struct *ttask;
  364. spin_lock_irqsave(&task_mortuary, flags);
  365. list_splice_init(&dead_tasks, &local_dead_tasks);
  366. list_splice_init(&dying_tasks, &dead_tasks);
  367. spin_unlock_irqrestore(&task_mortuary, flags);
  368. list_for_each_entry_safe(task, ttask, &local_dead_tasks, tasks) {
  369. list_del(&task->tasks);
  370. free_task(task);
  371. }
  372. }
  373. static void mark_done(int cpu)
  374. {
  375. int i;
  376. cpumask_set_cpu(cpu, marked_cpus);
  377. for_each_online_cpu(i) {
  378. if (!cpumask_test_cpu(i, marked_cpus))
  379. return;
  380. }
  381. /* All CPUs have been processed at least once,
  382. * we can process the mortuary once
  383. */
  384. process_task_mortuary();
  385. cpumask_clear(marked_cpus);
  386. }
  387. /* FIXME: this is not sufficient if we implement syscall barrier backtrace
  388. * traversal, the code switch to sb_sample_start at first kernel enter/exit
  389. * switch so we need a fifth state and some special handling in sync_buffer()
  390. */
  391. typedef enum {
  392. sb_bt_ignore = -2,
  393. sb_buffer_start,
  394. sb_bt_start,
  395. sb_sample_start,
  396. } sync_buffer_state;
  397. /* Sync one of the CPU's buffers into the global event buffer.
  398. * Here we need to go through each batch of samples punctuated
  399. * by context switch notes, taking the task's mmap_sem and doing
  400. * lookup in task->mm->mmap to convert EIP into dcookie/offset
  401. * value.
  402. */
  403. void sync_buffer(int cpu)
  404. {
  405. struct mm_struct *mm = NULL;
  406. struct mm_struct *oldmm;
  407. unsigned long val;
  408. struct task_struct *new;
  409. unsigned long cookie = 0;
  410. int in_kernel = 1;
  411. sync_buffer_state state = sb_buffer_start;
  412. unsigned int i;
  413. unsigned long available;
  414. unsigned long flags;
  415. struct op_entry entry;
  416. struct op_sample *sample;
  417. mutex_lock(&buffer_mutex);
  418. add_cpu_switch(cpu);
  419. op_cpu_buffer_reset(cpu);
  420. available = op_cpu_buffer_entries(cpu);
  421. for (i = 0; i < available; ++i) {
  422. sample = op_cpu_buffer_read_entry(&entry, cpu);
  423. if (!sample)
  424. break;
  425. if (is_code(sample->eip)) {
  426. flags = sample->event;
  427. if (flags & TRACE_BEGIN) {
  428. state = sb_bt_start;
  429. add_trace_begin();
  430. }
  431. if (flags & KERNEL_CTX_SWITCH) {
  432. /* kernel/userspace switch */
  433. in_kernel = flags & IS_KERNEL;
  434. if (state == sb_buffer_start)
  435. state = sb_sample_start;
  436. add_kernel_ctx_switch(flags & IS_KERNEL);
  437. }
  438. if (flags & USER_CTX_SWITCH
  439. && op_cpu_buffer_get_data(&entry, &val)) {
  440. /* userspace context switch */
  441. new = (struct task_struct *)val;
  442. oldmm = mm;
  443. release_mm(oldmm);
  444. mm = get_task_mm(new);
  445. if (mm != oldmm)
  446. cookie = get_exec_dcookie(mm);
  447. add_user_ctx_switch(new, cookie);
  448. }
  449. if (op_cpu_buffer_get_size(&entry))
  450. add_data(&entry, mm);
  451. continue;
  452. }
  453. if (state < sb_bt_start)
  454. /* ignore sample */
  455. continue;
  456. if (add_sample(mm, sample, in_kernel))
  457. continue;
  458. /* ignore backtraces if failed to add a sample */
  459. if (state == sb_bt_start) {
  460. state = sb_bt_ignore;
  461. atomic_inc(&oprofile_stats.bt_lost_no_mapping);
  462. }
  463. }
  464. release_mm(mm);
  465. mark_done(cpu);
  466. mutex_unlock(&buffer_mutex);
  467. }
  468. /* The function can be used to add a buffer worth of data directly to
  469. * the kernel buffer. The buffer is assumed to be a circular buffer.
  470. * Take the entries from index start and end at index end, wrapping
  471. * at max_entries.
  472. */
  473. void oprofile_put_buff(unsigned long *buf, unsigned int start,
  474. unsigned int stop, unsigned int max)
  475. {
  476. int i;
  477. i = start;
  478. mutex_lock(&buffer_mutex);
  479. while (i != stop) {
  480. add_event_entry(buf[i++]);
  481. if (i >= max)
  482. i = 0;
  483. }
  484. mutex_unlock(&buffer_mutex);
  485. }