buffer_sync.c 13 KB

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