task_mmu.c 19 KB

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