task_mmu.c 13 KB

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