buffer_sync.c 13 KB

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