task_mmu.c 21 KB

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