task_mmu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. #include <linux/mm.h>
  2. #include <linux/hugetlb.h>
  3. #include <linux/mount.h>
  4. #include <linux/seq_file.h>
  5. #include <linux/highmem.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/mempolicy.h>
  8. #include <asm/elf.h>
  9. #include <asm/uaccess.h>
  10. #include <asm/tlbflush.h>
  11. #include "internal.h"
  12. char *task_mem(struct mm_struct *mm, char *buffer)
  13. {
  14. unsigned long data, text, lib;
  15. unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
  16. /*
  17. * Note: to minimize their overhead, mm maintains hiwater_vm and
  18. * hiwater_rss only when about to *lower* total_vm or rss. Any
  19. * collector of these hiwater stats must therefore get total_vm
  20. * and rss too, which will usually be the higher. Barriers? not
  21. * worth the effort, such snapshots can always be inconsistent.
  22. */
  23. hiwater_vm = total_vm = mm->total_vm;
  24. if (hiwater_vm < mm->hiwater_vm)
  25. hiwater_vm = mm->hiwater_vm;
  26. hiwater_rss = total_rss = get_mm_rss(mm);
  27. if (hiwater_rss < mm->hiwater_rss)
  28. hiwater_rss = mm->hiwater_rss;
  29. data = mm->total_vm - mm->shared_vm - mm->stack_vm;
  30. text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
  31. lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
  32. buffer += sprintf(buffer,
  33. "VmPeak:\t%8lu kB\n"
  34. "VmSize:\t%8lu kB\n"
  35. "VmLck:\t%8lu kB\n"
  36. "VmHWM:\t%8lu kB\n"
  37. "VmRSS:\t%8lu kB\n"
  38. "VmData:\t%8lu kB\n"
  39. "VmStk:\t%8lu kB\n"
  40. "VmExe:\t%8lu kB\n"
  41. "VmLib:\t%8lu kB\n"
  42. "VmPTE:\t%8lu kB\n",
  43. hiwater_vm << (PAGE_SHIFT-10),
  44. (total_vm - mm->reserved_vm) << (PAGE_SHIFT-10),
  45. mm->locked_vm << (PAGE_SHIFT-10),
  46. hiwater_rss << (PAGE_SHIFT-10),
  47. total_rss << (PAGE_SHIFT-10),
  48. data << (PAGE_SHIFT-10),
  49. mm->stack_vm << (PAGE_SHIFT-10), text, lib,
  50. (PTRS_PER_PTE*sizeof(pte_t)*mm->nr_ptes) >> 10);
  51. return buffer;
  52. }
  53. unsigned long task_vsize(struct mm_struct *mm)
  54. {
  55. return PAGE_SIZE * mm->total_vm;
  56. }
  57. int task_statm(struct mm_struct *mm, int *shared, int *text,
  58. int *data, int *resident)
  59. {
  60. *shared = get_mm_counter(mm, file_rss);
  61. *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
  62. >> PAGE_SHIFT;
  63. *data = mm->total_vm - mm->shared_vm;
  64. *resident = *shared + get_mm_counter(mm, anon_rss);
  65. return mm->total_vm;
  66. }
  67. int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
  68. {
  69. struct vm_area_struct * vma;
  70. int result = -ENOENT;
  71. struct task_struct *task = get_proc_task(inode);
  72. struct mm_struct * mm = NULL;
  73. if (task) {
  74. mm = get_task_mm(task);
  75. put_task_struct(task);
  76. }
  77. if (!mm)
  78. goto out;
  79. down_read(&mm->mmap_sem);
  80. vma = mm->mmap;
  81. while (vma) {
  82. if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file)
  83. break;
  84. vma = vma->vm_next;
  85. }
  86. if (vma) {
  87. *mnt = mntget(vma->vm_file->f_vfsmnt);
  88. *dentry = dget(vma->vm_file->f_dentry);
  89. result = 0;
  90. }
  91. up_read(&mm->mmap_sem);
  92. mmput(mm);
  93. out:
  94. return result;
  95. }
  96. static void pad_len_spaces(struct seq_file *m, int len)
  97. {
  98. len = 25 + sizeof(void*) * 6 - len;
  99. if (len < 1)
  100. len = 1;
  101. seq_printf(m, "%*c", len, ' ');
  102. }
  103. struct mem_size_stats
  104. {
  105. unsigned long resident;
  106. unsigned long shared_clean;
  107. unsigned long shared_dirty;
  108. unsigned long private_clean;
  109. unsigned long private_dirty;
  110. };
  111. __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
  112. {
  113. return NULL;
  114. }
  115. static int show_map_internal(struct seq_file *m, void *v, struct mem_size_stats *mss)
  116. {
  117. struct proc_maps_private *priv = m->private;
  118. struct task_struct *task = priv->task;
  119. struct vm_area_struct *vma = v;
  120. struct mm_struct *mm = vma->vm_mm;
  121. struct file *file = vma->vm_file;
  122. int flags = vma->vm_flags;
  123. unsigned long ino = 0;
  124. dev_t dev = 0;
  125. int len;
  126. if (file) {
  127. struct inode *inode = vma->vm_file->f_dentry->d_inode;
  128. dev = inode->i_sb->s_dev;
  129. ino = inode->i_ino;
  130. }
  131. seq_printf(m, "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n",
  132. vma->vm_start,
  133. vma->vm_end,
  134. flags & VM_READ ? 'r' : '-',
  135. flags & VM_WRITE ? 'w' : '-',
  136. flags & VM_EXEC ? 'x' : '-',
  137. flags & VM_MAYSHARE ? 's' : 'p',
  138. vma->vm_pgoff << PAGE_SHIFT,
  139. MAJOR(dev), MINOR(dev), ino, &len);
  140. /*
  141. * Print the dentry name for named mappings, and a
  142. * special [heap] marker for the heap:
  143. */
  144. if (file) {
  145. pad_len_spaces(m, len);
  146. seq_path(m, file->f_vfsmnt, file->f_dentry, "\n");
  147. } else {
  148. const char *name = arch_vma_name(vma);
  149. if (!name) {
  150. if (mm) {
  151. if (vma->vm_start <= mm->start_brk &&
  152. vma->vm_end >= mm->brk) {
  153. name = "[heap]";
  154. } else if (vma->vm_start <= mm->start_stack &&
  155. vma->vm_end >= mm->start_stack) {
  156. name = "[stack]";
  157. }
  158. } else {
  159. name = "[vdso]";
  160. }
  161. }
  162. if (name) {
  163. pad_len_spaces(m, len);
  164. seq_puts(m, name);
  165. }
  166. }
  167. seq_putc(m, '\n');
  168. if (mss)
  169. seq_printf(m,
  170. "Size: %8lu kB\n"
  171. "Rss: %8lu kB\n"
  172. "Shared_Clean: %8lu kB\n"
  173. "Shared_Dirty: %8lu kB\n"
  174. "Private_Clean: %8lu kB\n"
  175. "Private_Dirty: %8lu kB\n",
  176. (vma->vm_end - vma->vm_start) >> 10,
  177. mss->resident >> 10,
  178. mss->shared_clean >> 10,
  179. mss->shared_dirty >> 10,
  180. mss->private_clean >> 10,
  181. mss->private_dirty >> 10);
  182. if (m->count < m->size) /* vma is copied successfully */
  183. m->version = (vma != get_gate_vma(task))? vma->vm_start: 0;
  184. return 0;
  185. }
  186. static int show_map(struct seq_file *m, void *v)
  187. {
  188. return show_map_internal(m, v, NULL);
  189. }
  190. static void smaps_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  191. unsigned long addr, unsigned long end,
  192. struct mem_size_stats *mss)
  193. {
  194. pte_t *pte, ptent;
  195. spinlock_t *ptl;
  196. struct page *page;
  197. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  198. do {
  199. ptent = *pte;
  200. if (!pte_present(ptent))
  201. continue;
  202. mss->resident += PAGE_SIZE;
  203. page = vm_normal_page(vma, addr, ptent);
  204. if (!page)
  205. continue;
  206. if (page_mapcount(page) >= 2) {
  207. if (pte_dirty(ptent))
  208. mss->shared_dirty += PAGE_SIZE;
  209. else
  210. mss->shared_clean += PAGE_SIZE;
  211. } else {
  212. if (pte_dirty(ptent))
  213. mss->private_dirty += PAGE_SIZE;
  214. else
  215. mss->private_clean += PAGE_SIZE;
  216. }
  217. } while (pte++, addr += PAGE_SIZE, addr != end);
  218. pte_unmap_unlock(pte - 1, ptl);
  219. cond_resched();
  220. }
  221. static inline void smaps_pmd_range(struct vm_area_struct *vma, pud_t *pud,
  222. unsigned long addr, unsigned long end,
  223. struct mem_size_stats *mss)
  224. {
  225. pmd_t *pmd;
  226. unsigned long next;
  227. pmd = pmd_offset(pud, addr);
  228. do {
  229. next = pmd_addr_end(addr, end);
  230. if (pmd_none_or_clear_bad(pmd))
  231. continue;
  232. smaps_pte_range(vma, pmd, addr, next, mss);
  233. } while (pmd++, addr = next, addr != end);
  234. }
  235. static inline void smaps_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
  236. unsigned long addr, unsigned long end,
  237. struct mem_size_stats *mss)
  238. {
  239. pud_t *pud;
  240. unsigned long next;
  241. pud = pud_offset(pgd, addr);
  242. do {
  243. next = pud_addr_end(addr, end);
  244. if (pud_none_or_clear_bad(pud))
  245. continue;
  246. smaps_pmd_range(vma, pud, addr, next, mss);
  247. } while (pud++, addr = next, addr != end);
  248. }
  249. static inline void smaps_pgd_range(struct vm_area_struct *vma,
  250. unsigned long addr, unsigned long end,
  251. struct mem_size_stats *mss)
  252. {
  253. pgd_t *pgd;
  254. unsigned long next;
  255. pgd = pgd_offset(vma->vm_mm, addr);
  256. do {
  257. next = pgd_addr_end(addr, end);
  258. if (pgd_none_or_clear_bad(pgd))
  259. continue;
  260. smaps_pud_range(vma, pgd, addr, next, mss);
  261. } while (pgd++, addr = next, addr != end);
  262. }
  263. static int show_smap(struct seq_file *m, void *v)
  264. {
  265. struct vm_area_struct *vma = v;
  266. struct mem_size_stats mss;
  267. memset(&mss, 0, sizeof mss);
  268. if (vma->vm_mm && !is_vm_hugetlb_page(vma))
  269. smaps_pgd_range(vma, vma->vm_start, vma->vm_end, &mss);
  270. return show_map_internal(m, v, &mss);
  271. }
  272. static void *m_start(struct seq_file *m, loff_t *pos)
  273. {
  274. struct proc_maps_private *priv = m->private;
  275. unsigned long last_addr = m->version;
  276. struct mm_struct *mm;
  277. struct vm_area_struct *vma, *tail_vma = NULL;
  278. loff_t l = *pos;
  279. /* Clear the per syscall fields in priv */
  280. priv->task = NULL;
  281. priv->tail_vma = NULL;
  282. /*
  283. * We remember last_addr rather than next_addr to hit with
  284. * mmap_cache most of the time. We have zero last_addr at
  285. * the beginning and also after lseek. We will have -1 last_addr
  286. * after the end of the vmas.
  287. */
  288. if (last_addr == -1UL)
  289. return NULL;
  290. priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
  291. if (!priv->task)
  292. return NULL;
  293. mm = get_task_mm(priv->task);
  294. if (!mm)
  295. return NULL;
  296. priv->tail_vma = tail_vma = get_gate_vma(priv->task);
  297. down_read(&mm->mmap_sem);
  298. /* Start with last addr hint */
  299. if (last_addr && (vma = find_vma(mm, last_addr))) {
  300. vma = vma->vm_next;
  301. goto out;
  302. }
  303. /*
  304. * Check the vma index is within the range and do
  305. * sequential scan until m_index.
  306. */
  307. vma = NULL;
  308. if ((unsigned long)l < mm->map_count) {
  309. vma = mm->mmap;
  310. while (l-- && vma)
  311. vma = vma->vm_next;
  312. goto out;
  313. }
  314. if (l != mm->map_count)
  315. tail_vma = NULL; /* After gate vma */
  316. out:
  317. if (vma)
  318. return vma;
  319. /* End of vmas has been reached */
  320. m->version = (tail_vma != NULL)? 0: -1UL;
  321. up_read(&mm->mmap_sem);
  322. mmput(mm);
  323. return tail_vma;
  324. }
  325. static void vma_stop(struct proc_maps_private *priv, struct vm_area_struct *vma)
  326. {
  327. if (vma && vma != priv->tail_vma) {
  328. struct mm_struct *mm = vma->vm_mm;
  329. up_read(&mm->mmap_sem);
  330. mmput(mm);
  331. }
  332. }
  333. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  334. {
  335. struct proc_maps_private *priv = m->private;
  336. struct vm_area_struct *vma = v;
  337. struct vm_area_struct *tail_vma = priv->tail_vma;
  338. (*pos)++;
  339. if (vma && (vma != tail_vma) && vma->vm_next)
  340. return vma->vm_next;
  341. vma_stop(priv, vma);
  342. return (vma != tail_vma)? tail_vma: NULL;
  343. }
  344. static void m_stop(struct seq_file *m, void *v)
  345. {
  346. struct proc_maps_private *priv = m->private;
  347. struct vm_area_struct *vma = v;
  348. vma_stop(priv, vma);
  349. if (priv->task)
  350. put_task_struct(priv->task);
  351. }
  352. static struct seq_operations proc_pid_maps_op = {
  353. .start = m_start,
  354. .next = m_next,
  355. .stop = m_stop,
  356. .show = show_map
  357. };
  358. static struct seq_operations proc_pid_smaps_op = {
  359. .start = m_start,
  360. .next = m_next,
  361. .stop = m_stop,
  362. .show = show_smap
  363. };
  364. static int do_maps_open(struct inode *inode, struct file *file,
  365. struct seq_operations *ops)
  366. {
  367. struct proc_maps_private *priv;
  368. int ret = -ENOMEM;
  369. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  370. if (priv) {
  371. priv->pid = proc_pid(inode);
  372. ret = seq_open(file, ops);
  373. if (!ret) {
  374. struct seq_file *m = file->private_data;
  375. m->private = priv;
  376. } else {
  377. kfree(priv);
  378. }
  379. }
  380. return ret;
  381. }
  382. static int maps_open(struct inode *inode, struct file *file)
  383. {
  384. return do_maps_open(inode, file, &proc_pid_maps_op);
  385. }
  386. struct file_operations proc_maps_operations = {
  387. .open = maps_open,
  388. .read = seq_read,
  389. .llseek = seq_lseek,
  390. .release = seq_release_private,
  391. };
  392. #ifdef CONFIG_NUMA
  393. extern int show_numa_map(struct seq_file *m, void *v);
  394. static struct seq_operations proc_pid_numa_maps_op = {
  395. .start = m_start,
  396. .next = m_next,
  397. .stop = m_stop,
  398. .show = show_numa_map
  399. };
  400. static int numa_maps_open(struct inode *inode, struct file *file)
  401. {
  402. return do_maps_open(inode, file, &proc_pid_numa_maps_op);
  403. }
  404. struct file_operations proc_numa_maps_operations = {
  405. .open = numa_maps_open,
  406. .read = seq_read,
  407. .llseek = seq_lseek,
  408. .release = seq_release_private,
  409. };
  410. #endif
  411. static int smaps_open(struct inode *inode, struct file *file)
  412. {
  413. return do_maps_open(inode, file, &proc_pid_smaps_op);
  414. }
  415. struct file_operations proc_smaps_operations = {
  416. .open = smaps_open,
  417. .read = seq_read,
  418. .llseek = seq_lseek,
  419. .release = seq_release_private,
  420. };