task_mmu.c 20 KB

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