task_mmu.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 = proc_task(inode);
  72. struct mm_struct * mm = get_task_mm(task);
  73. if (!mm)
  74. goto out;
  75. down_read(&mm->mmap_sem);
  76. vma = mm->mmap;
  77. while (vma) {
  78. if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file)
  79. break;
  80. vma = vma->vm_next;
  81. }
  82. if (vma) {
  83. *mnt = mntget(vma->vm_file->f_vfsmnt);
  84. *dentry = dget(vma->vm_file->f_dentry);
  85. result = 0;
  86. }
  87. up_read(&mm->mmap_sem);
  88. mmput(mm);
  89. out:
  90. return result;
  91. }
  92. static void pad_len_spaces(struct seq_file *m, int len)
  93. {
  94. len = 25 + sizeof(void*) * 6 - len;
  95. if (len < 1)
  96. len = 1;
  97. seq_printf(m, "%*c", len, ' ');
  98. }
  99. struct mem_size_stats
  100. {
  101. unsigned long resident;
  102. unsigned long shared_clean;
  103. unsigned long shared_dirty;
  104. unsigned long private_clean;
  105. unsigned long private_dirty;
  106. };
  107. static int show_map_internal(struct seq_file *m, void *v, struct mem_size_stats *mss)
  108. {
  109. struct task_struct *task = m->private;
  110. struct vm_area_struct *vma = v;
  111. struct mm_struct *mm = vma->vm_mm;
  112. struct file *file = vma->vm_file;
  113. int flags = vma->vm_flags;
  114. unsigned long ino = 0;
  115. dev_t dev = 0;
  116. int len;
  117. if (file) {
  118. struct inode *inode = vma->vm_file->f_dentry->d_inode;
  119. dev = inode->i_sb->s_dev;
  120. ino = inode->i_ino;
  121. }
  122. seq_printf(m, "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n",
  123. vma->vm_start,
  124. vma->vm_end,
  125. flags & VM_READ ? 'r' : '-',
  126. flags & VM_WRITE ? 'w' : '-',
  127. flags & VM_EXEC ? 'x' : '-',
  128. flags & VM_MAYSHARE ? 's' : 'p',
  129. vma->vm_pgoff << PAGE_SHIFT,
  130. MAJOR(dev), MINOR(dev), ino, &len);
  131. /*
  132. * Print the dentry name for named mappings, and a
  133. * special [heap] marker for the heap:
  134. */
  135. if (file) {
  136. pad_len_spaces(m, len);
  137. seq_path(m, file->f_vfsmnt, file->f_dentry, "\n");
  138. } else {
  139. if (mm) {
  140. if (vma->vm_start <= mm->start_brk &&
  141. vma->vm_end >= mm->brk) {
  142. pad_len_spaces(m, len);
  143. seq_puts(m, "[heap]");
  144. } else {
  145. if (vma->vm_start <= mm->start_stack &&
  146. vma->vm_end >= mm->start_stack) {
  147. pad_len_spaces(m, len);
  148. seq_puts(m, "[stack]");
  149. }
  150. }
  151. } else {
  152. pad_len_spaces(m, len);
  153. seq_puts(m, "[vdso]");
  154. }
  155. }
  156. seq_putc(m, '\n');
  157. if (mss)
  158. seq_printf(m,
  159. "Size: %8lu kB\n"
  160. "Rss: %8lu kB\n"
  161. "Shared_Clean: %8lu kB\n"
  162. "Shared_Dirty: %8lu kB\n"
  163. "Private_Clean: %8lu kB\n"
  164. "Private_Dirty: %8lu kB\n",
  165. (vma->vm_end - vma->vm_start) >> 10,
  166. mss->resident >> 10,
  167. mss->shared_clean >> 10,
  168. mss->shared_dirty >> 10,
  169. mss->private_clean >> 10,
  170. mss->private_dirty >> 10);
  171. if (m->count < m->size) /* vma is copied successfully */
  172. m->version = (vma != get_gate_vma(task))? vma->vm_start: 0;
  173. return 0;
  174. }
  175. static int show_map(struct seq_file *m, void *v)
  176. {
  177. return show_map_internal(m, v, NULL);
  178. }
  179. static void smaps_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  180. unsigned long addr, unsigned long end,
  181. struct mem_size_stats *mss)
  182. {
  183. pte_t *pte, ptent;
  184. spinlock_t *ptl;
  185. unsigned long pfn;
  186. struct page *page;
  187. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  188. do {
  189. ptent = *pte;
  190. if (!pte_present(ptent))
  191. continue;
  192. mss->resident += PAGE_SIZE;
  193. pfn = pte_pfn(ptent);
  194. if (!pfn_valid(pfn))
  195. continue;
  196. page = pfn_to_page(pfn);
  197. if (page_count(page) >= 2) {
  198. if (pte_dirty(ptent))
  199. mss->shared_dirty += PAGE_SIZE;
  200. else
  201. mss->shared_clean += PAGE_SIZE;
  202. } else {
  203. if (pte_dirty(ptent))
  204. mss->private_dirty += PAGE_SIZE;
  205. else
  206. mss->private_clean += PAGE_SIZE;
  207. }
  208. } while (pte++, addr += PAGE_SIZE, addr != end);
  209. pte_unmap_unlock(pte - 1, ptl);
  210. cond_resched();
  211. }
  212. static inline void smaps_pmd_range(struct vm_area_struct *vma, pud_t *pud,
  213. unsigned long addr, unsigned long end,
  214. struct mem_size_stats *mss)
  215. {
  216. pmd_t *pmd;
  217. unsigned long next;
  218. pmd = pmd_offset(pud, addr);
  219. do {
  220. next = pmd_addr_end(addr, end);
  221. if (pmd_none_or_clear_bad(pmd))
  222. continue;
  223. smaps_pte_range(vma, pmd, addr, next, mss);
  224. } while (pmd++, addr = next, addr != end);
  225. }
  226. static inline void smaps_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
  227. unsigned long addr, unsigned long end,
  228. struct mem_size_stats *mss)
  229. {
  230. pud_t *pud;
  231. unsigned long next;
  232. pud = pud_offset(pgd, addr);
  233. do {
  234. next = pud_addr_end(addr, end);
  235. if (pud_none_or_clear_bad(pud))
  236. continue;
  237. smaps_pmd_range(vma, pud, addr, next, mss);
  238. } while (pud++, addr = next, addr != end);
  239. }
  240. static inline void smaps_pgd_range(struct vm_area_struct *vma,
  241. unsigned long addr, unsigned long end,
  242. struct mem_size_stats *mss)
  243. {
  244. pgd_t *pgd;
  245. unsigned long next;
  246. pgd = pgd_offset(vma->vm_mm, addr);
  247. do {
  248. next = pgd_addr_end(addr, end);
  249. if (pgd_none_or_clear_bad(pgd))
  250. continue;
  251. smaps_pud_range(vma, pgd, addr, next, mss);
  252. } while (pgd++, addr = next, addr != end);
  253. }
  254. static int show_smap(struct seq_file *m, void *v)
  255. {
  256. struct vm_area_struct *vma = v;
  257. struct mem_size_stats mss;
  258. memset(&mss, 0, sizeof mss);
  259. if (vma->vm_mm)
  260. smaps_pgd_range(vma, vma->vm_start, vma->vm_end, &mss);
  261. return show_map_internal(m, v, &mss);
  262. }
  263. static void *m_start(struct seq_file *m, loff_t *pos)
  264. {
  265. struct task_struct *task = m->private;
  266. unsigned long last_addr = m->version;
  267. struct mm_struct *mm;
  268. struct vm_area_struct *vma, *tail_vma;
  269. loff_t l = *pos;
  270. /*
  271. * We remember last_addr rather than next_addr to hit with
  272. * mmap_cache most of the time. We have zero last_addr at
  273. * the beginning and also after lseek. We will have -1 last_addr
  274. * after the end of the vmas.
  275. */
  276. if (last_addr == -1UL)
  277. return NULL;
  278. mm = get_task_mm(task);
  279. if (!mm)
  280. return NULL;
  281. tail_vma = get_gate_vma(task);
  282. down_read(&mm->mmap_sem);
  283. /* Start with last addr hint */
  284. if (last_addr && (vma = find_vma(mm, last_addr))) {
  285. vma = vma->vm_next;
  286. goto out;
  287. }
  288. /*
  289. * Check the vma index is within the range and do
  290. * sequential scan until m_index.
  291. */
  292. vma = NULL;
  293. if ((unsigned long)l < mm->map_count) {
  294. vma = mm->mmap;
  295. while (l-- && vma)
  296. vma = vma->vm_next;
  297. goto out;
  298. }
  299. if (l != mm->map_count)
  300. tail_vma = NULL; /* After gate vma */
  301. out:
  302. if (vma)
  303. return vma;
  304. /* End of vmas has been reached */
  305. m->version = (tail_vma != NULL)? 0: -1UL;
  306. up_read(&mm->mmap_sem);
  307. mmput(mm);
  308. return tail_vma;
  309. }
  310. static void m_stop(struct seq_file *m, void *v)
  311. {
  312. struct task_struct *task = m->private;
  313. struct vm_area_struct *vma = v;
  314. if (vma && vma != get_gate_vma(task)) {
  315. struct mm_struct *mm = vma->vm_mm;
  316. up_read(&mm->mmap_sem);
  317. mmput(mm);
  318. }
  319. }
  320. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  321. {
  322. struct task_struct *task = m->private;
  323. struct vm_area_struct *vma = v;
  324. struct vm_area_struct *tail_vma = get_gate_vma(task);
  325. (*pos)++;
  326. if (vma && (vma != tail_vma) && vma->vm_next)
  327. return vma->vm_next;
  328. m_stop(m, v);
  329. return (vma != tail_vma)? tail_vma: NULL;
  330. }
  331. struct seq_operations proc_pid_maps_op = {
  332. .start = m_start,
  333. .next = m_next,
  334. .stop = m_stop,
  335. .show = show_map
  336. };
  337. struct seq_operations proc_pid_smaps_op = {
  338. .start = m_start,
  339. .next = m_next,
  340. .stop = m_stop,
  341. .show = show_smap
  342. };
  343. #ifdef CONFIG_NUMA
  344. extern int show_numa_map(struct seq_file *m, void *v);
  345. struct seq_operations proc_pid_numa_maps_op = {
  346. .start = m_start,
  347. .next = m_next,
  348. .stop = m_stop,
  349. .show = show_numa_map
  350. };
  351. #endif