buffer_sync.c 13 KB

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