buffer_sync.c 14 KB

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