task_mmu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. #include <linux/mm.h>
  2. #include <linux/hugetlb.h>
  3. #include <linux/huge_mm.h>
  4. #include <linux/mount.h>
  5. #include <linux/seq_file.h>
  6. #include <linux/highmem.h>
  7. #include <linux/ptrace.h>
  8. #include <linux/slab.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/mempolicy.h>
  11. #include <linux/rmap.h>
  12. #include <linux/swap.h>
  13. #include <linux/swapops.h>
  14. #include <asm/elf.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/tlbflush.h>
  17. #include "internal.h"
  18. void task_mem(struct seq_file *m, struct mm_struct *mm)
  19. {
  20. unsigned long data, text, lib, swap;
  21. unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
  22. /*
  23. * Note: to minimize their overhead, mm maintains hiwater_vm and
  24. * hiwater_rss only when about to *lower* total_vm or rss. Any
  25. * collector of these hiwater stats must therefore get total_vm
  26. * and rss too, which will usually be the higher. Barriers? not
  27. * worth the effort, such snapshots can always be inconsistent.
  28. */
  29. hiwater_vm = total_vm = mm->total_vm;
  30. if (hiwater_vm < mm->hiwater_vm)
  31. hiwater_vm = mm->hiwater_vm;
  32. hiwater_rss = total_rss = get_mm_rss(mm);
  33. if (hiwater_rss < mm->hiwater_rss)
  34. hiwater_rss = mm->hiwater_rss;
  35. data = mm->total_vm - mm->shared_vm - mm->stack_vm;
  36. text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
  37. lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
  38. swap = get_mm_counter(mm, MM_SWAPENTS);
  39. seq_printf(m,
  40. "VmPeak:\t%8lu kB\n"
  41. "VmSize:\t%8lu kB\n"
  42. "VmLck:\t%8lu kB\n"
  43. "VmHWM:\t%8lu kB\n"
  44. "VmRSS:\t%8lu kB\n"
  45. "VmData:\t%8lu kB\n"
  46. "VmStk:\t%8lu kB\n"
  47. "VmExe:\t%8lu kB\n"
  48. "VmLib:\t%8lu kB\n"
  49. "VmPTE:\t%8lu kB\n"
  50. "VmSwap:\t%8lu kB\n",
  51. hiwater_vm << (PAGE_SHIFT-10),
  52. (total_vm - mm->reserved_vm) << (PAGE_SHIFT-10),
  53. mm->locked_vm << (PAGE_SHIFT-10),
  54. hiwater_rss << (PAGE_SHIFT-10),
  55. total_rss << (PAGE_SHIFT-10),
  56. data << (PAGE_SHIFT-10),
  57. mm->stack_vm << (PAGE_SHIFT-10), text, lib,
  58. (PTRS_PER_PTE*sizeof(pte_t)*mm->nr_ptes) >> 10,
  59. swap << (PAGE_SHIFT-10));
  60. }
  61. unsigned long task_vsize(struct mm_struct *mm)
  62. {
  63. return PAGE_SIZE * mm->total_vm;
  64. }
  65. unsigned long task_statm(struct mm_struct *mm,
  66. unsigned long *shared, unsigned long *text,
  67. unsigned long *data, unsigned long *resident)
  68. {
  69. *shared = get_mm_counter(mm, MM_FILEPAGES);
  70. *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
  71. >> PAGE_SHIFT;
  72. *data = mm->total_vm - mm->shared_vm;
  73. *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
  74. return mm->total_vm;
  75. }
  76. static void pad_len_spaces(struct seq_file *m, int len)
  77. {
  78. len = 25 + sizeof(void*) * 6 - len;
  79. if (len < 1)
  80. len = 1;
  81. seq_printf(m, "%*c", len, ' ');
  82. }
  83. static void vma_stop(struct proc_maps_private *priv, struct vm_area_struct *vma)
  84. {
  85. if (vma && vma != priv->tail_vma) {
  86. struct mm_struct *mm = vma->vm_mm;
  87. up_read(&mm->mmap_sem);
  88. mmput(mm);
  89. }
  90. }
  91. static void *m_start(struct seq_file *m, loff_t *pos)
  92. {
  93. struct proc_maps_private *priv = m->private;
  94. unsigned long last_addr = m->version;
  95. struct mm_struct *mm;
  96. struct vm_area_struct *vma, *tail_vma = NULL;
  97. loff_t l = *pos;
  98. /* Clear the per syscall fields in priv */
  99. priv->task = NULL;
  100. priv->tail_vma = NULL;
  101. /*
  102. * We remember last_addr rather than next_addr to hit with
  103. * mmap_cache most of the time. We have zero last_addr at
  104. * the beginning and also after lseek. We will have -1 last_addr
  105. * after the end of the vmas.
  106. */
  107. if (last_addr == -1UL)
  108. return NULL;
  109. priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
  110. if (!priv->task)
  111. return NULL;
  112. mm = mm_for_maps(priv->task);
  113. if (!mm)
  114. return NULL;
  115. down_read(&mm->mmap_sem);
  116. tail_vma = get_gate_vma(priv->task);
  117. priv->tail_vma = tail_vma;
  118. /* Start with last addr hint */
  119. vma = find_vma(mm, last_addr);
  120. if (last_addr && vma) {
  121. vma = vma->vm_next;
  122. goto out;
  123. }
  124. /*
  125. * Check the vma index is within the range and do
  126. * sequential scan until m_index.
  127. */
  128. vma = NULL;
  129. if ((unsigned long)l < mm->map_count) {
  130. vma = mm->mmap;
  131. while (l-- && vma)
  132. vma = vma->vm_next;
  133. goto out;
  134. }
  135. if (l != mm->map_count)
  136. tail_vma = NULL; /* After gate vma */
  137. out:
  138. if (vma)
  139. return vma;
  140. /* End of vmas has been reached */
  141. m->version = (tail_vma != NULL)? 0: -1UL;
  142. up_read(&mm->mmap_sem);
  143. mmput(mm);
  144. return tail_vma;
  145. }
  146. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  147. {
  148. struct proc_maps_private *priv = m->private;
  149. struct vm_area_struct *vma = v;
  150. struct vm_area_struct *tail_vma = priv->tail_vma;
  151. (*pos)++;
  152. if (vma && (vma != tail_vma) && vma->vm_next)
  153. return vma->vm_next;
  154. vma_stop(priv, vma);
  155. return (vma != tail_vma)? tail_vma: NULL;
  156. }
  157. static void m_stop(struct seq_file *m, void *v)
  158. {
  159. struct proc_maps_private *priv = m->private;
  160. struct vm_area_struct *vma = v;
  161. vma_stop(priv, vma);
  162. if (priv->task)
  163. put_task_struct(priv->task);
  164. }
  165. static int do_maps_open(struct inode *inode, struct file *file,
  166. const struct seq_operations *ops)
  167. {
  168. struct proc_maps_private *priv;
  169. int ret = -ENOMEM;
  170. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  171. if (priv) {
  172. priv->pid = proc_pid(inode);
  173. ret = seq_open(file, ops);
  174. if (!ret) {
  175. struct seq_file *m = file->private_data;
  176. m->private = priv;
  177. } else {
  178. kfree(priv);
  179. }
  180. }
  181. return ret;
  182. }
  183. static void show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
  184. {
  185. struct mm_struct *mm = vma->vm_mm;
  186. struct file *file = vma->vm_file;
  187. int flags = vma->vm_flags;
  188. unsigned long ino = 0;
  189. unsigned long long pgoff = 0;
  190. unsigned long start;
  191. dev_t dev = 0;
  192. int len;
  193. if (file) {
  194. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  195. dev = inode->i_sb->s_dev;
  196. ino = inode->i_ino;
  197. pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
  198. }
  199. /* We don't show the stack guard page in /proc/maps */
  200. start = vma->vm_start;
  201. if (vma->vm_flags & VM_GROWSDOWN)
  202. if (!vma_stack_continue(vma->vm_prev, vma->vm_start))
  203. start += PAGE_SIZE;
  204. seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
  205. start,
  206. vma->vm_end,
  207. flags & VM_READ ? 'r' : '-',
  208. flags & VM_WRITE ? 'w' : '-',
  209. flags & VM_EXEC ? 'x' : '-',
  210. flags & VM_MAYSHARE ? 's' : 'p',
  211. pgoff,
  212. MAJOR(dev), MINOR(dev), ino, &len);
  213. /*
  214. * Print the dentry name for named mappings, and a
  215. * special [heap] marker for the heap:
  216. */
  217. if (file) {
  218. pad_len_spaces(m, len);
  219. seq_path(m, &file->f_path, "\n");
  220. } else {
  221. const char *name = arch_vma_name(vma);
  222. if (!name) {
  223. if (mm) {
  224. if (vma->vm_start <= mm->start_brk &&
  225. vma->vm_end >= mm->brk) {
  226. name = "[heap]";
  227. } else if (vma->vm_start <= mm->start_stack &&
  228. vma->vm_end >= mm->start_stack) {
  229. name = "[stack]";
  230. }
  231. } else {
  232. name = "[vdso]";
  233. }
  234. }
  235. if (name) {
  236. pad_len_spaces(m, len);
  237. seq_puts(m, name);
  238. }
  239. }
  240. seq_putc(m, '\n');
  241. }
  242. static int show_map(struct seq_file *m, void *v)
  243. {
  244. struct vm_area_struct *vma = v;
  245. struct proc_maps_private *priv = m->private;
  246. struct task_struct *task = priv->task;
  247. show_map_vma(m, vma);
  248. if (m->count < m->size) /* vma is copied successfully */
  249. m->version = (vma != get_gate_vma(task))? vma->vm_start: 0;
  250. return 0;
  251. }
  252. static const struct seq_operations proc_pid_maps_op = {
  253. .start = m_start,
  254. .next = m_next,
  255. .stop = m_stop,
  256. .show = show_map
  257. };
  258. static int maps_open(struct inode *inode, struct file *file)
  259. {
  260. return do_maps_open(inode, file, &proc_pid_maps_op);
  261. }
  262. const struct file_operations proc_maps_operations = {
  263. .open = maps_open,
  264. .read = seq_read,
  265. .llseek = seq_lseek,
  266. .release = seq_release_private,
  267. };
  268. /*
  269. * Proportional Set Size(PSS): my share of RSS.
  270. *
  271. * PSS of a process is the count of pages it has in memory, where each
  272. * page is divided by the number of processes sharing it. So if a
  273. * process has 1000 pages all to itself, and 1000 shared with one other
  274. * process, its PSS will be 1500.
  275. *
  276. * To keep (accumulated) division errors low, we adopt a 64bit
  277. * fixed-point pss counter to minimize division errors. So (pss >>
  278. * PSS_SHIFT) would be the real byte count.
  279. *
  280. * A shift of 12 before division means (assuming 4K page size):
  281. * - 1M 3-user-pages add up to 8KB errors;
  282. * - supports mapcount up to 2^24, or 16M;
  283. * - supports PSS up to 2^52 bytes, or 4PB.
  284. */
  285. #define PSS_SHIFT 12
  286. #ifdef CONFIG_PROC_PAGE_MONITOR
  287. struct mem_size_stats {
  288. struct vm_area_struct *vma;
  289. unsigned long resident;
  290. unsigned long shared_clean;
  291. unsigned long shared_dirty;
  292. unsigned long private_clean;
  293. unsigned long private_dirty;
  294. unsigned long referenced;
  295. unsigned long anonymous;
  296. unsigned long swap;
  297. u64 pss;
  298. };
  299. static void smaps_pte_entry(pte_t ptent, unsigned long addr,
  300. unsigned long ptent_size, struct mm_walk *walk)
  301. {
  302. struct mem_size_stats *mss = walk->private;
  303. struct vm_area_struct *vma = mss->vma;
  304. struct page *page;
  305. int mapcount;
  306. if (is_swap_pte(ptent)) {
  307. mss->swap += ptent_size;
  308. return;
  309. }
  310. if (!pte_present(ptent))
  311. return;
  312. page = vm_normal_page(vma, addr, ptent);
  313. if (!page)
  314. return;
  315. if (PageAnon(page))
  316. mss->anonymous += ptent_size;
  317. mss->resident += ptent_size;
  318. /* Accumulate the size in pages that have been accessed. */
  319. if (pte_young(ptent) || PageReferenced(page))
  320. mss->referenced += ptent_size;
  321. mapcount = page_mapcount(page);
  322. if (mapcount >= 2) {
  323. if (pte_dirty(ptent) || PageDirty(page))
  324. mss->shared_dirty += ptent_size;
  325. else
  326. mss->shared_clean += ptent_size;
  327. mss->pss += (ptent_size << PSS_SHIFT) / mapcount;
  328. } else {
  329. if (pte_dirty(ptent) || PageDirty(page))
  330. mss->private_dirty += ptent_size;
  331. else
  332. mss->private_clean += ptent_size;
  333. mss->pss += (ptent_size << PSS_SHIFT);
  334. }
  335. }
  336. static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
  337. struct mm_walk *walk)
  338. {
  339. struct mem_size_stats *mss = walk->private;
  340. struct vm_area_struct *vma = mss->vma;
  341. pte_t *pte;
  342. spinlock_t *ptl;
  343. spin_lock(&walk->mm->page_table_lock);
  344. if (pmd_trans_huge(*pmd)) {
  345. if (pmd_trans_splitting(*pmd)) {
  346. spin_unlock(&walk->mm->page_table_lock);
  347. wait_split_huge_page(vma->anon_vma, pmd);
  348. } else {
  349. smaps_pte_entry(*(pte_t *)pmd, addr,
  350. HPAGE_PMD_SIZE, walk);
  351. spin_unlock(&walk->mm->page_table_lock);
  352. return 0;
  353. }
  354. } else {
  355. spin_unlock(&walk->mm->page_table_lock);
  356. }
  357. /*
  358. * The mmap_sem held all the way back in m_start() is what
  359. * keeps khugepaged out of here and from collapsing things
  360. * in here.
  361. */
  362. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  363. for (; addr != end; pte++, addr += PAGE_SIZE)
  364. smaps_pte_entry(*pte, addr, PAGE_SIZE, walk);
  365. pte_unmap_unlock(pte - 1, ptl);
  366. cond_resched();
  367. return 0;
  368. }
  369. static int show_smap(struct seq_file *m, void *v)
  370. {
  371. struct proc_maps_private *priv = m->private;
  372. struct task_struct *task = priv->task;
  373. struct vm_area_struct *vma = v;
  374. struct mem_size_stats mss;
  375. struct mm_walk smaps_walk = {
  376. .pmd_entry = smaps_pte_range,
  377. .mm = vma->vm_mm,
  378. .private = &mss,
  379. };
  380. memset(&mss, 0, sizeof mss);
  381. mss.vma = vma;
  382. /* mmap_sem is held in m_start */
  383. if (vma->vm_mm && !is_vm_hugetlb_page(vma))
  384. walk_page_range(vma->vm_start, vma->vm_end, &smaps_walk);
  385. show_map_vma(m, vma);
  386. seq_printf(m,
  387. "Size: %8lu kB\n"
  388. "Rss: %8lu kB\n"
  389. "Pss: %8lu kB\n"
  390. "Shared_Clean: %8lu kB\n"
  391. "Shared_Dirty: %8lu kB\n"
  392. "Private_Clean: %8lu kB\n"
  393. "Private_Dirty: %8lu kB\n"
  394. "Referenced: %8lu kB\n"
  395. "Anonymous: %8lu kB\n"
  396. "Swap: %8lu kB\n"
  397. "KernelPageSize: %8lu kB\n"
  398. "MMUPageSize: %8lu kB\n"
  399. "Locked: %8lu kB\n",
  400. (vma->vm_end - vma->vm_start) >> 10,
  401. mss.resident >> 10,
  402. (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
  403. mss.shared_clean >> 10,
  404. mss.shared_dirty >> 10,
  405. mss.private_clean >> 10,
  406. mss.private_dirty >> 10,
  407. mss.referenced >> 10,
  408. mss.anonymous >> 10,
  409. mss.swap >> 10,
  410. vma_kernel_pagesize(vma) >> 10,
  411. vma_mmu_pagesize(vma) >> 10,
  412. (vma->vm_flags & VM_LOCKED) ?
  413. (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
  414. if (m->count < m->size) /* vma is copied successfully */
  415. m->version = (vma != get_gate_vma(task)) ? vma->vm_start : 0;
  416. return 0;
  417. }
  418. static const struct seq_operations proc_pid_smaps_op = {
  419. .start = m_start,
  420. .next = m_next,
  421. .stop = m_stop,
  422. .show = show_smap
  423. };
  424. static int smaps_open(struct inode *inode, struct file *file)
  425. {
  426. return do_maps_open(inode, file, &proc_pid_smaps_op);
  427. }
  428. const struct file_operations proc_smaps_operations = {
  429. .open = smaps_open,
  430. .read = seq_read,
  431. .llseek = seq_lseek,
  432. .release = seq_release_private,
  433. };
  434. static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
  435. unsigned long end, struct mm_walk *walk)
  436. {
  437. struct vm_area_struct *vma = walk->private;
  438. pte_t *pte, ptent;
  439. spinlock_t *ptl;
  440. struct page *page;
  441. split_huge_page_pmd(walk->mm, pmd);
  442. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  443. for (; addr != end; pte++, addr += PAGE_SIZE) {
  444. ptent = *pte;
  445. if (!pte_present(ptent))
  446. continue;
  447. page = vm_normal_page(vma, addr, ptent);
  448. if (!page)
  449. continue;
  450. /* Clear accessed and referenced bits. */
  451. ptep_test_and_clear_young(vma, addr, pte);
  452. ClearPageReferenced(page);
  453. }
  454. pte_unmap_unlock(pte - 1, ptl);
  455. cond_resched();
  456. return 0;
  457. }
  458. #define CLEAR_REFS_ALL 1
  459. #define CLEAR_REFS_ANON 2
  460. #define CLEAR_REFS_MAPPED 3
  461. static ssize_t clear_refs_write(struct file *file, const char __user *buf,
  462. size_t count, loff_t *ppos)
  463. {
  464. struct task_struct *task;
  465. char buffer[PROC_NUMBUF];
  466. struct mm_struct *mm;
  467. struct vm_area_struct *vma;
  468. long type;
  469. memset(buffer, 0, sizeof(buffer));
  470. if (count > sizeof(buffer) - 1)
  471. count = sizeof(buffer) - 1;
  472. if (copy_from_user(buffer, buf, count))
  473. return -EFAULT;
  474. if (strict_strtol(strstrip(buffer), 10, &type))
  475. return -EINVAL;
  476. if (type < CLEAR_REFS_ALL || type > CLEAR_REFS_MAPPED)
  477. return -EINVAL;
  478. task = get_proc_task(file->f_path.dentry->d_inode);
  479. if (!task)
  480. return -ESRCH;
  481. mm = get_task_mm(task);
  482. if (mm) {
  483. struct mm_walk clear_refs_walk = {
  484. .pmd_entry = clear_refs_pte_range,
  485. .mm = mm,
  486. };
  487. down_read(&mm->mmap_sem);
  488. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  489. clear_refs_walk.private = vma;
  490. if (is_vm_hugetlb_page(vma))
  491. continue;
  492. /*
  493. * Writing 1 to /proc/pid/clear_refs affects all pages.
  494. *
  495. * Writing 2 to /proc/pid/clear_refs only affects
  496. * Anonymous pages.
  497. *
  498. * Writing 3 to /proc/pid/clear_refs only affects file
  499. * mapped pages.
  500. */
  501. if (type == CLEAR_REFS_ANON && vma->vm_file)
  502. continue;
  503. if (type == CLEAR_REFS_MAPPED && !vma->vm_file)
  504. continue;
  505. walk_page_range(vma->vm_start, vma->vm_end,
  506. &clear_refs_walk);
  507. }
  508. flush_tlb_mm(mm);
  509. up_read(&mm->mmap_sem);
  510. mmput(mm);
  511. }
  512. put_task_struct(task);
  513. return count;
  514. }
  515. const struct file_operations proc_clear_refs_operations = {
  516. .write = clear_refs_write,
  517. .llseek = noop_llseek,
  518. };
  519. struct pagemapread {
  520. int pos, len;
  521. u64 *buffer;
  522. };
  523. #define PM_ENTRY_BYTES sizeof(u64)
  524. #define PM_STATUS_BITS 3
  525. #define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
  526. #define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
  527. #define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
  528. #define PM_PSHIFT_BITS 6
  529. #define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
  530. #define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
  531. #define PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
  532. #define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
  533. #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
  534. #define PM_PRESENT PM_STATUS(4LL)
  535. #define PM_SWAP PM_STATUS(2LL)
  536. #define PM_NOT_PRESENT PM_PSHIFT(PAGE_SHIFT)
  537. #define PM_END_OF_BUFFER 1
  538. static int add_to_pagemap(unsigned long addr, u64 pfn,
  539. struct pagemapread *pm)
  540. {
  541. pm->buffer[pm->pos++] = pfn;
  542. if (pm->pos >= pm->len)
  543. return PM_END_OF_BUFFER;
  544. return 0;
  545. }
  546. static int pagemap_pte_hole(unsigned long start, unsigned long end,
  547. struct mm_walk *walk)
  548. {
  549. struct pagemapread *pm = walk->private;
  550. unsigned long addr;
  551. int err = 0;
  552. for (addr = start; addr < end; addr += PAGE_SIZE) {
  553. err = add_to_pagemap(addr, PM_NOT_PRESENT, pm);
  554. if (err)
  555. break;
  556. }
  557. return err;
  558. }
  559. static u64 swap_pte_to_pagemap_entry(pte_t pte)
  560. {
  561. swp_entry_t e = pte_to_swp_entry(pte);
  562. return swp_type(e) | (swp_offset(e) << MAX_SWAPFILES_SHIFT);
  563. }
  564. static u64 pte_to_pagemap_entry(pte_t pte)
  565. {
  566. u64 pme = 0;
  567. if (is_swap_pte(pte))
  568. pme = PM_PFRAME(swap_pte_to_pagemap_entry(pte))
  569. | PM_PSHIFT(PAGE_SHIFT) | PM_SWAP;
  570. else if (pte_present(pte))
  571. pme = PM_PFRAME(pte_pfn(pte))
  572. | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT;
  573. return pme;
  574. }
  575. static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
  576. struct mm_walk *walk)
  577. {
  578. struct vm_area_struct *vma;
  579. struct pagemapread *pm = walk->private;
  580. pte_t *pte;
  581. int err = 0;
  582. split_huge_page_pmd(walk->mm, pmd);
  583. /* find the first VMA at or above 'addr' */
  584. vma = find_vma(walk->mm, addr);
  585. for (; addr != end; addr += PAGE_SIZE) {
  586. u64 pfn = PM_NOT_PRESENT;
  587. /* check to see if we've left 'vma' behind
  588. * and need a new, higher one */
  589. if (vma && (addr >= vma->vm_end))
  590. vma = find_vma(walk->mm, addr);
  591. /* check that 'vma' actually covers this address,
  592. * and that it isn't a huge page vma */
  593. if (vma && (vma->vm_start <= addr) &&
  594. !is_vm_hugetlb_page(vma)) {
  595. pte = pte_offset_map(pmd, addr);
  596. pfn = pte_to_pagemap_entry(*pte);
  597. /* unmap before userspace copy */
  598. pte_unmap(pte);
  599. }
  600. err = add_to_pagemap(addr, pfn, pm);
  601. if (err)
  602. return err;
  603. }
  604. cond_resched();
  605. return err;
  606. }
  607. #ifdef CONFIG_HUGETLB_PAGE
  608. static u64 huge_pte_to_pagemap_entry(pte_t pte, int offset)
  609. {
  610. u64 pme = 0;
  611. if (pte_present(pte))
  612. pme = PM_PFRAME(pte_pfn(pte) + offset)
  613. | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT;
  614. return pme;
  615. }
  616. /* This function walks within one hugetlb entry in the single call */
  617. static int pagemap_hugetlb_range(pte_t *pte, unsigned long hmask,
  618. unsigned long addr, unsigned long end,
  619. struct mm_walk *walk)
  620. {
  621. struct pagemapread *pm = walk->private;
  622. int err = 0;
  623. u64 pfn;
  624. for (; addr != end; addr += PAGE_SIZE) {
  625. int offset = (addr & ~hmask) >> PAGE_SHIFT;
  626. pfn = huge_pte_to_pagemap_entry(*pte, offset);
  627. err = add_to_pagemap(addr, pfn, pm);
  628. if (err)
  629. return err;
  630. }
  631. cond_resched();
  632. return err;
  633. }
  634. #endif /* HUGETLB_PAGE */
  635. /*
  636. * /proc/pid/pagemap - an array mapping virtual pages to pfns
  637. *
  638. * For each page in the address space, this file contains one 64-bit entry
  639. * consisting of the following:
  640. *
  641. * Bits 0-55 page frame number (PFN) if present
  642. * Bits 0-4 swap type if swapped
  643. * Bits 5-55 swap offset if swapped
  644. * Bits 55-60 page shift (page size = 1<<page shift)
  645. * Bit 61 reserved for future use
  646. * Bit 62 page swapped
  647. * Bit 63 page present
  648. *
  649. * If the page is not present but in swap, then the PFN contains an
  650. * encoding of the swap file number and the page's offset into the
  651. * swap. Unmapped pages return a null PFN. This allows determining
  652. * precisely which pages are mapped (or in swap) and comparing mapped
  653. * pages between processes.
  654. *
  655. * Efficient users of this interface will use /proc/pid/maps to
  656. * determine which areas of memory are actually mapped and llseek to
  657. * skip over unmapped regions.
  658. */
  659. #define PAGEMAP_WALK_SIZE (PMD_SIZE)
  660. #define PAGEMAP_WALK_MASK (PMD_MASK)
  661. static ssize_t pagemap_read(struct file *file, char __user *buf,
  662. size_t count, loff_t *ppos)
  663. {
  664. struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
  665. struct mm_struct *mm;
  666. struct pagemapread pm;
  667. int ret = -ESRCH;
  668. struct mm_walk pagemap_walk = {};
  669. unsigned long src;
  670. unsigned long svpfn;
  671. unsigned long start_vaddr;
  672. unsigned long end_vaddr;
  673. int copied = 0;
  674. if (!task)
  675. goto out;
  676. ret = -EACCES;
  677. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  678. goto out_task;
  679. ret = -EINVAL;
  680. /* file position must be aligned */
  681. if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
  682. goto out_task;
  683. ret = 0;
  684. if (!count)
  685. goto out_task;
  686. mm = get_task_mm(task);
  687. if (!mm)
  688. goto out_task;
  689. pm.len = PM_ENTRY_BYTES * (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
  690. pm.buffer = kmalloc(pm.len, GFP_TEMPORARY);
  691. ret = -ENOMEM;
  692. if (!pm.buffer)
  693. goto out_mm;
  694. pagemap_walk.pmd_entry = pagemap_pte_range;
  695. pagemap_walk.pte_hole = pagemap_pte_hole;
  696. #ifdef CONFIG_HUGETLB_PAGE
  697. pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
  698. #endif
  699. pagemap_walk.mm = mm;
  700. pagemap_walk.private = &pm;
  701. src = *ppos;
  702. svpfn = src / PM_ENTRY_BYTES;
  703. start_vaddr = svpfn << PAGE_SHIFT;
  704. end_vaddr = TASK_SIZE_OF(task);
  705. /* watch out for wraparound */
  706. if (svpfn > TASK_SIZE_OF(task) >> PAGE_SHIFT)
  707. start_vaddr = end_vaddr;
  708. /*
  709. * The odds are that this will stop walking way
  710. * before end_vaddr, because the length of the
  711. * user buffer is tracked in "pm", and the walk
  712. * will stop when we hit the end of the buffer.
  713. */
  714. ret = 0;
  715. while (count && (start_vaddr < end_vaddr)) {
  716. int len;
  717. unsigned long end;
  718. pm.pos = 0;
  719. end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
  720. /* overflow ? */
  721. if (end < start_vaddr || end > end_vaddr)
  722. end = end_vaddr;
  723. down_read(&mm->mmap_sem);
  724. ret = walk_page_range(start_vaddr, end, &pagemap_walk);
  725. up_read(&mm->mmap_sem);
  726. start_vaddr = end;
  727. len = min(count, PM_ENTRY_BYTES * pm.pos);
  728. if (copy_to_user(buf, pm.buffer, len)) {
  729. ret = -EFAULT;
  730. goto out_free;
  731. }
  732. copied += len;
  733. buf += len;
  734. count -= len;
  735. }
  736. *ppos += copied;
  737. if (!ret || ret == PM_END_OF_BUFFER)
  738. ret = copied;
  739. out_free:
  740. kfree(pm.buffer);
  741. out_mm:
  742. mmput(mm);
  743. out_task:
  744. put_task_struct(task);
  745. out:
  746. return ret;
  747. }
  748. const struct file_operations proc_pagemap_operations = {
  749. .llseek = mem_lseek, /* borrow this */
  750. .read = pagemap_read,
  751. };
  752. #endif /* CONFIG_PROC_PAGE_MONITOR */
  753. #ifdef CONFIG_NUMA
  754. extern int show_numa_map(struct seq_file *m, void *v);
  755. static const struct seq_operations proc_pid_numa_maps_op = {
  756. .start = m_start,
  757. .next = m_next,
  758. .stop = m_stop,
  759. .show = show_numa_map,
  760. };
  761. static int numa_maps_open(struct inode *inode, struct file *file)
  762. {
  763. return do_maps_open(inode, file, &proc_pid_numa_maps_op);
  764. }
  765. const struct file_operations proc_numa_maps_operations = {
  766. .open = numa_maps_open,
  767. .read = seq_read,
  768. .llseek = seq_lseek,
  769. .release = seq_release_private,
  770. };
  771. #endif