task_mmu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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_path.mnt);
  88. *dentry = dget(vma->vm_file->f_path.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. static int show_map_internal(struct seq_file *m, void *v, struct mem_size_stats *mss)
  112. {
  113. struct proc_maps_private *priv = m->private;
  114. struct task_struct *task = priv->task;
  115. struct vm_area_struct *vma = v;
  116. struct mm_struct *mm = vma->vm_mm;
  117. struct file *file = vma->vm_file;
  118. int flags = vma->vm_flags;
  119. unsigned long ino = 0;
  120. dev_t dev = 0;
  121. int len;
  122. if (file) {
  123. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  124. dev = inode->i_sb->s_dev;
  125. ino = inode->i_ino;
  126. }
  127. seq_printf(m, "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n",
  128. vma->vm_start,
  129. vma->vm_end,
  130. flags & VM_READ ? 'r' : '-',
  131. flags & VM_WRITE ? 'w' : '-',
  132. flags & VM_EXEC ? 'x' : '-',
  133. flags & VM_MAYSHARE ? 's' : 'p',
  134. vma->vm_pgoff << PAGE_SHIFT,
  135. MAJOR(dev), MINOR(dev), ino, &len);
  136. /*
  137. * Print the dentry name for named mappings, and a
  138. * special [heap] marker for the heap:
  139. */
  140. if (file) {
  141. pad_len_spaces(m, len);
  142. seq_path(m, file->f_path.mnt, file->f_path.dentry, "\n");
  143. } else {
  144. const char *name = arch_vma_name(vma);
  145. if (!name) {
  146. if (mm) {
  147. if (vma->vm_start <= mm->start_brk &&
  148. vma->vm_end >= mm->brk) {
  149. name = "[heap]";
  150. } else if (vma->vm_start <= mm->start_stack &&
  151. vma->vm_end >= mm->start_stack) {
  152. name = "[stack]";
  153. }
  154. } else {
  155. name = "[vdso]";
  156. }
  157. }
  158. if (name) {
  159. pad_len_spaces(m, len);
  160. seq_puts(m, name);
  161. }
  162. }
  163. seq_putc(m, '\n');
  164. if (mss)
  165. seq_printf(m,
  166. "Size: %8lu kB\n"
  167. "Rss: %8lu kB\n"
  168. "Shared_Clean: %8lu kB\n"
  169. "Shared_Dirty: %8lu kB\n"
  170. "Private_Clean: %8lu kB\n"
  171. "Private_Dirty: %8lu kB\n",
  172. (vma->vm_end - vma->vm_start) >> 10,
  173. mss->resident >> 10,
  174. mss->shared_clean >> 10,
  175. mss->shared_dirty >> 10,
  176. mss->private_clean >> 10,
  177. mss->private_dirty >> 10);
  178. if (m->count < m->size) /* vma is copied successfully */
  179. m->version = (vma != get_gate_vma(task))? vma->vm_start: 0;
  180. return 0;
  181. }
  182. static int show_map(struct seq_file *m, void *v)
  183. {
  184. return show_map_internal(m, v, NULL);
  185. }
  186. static void smaps_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  187. unsigned long addr, unsigned long end,
  188. struct mem_size_stats *mss)
  189. {
  190. pte_t *pte, ptent;
  191. spinlock_t *ptl;
  192. struct page *page;
  193. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  194. do {
  195. ptent = *pte;
  196. if (!pte_present(ptent))
  197. continue;
  198. mss->resident += PAGE_SIZE;
  199. page = vm_normal_page(vma, addr, ptent);
  200. if (!page)
  201. continue;
  202. if (page_mapcount(page) >= 2) {
  203. if (pte_dirty(ptent))
  204. mss->shared_dirty += PAGE_SIZE;
  205. else
  206. mss->shared_clean += PAGE_SIZE;
  207. } else {
  208. if (pte_dirty(ptent))
  209. mss->private_dirty += PAGE_SIZE;
  210. else
  211. mss->private_clean += PAGE_SIZE;
  212. }
  213. } while (pte++, addr += PAGE_SIZE, addr != end);
  214. pte_unmap_unlock(pte - 1, ptl);
  215. cond_resched();
  216. }
  217. static inline void smaps_pmd_range(struct vm_area_struct *vma, pud_t *pud,
  218. unsigned long addr, unsigned long end,
  219. struct mem_size_stats *mss)
  220. {
  221. pmd_t *pmd;
  222. unsigned long next;
  223. pmd = pmd_offset(pud, addr);
  224. do {
  225. next = pmd_addr_end(addr, end);
  226. if (pmd_none_or_clear_bad(pmd))
  227. continue;
  228. smaps_pte_range(vma, pmd, addr, next, mss);
  229. } while (pmd++, addr = next, addr != end);
  230. }
  231. static inline void smaps_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
  232. unsigned long addr, unsigned long end,
  233. struct mem_size_stats *mss)
  234. {
  235. pud_t *pud;
  236. unsigned long next;
  237. pud = pud_offset(pgd, addr);
  238. do {
  239. next = pud_addr_end(addr, end);
  240. if (pud_none_or_clear_bad(pud))
  241. continue;
  242. smaps_pmd_range(vma, pud, addr, next, mss);
  243. } while (pud++, addr = next, addr != end);
  244. }
  245. static inline void smaps_pgd_range(struct vm_area_struct *vma,
  246. unsigned long addr, unsigned long end,
  247. struct mem_size_stats *mss)
  248. {
  249. pgd_t *pgd;
  250. unsigned long next;
  251. pgd = pgd_offset(vma->vm_mm, addr);
  252. do {
  253. next = pgd_addr_end(addr, end);
  254. if (pgd_none_or_clear_bad(pgd))
  255. continue;
  256. smaps_pud_range(vma, pgd, addr, next, mss);
  257. } while (pgd++, addr = next, addr != end);
  258. }
  259. static int show_smap(struct seq_file *m, void *v)
  260. {
  261. struct vm_area_struct *vma = v;
  262. struct mem_size_stats mss;
  263. memset(&mss, 0, sizeof mss);
  264. if (vma->vm_mm && !is_vm_hugetlb_page(vma))
  265. smaps_pgd_range(vma, vma->vm_start, vma->vm_end, &mss);
  266. return show_map_internal(m, v, &mss);
  267. }
  268. static void *m_start(struct seq_file *m, loff_t *pos)
  269. {
  270. struct proc_maps_private *priv = m->private;
  271. unsigned long last_addr = m->version;
  272. struct mm_struct *mm;
  273. struct vm_area_struct *vma, *tail_vma = NULL;
  274. loff_t l = *pos;
  275. /* Clear the per syscall fields in priv */
  276. priv->task = NULL;
  277. priv->tail_vma = NULL;
  278. /*
  279. * We remember last_addr rather than next_addr to hit with
  280. * mmap_cache most of the time. We have zero last_addr at
  281. * the beginning and also after lseek. We will have -1 last_addr
  282. * after the end of the vmas.
  283. */
  284. if (last_addr == -1UL)
  285. return NULL;
  286. priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
  287. if (!priv->task)
  288. return NULL;
  289. mm = get_task_mm(priv->task);
  290. if (!mm)
  291. return NULL;
  292. priv->tail_vma = tail_vma = get_gate_vma(priv->task);
  293. down_read(&mm->mmap_sem);
  294. /* Start with last addr hint */
  295. if (last_addr && (vma = find_vma(mm, last_addr))) {
  296. vma = vma->vm_next;
  297. goto out;
  298. }
  299. /*
  300. * Check the vma index is within the range and do
  301. * sequential scan until m_index.
  302. */
  303. vma = NULL;
  304. if ((unsigned long)l < mm->map_count) {
  305. vma = mm->mmap;
  306. while (l-- && vma)
  307. vma = vma->vm_next;
  308. goto out;
  309. }
  310. if (l != mm->map_count)
  311. tail_vma = NULL; /* After gate vma */
  312. out:
  313. if (vma)
  314. return vma;
  315. /* End of vmas has been reached */
  316. m->version = (tail_vma != NULL)? 0: -1UL;
  317. up_read(&mm->mmap_sem);
  318. mmput(mm);
  319. return tail_vma;
  320. }
  321. static void vma_stop(struct proc_maps_private *priv, struct vm_area_struct *vma)
  322. {
  323. if (vma && vma != priv->tail_vma) {
  324. struct mm_struct *mm = vma->vm_mm;
  325. up_read(&mm->mmap_sem);
  326. mmput(mm);
  327. }
  328. }
  329. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  330. {
  331. struct proc_maps_private *priv = m->private;
  332. struct vm_area_struct *vma = v;
  333. struct vm_area_struct *tail_vma = priv->tail_vma;
  334. (*pos)++;
  335. if (vma && (vma != tail_vma) && vma->vm_next)
  336. return vma->vm_next;
  337. vma_stop(priv, vma);
  338. return (vma != tail_vma)? tail_vma: NULL;
  339. }
  340. static void m_stop(struct seq_file *m, void *v)
  341. {
  342. struct proc_maps_private *priv = m->private;
  343. struct vm_area_struct *vma = v;
  344. vma_stop(priv, vma);
  345. if (priv->task)
  346. put_task_struct(priv->task);
  347. }
  348. static struct seq_operations proc_pid_maps_op = {
  349. .start = m_start,
  350. .next = m_next,
  351. .stop = m_stop,
  352. .show = show_map
  353. };
  354. static struct seq_operations proc_pid_smaps_op = {
  355. .start = m_start,
  356. .next = m_next,
  357. .stop = m_stop,
  358. .show = show_smap
  359. };
  360. static int do_maps_open(struct inode *inode, struct file *file,
  361. struct seq_operations *ops)
  362. {
  363. struct proc_maps_private *priv;
  364. int ret = -ENOMEM;
  365. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  366. if (priv) {
  367. priv->pid = proc_pid(inode);
  368. ret = seq_open(file, ops);
  369. if (!ret) {
  370. struct seq_file *m = file->private_data;
  371. m->private = priv;
  372. } else {
  373. kfree(priv);
  374. }
  375. }
  376. return ret;
  377. }
  378. static int maps_open(struct inode *inode, struct file *file)
  379. {
  380. return do_maps_open(inode, file, &proc_pid_maps_op);
  381. }
  382. const struct file_operations proc_maps_operations = {
  383. .open = maps_open,
  384. .read = seq_read,
  385. .llseek = seq_lseek,
  386. .release = seq_release_private,
  387. };
  388. #ifdef CONFIG_NUMA
  389. extern int show_numa_map(struct seq_file *m, void *v);
  390. static struct seq_operations proc_pid_numa_maps_op = {
  391. .start = m_start,
  392. .next = m_next,
  393. .stop = m_stop,
  394. .show = show_numa_map
  395. };
  396. static int numa_maps_open(struct inode *inode, struct file *file)
  397. {
  398. return do_maps_open(inode, file, &proc_pid_numa_maps_op);
  399. }
  400. const struct file_operations proc_numa_maps_operations = {
  401. .open = numa_maps_open,
  402. .read = seq_read,
  403. .llseek = seq_lseek,
  404. .release = seq_release_private,
  405. };
  406. #endif
  407. static int smaps_open(struct inode *inode, struct file *file)
  408. {
  409. return do_maps_open(inode, file, &proc_pid_smaps_op);
  410. }
  411. const struct file_operations proc_smaps_operations = {
  412. .open = smaps_open,
  413. .read = seq_read,
  414. .llseek = seq_lseek,
  415. .release = seq_release_private,
  416. };