task_mmu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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 ERR_PTR(-ESRCH);
  112. mm = mm_for_maps(priv->task);
  113. if (!mm || IS_ERR(mm))
  114. return mm;
  115. down_read(&mm->mmap_sem);
  116. tail_vma = get_gate_vma(priv->task->mm);
  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. if (!IS_ERR(vma))
  162. vma_stop(priv, vma);
  163. if (priv->task)
  164. put_task_struct(priv->task);
  165. }
  166. static int do_maps_open(struct inode *inode, struct file *file,
  167. const struct seq_operations *ops)
  168. {
  169. struct proc_maps_private *priv;
  170. int ret = -ENOMEM;
  171. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  172. if (priv) {
  173. priv->pid = proc_pid(inode);
  174. ret = seq_open(file, ops);
  175. if (!ret) {
  176. struct seq_file *m = file->private_data;
  177. m->private = priv;
  178. } else {
  179. kfree(priv);
  180. }
  181. }
  182. return ret;
  183. }
  184. static void show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
  185. {
  186. struct mm_struct *mm = vma->vm_mm;
  187. struct file *file = vma->vm_file;
  188. int flags = vma->vm_flags;
  189. unsigned long ino = 0;
  190. unsigned long long pgoff = 0;
  191. unsigned long start, end;
  192. dev_t dev = 0;
  193. int len;
  194. if (file) {
  195. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  196. dev = inode->i_sb->s_dev;
  197. ino = inode->i_ino;
  198. pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
  199. }
  200. /* We don't show the stack guard page in /proc/maps */
  201. start = vma->vm_start;
  202. if (stack_guard_page_start(vma, start))
  203. start += PAGE_SIZE;
  204. end = vma->vm_end;
  205. if (stack_guard_page_end(vma, end))
  206. end -= PAGE_SIZE;
  207. seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
  208. start,
  209. end,
  210. flags & VM_READ ? 'r' : '-',
  211. flags & VM_WRITE ? 'w' : '-',
  212. flags & VM_EXEC ? 'x' : '-',
  213. flags & VM_MAYSHARE ? 's' : 'p',
  214. pgoff,
  215. MAJOR(dev), MINOR(dev), ino, &len);
  216. /*
  217. * Print the dentry name for named mappings, and a
  218. * special [heap] marker for the heap:
  219. */
  220. if (file) {
  221. pad_len_spaces(m, len);
  222. seq_path(m, &file->f_path, "\n");
  223. } else {
  224. const char *name = arch_vma_name(vma);
  225. if (!name) {
  226. if (mm) {
  227. if (vma->vm_start <= mm->brk &&
  228. vma->vm_end >= mm->start_brk) {
  229. name = "[heap]";
  230. } else if (vma->vm_start <= mm->start_stack &&
  231. vma->vm_end >= mm->start_stack) {
  232. name = "[stack]";
  233. }
  234. } else {
  235. name = "[vdso]";
  236. }
  237. }
  238. if (name) {
  239. pad_len_spaces(m, len);
  240. seq_puts(m, name);
  241. }
  242. }
  243. seq_putc(m, '\n');
  244. }
  245. static int show_map(struct seq_file *m, void *v)
  246. {
  247. struct vm_area_struct *vma = v;
  248. struct proc_maps_private *priv = m->private;
  249. struct task_struct *task = priv->task;
  250. show_map_vma(m, vma);
  251. if (m->count < m->size) /* vma is copied successfully */
  252. m->version = (vma != get_gate_vma(task->mm))
  253. ? 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 anonymous;
  300. unsigned long anonymous_thp;
  301. unsigned long swap;
  302. u64 pss;
  303. };
  304. static void smaps_pte_entry(pte_t ptent, unsigned long addr,
  305. unsigned long ptent_size, struct mm_walk *walk)
  306. {
  307. struct mem_size_stats *mss = walk->private;
  308. struct vm_area_struct *vma = mss->vma;
  309. struct page *page;
  310. int mapcount;
  311. if (is_swap_pte(ptent)) {
  312. mss->swap += ptent_size;
  313. return;
  314. }
  315. if (!pte_present(ptent))
  316. return;
  317. page = vm_normal_page(vma, addr, ptent);
  318. if (!page)
  319. return;
  320. if (PageAnon(page))
  321. mss->anonymous += ptent_size;
  322. mss->resident += ptent_size;
  323. /* Accumulate the size in pages that have been accessed. */
  324. if (pte_young(ptent) || PageReferenced(page))
  325. mss->referenced += ptent_size;
  326. mapcount = page_mapcount(page);
  327. if (mapcount >= 2) {
  328. if (pte_dirty(ptent) || PageDirty(page))
  329. mss->shared_dirty += ptent_size;
  330. else
  331. mss->shared_clean += ptent_size;
  332. mss->pss += (ptent_size << PSS_SHIFT) / mapcount;
  333. } else {
  334. if (pte_dirty(ptent) || PageDirty(page))
  335. mss->private_dirty += ptent_size;
  336. else
  337. mss->private_clean += ptent_size;
  338. mss->pss += (ptent_size << PSS_SHIFT);
  339. }
  340. }
  341. static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
  342. struct mm_walk *walk)
  343. {
  344. struct mem_size_stats *mss = walk->private;
  345. struct vm_area_struct *vma = mss->vma;
  346. pte_t *pte;
  347. spinlock_t *ptl;
  348. spin_lock(&walk->mm->page_table_lock);
  349. if (pmd_trans_huge(*pmd)) {
  350. if (pmd_trans_splitting(*pmd)) {
  351. spin_unlock(&walk->mm->page_table_lock);
  352. wait_split_huge_page(vma->anon_vma, pmd);
  353. } else {
  354. smaps_pte_entry(*(pte_t *)pmd, addr,
  355. HPAGE_PMD_SIZE, walk);
  356. spin_unlock(&walk->mm->page_table_lock);
  357. mss->anonymous_thp += HPAGE_PMD_SIZE;
  358. return 0;
  359. }
  360. } else {
  361. spin_unlock(&walk->mm->page_table_lock);
  362. }
  363. /*
  364. * The mmap_sem held all the way back in m_start() is what
  365. * keeps khugepaged out of here and from collapsing things
  366. * in here.
  367. */
  368. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  369. for (; addr != end; pte++, addr += PAGE_SIZE)
  370. smaps_pte_entry(*pte, addr, PAGE_SIZE, walk);
  371. pte_unmap_unlock(pte - 1, ptl);
  372. cond_resched();
  373. return 0;
  374. }
  375. static int show_smap(struct seq_file *m, void *v)
  376. {
  377. struct proc_maps_private *priv = m->private;
  378. struct task_struct *task = priv->task;
  379. struct vm_area_struct *vma = v;
  380. struct mem_size_stats mss;
  381. struct mm_walk smaps_walk = {
  382. .pmd_entry = smaps_pte_range,
  383. .mm = vma->vm_mm,
  384. .private = &mss,
  385. };
  386. memset(&mss, 0, sizeof mss);
  387. mss.vma = vma;
  388. /* mmap_sem is held in m_start */
  389. if (vma->vm_mm && !is_vm_hugetlb_page(vma))
  390. walk_page_range(vma->vm_start, vma->vm_end, &smaps_walk);
  391. show_map_vma(m, vma);
  392. seq_printf(m,
  393. "Size: %8lu kB\n"
  394. "Rss: %8lu kB\n"
  395. "Pss: %8lu kB\n"
  396. "Shared_Clean: %8lu kB\n"
  397. "Shared_Dirty: %8lu kB\n"
  398. "Private_Clean: %8lu kB\n"
  399. "Private_Dirty: %8lu kB\n"
  400. "Referenced: %8lu kB\n"
  401. "Anonymous: %8lu kB\n"
  402. "AnonHugePages: %8lu kB\n"
  403. "Swap: %8lu kB\n"
  404. "KernelPageSize: %8lu kB\n"
  405. "MMUPageSize: %8lu kB\n"
  406. "Locked: %8lu kB\n",
  407. (vma->vm_end - vma->vm_start) >> 10,
  408. mss.resident >> 10,
  409. (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
  410. mss.shared_clean >> 10,
  411. mss.shared_dirty >> 10,
  412. mss.private_clean >> 10,
  413. mss.private_dirty >> 10,
  414. mss.referenced >> 10,
  415. mss.anonymous >> 10,
  416. mss.anonymous_thp >> 10,
  417. mss.swap >> 10,
  418. vma_kernel_pagesize(vma) >> 10,
  419. vma_mmu_pagesize(vma) >> 10,
  420. (vma->vm_flags & VM_LOCKED) ?
  421. (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
  422. if (m->count < m->size) /* vma is copied successfully */
  423. m->version = (vma != get_gate_vma(task->mm))
  424. ? vma->vm_start : 0;
  425. return 0;
  426. }
  427. static const struct seq_operations proc_pid_smaps_op = {
  428. .start = m_start,
  429. .next = m_next,
  430. .stop = m_stop,
  431. .show = show_smap
  432. };
  433. static int smaps_open(struct inode *inode, struct file *file)
  434. {
  435. return do_maps_open(inode, file, &proc_pid_smaps_op);
  436. }
  437. const struct file_operations proc_smaps_operations = {
  438. .open = smaps_open,
  439. .read = seq_read,
  440. .llseek = seq_lseek,
  441. .release = seq_release_private,
  442. };
  443. static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
  444. unsigned long end, struct mm_walk *walk)
  445. {
  446. struct vm_area_struct *vma = walk->private;
  447. pte_t *pte, ptent;
  448. spinlock_t *ptl;
  449. struct page *page;
  450. split_huge_page_pmd(walk->mm, pmd);
  451. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  452. for (; addr != end; pte++, addr += PAGE_SIZE) {
  453. ptent = *pte;
  454. if (!pte_present(ptent))
  455. continue;
  456. page = vm_normal_page(vma, addr, ptent);
  457. if (!page)
  458. continue;
  459. /* Clear accessed and referenced bits. */
  460. ptep_test_and_clear_young(vma, addr, pte);
  461. ClearPageReferenced(page);
  462. }
  463. pte_unmap_unlock(pte - 1, ptl);
  464. cond_resched();
  465. return 0;
  466. }
  467. #define CLEAR_REFS_ALL 1
  468. #define CLEAR_REFS_ANON 2
  469. #define CLEAR_REFS_MAPPED 3
  470. static ssize_t clear_refs_write(struct file *file, const char __user *buf,
  471. size_t count, loff_t *ppos)
  472. {
  473. struct task_struct *task;
  474. char buffer[PROC_NUMBUF];
  475. struct mm_struct *mm;
  476. struct vm_area_struct *vma;
  477. long type;
  478. memset(buffer, 0, sizeof(buffer));
  479. if (count > sizeof(buffer) - 1)
  480. count = sizeof(buffer) - 1;
  481. if (copy_from_user(buffer, buf, count))
  482. return -EFAULT;
  483. if (strict_strtol(strstrip(buffer), 10, &type))
  484. return -EINVAL;
  485. if (type < CLEAR_REFS_ALL || type > CLEAR_REFS_MAPPED)
  486. return -EINVAL;
  487. task = get_proc_task(file->f_path.dentry->d_inode);
  488. if (!task)
  489. return -ESRCH;
  490. mm = get_task_mm(task);
  491. if (mm) {
  492. struct mm_walk clear_refs_walk = {
  493. .pmd_entry = clear_refs_pte_range,
  494. .mm = mm,
  495. };
  496. down_read(&mm->mmap_sem);
  497. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  498. clear_refs_walk.private = vma;
  499. if (is_vm_hugetlb_page(vma))
  500. continue;
  501. /*
  502. * Writing 1 to /proc/pid/clear_refs affects all pages.
  503. *
  504. * Writing 2 to /proc/pid/clear_refs only affects
  505. * Anonymous pages.
  506. *
  507. * Writing 3 to /proc/pid/clear_refs only affects file
  508. * mapped pages.
  509. */
  510. if (type == CLEAR_REFS_ANON && vma->vm_file)
  511. continue;
  512. if (type == CLEAR_REFS_MAPPED && !vma->vm_file)
  513. continue;
  514. walk_page_range(vma->vm_start, vma->vm_end,
  515. &clear_refs_walk);
  516. }
  517. flush_tlb_mm(mm);
  518. up_read(&mm->mmap_sem);
  519. mmput(mm);
  520. }
  521. put_task_struct(task);
  522. return count;
  523. }
  524. const struct file_operations proc_clear_refs_operations = {
  525. .write = clear_refs_write,
  526. .llseek = noop_llseek,
  527. };
  528. struct pagemapread {
  529. int pos, len;
  530. u64 *buffer;
  531. };
  532. #define PM_ENTRY_BYTES sizeof(u64)
  533. #define PM_STATUS_BITS 3
  534. #define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
  535. #define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
  536. #define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
  537. #define PM_PSHIFT_BITS 6
  538. #define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
  539. #define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
  540. #define PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
  541. #define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
  542. #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
  543. #define PM_PRESENT PM_STATUS(4LL)
  544. #define PM_SWAP PM_STATUS(2LL)
  545. #define PM_NOT_PRESENT PM_PSHIFT(PAGE_SHIFT)
  546. #define PM_END_OF_BUFFER 1
  547. static int add_to_pagemap(unsigned long addr, u64 pfn,
  548. struct pagemapread *pm)
  549. {
  550. pm->buffer[pm->pos++] = pfn;
  551. if (pm->pos >= pm->len)
  552. return PM_END_OF_BUFFER;
  553. return 0;
  554. }
  555. static int pagemap_pte_hole(unsigned long start, unsigned long end,
  556. struct mm_walk *walk)
  557. {
  558. struct pagemapread *pm = walk->private;
  559. unsigned long addr;
  560. int err = 0;
  561. for (addr = start; addr < end; addr += PAGE_SIZE) {
  562. err = add_to_pagemap(addr, PM_NOT_PRESENT, pm);
  563. if (err)
  564. break;
  565. }
  566. return err;
  567. }
  568. static u64 swap_pte_to_pagemap_entry(pte_t pte)
  569. {
  570. swp_entry_t e = pte_to_swp_entry(pte);
  571. return swp_type(e) | (swp_offset(e) << MAX_SWAPFILES_SHIFT);
  572. }
  573. static u64 pte_to_pagemap_entry(pte_t pte)
  574. {
  575. u64 pme = 0;
  576. if (is_swap_pte(pte))
  577. pme = PM_PFRAME(swap_pte_to_pagemap_entry(pte))
  578. | PM_PSHIFT(PAGE_SHIFT) | PM_SWAP;
  579. else if (pte_present(pte))
  580. pme = PM_PFRAME(pte_pfn(pte))
  581. | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT;
  582. return pme;
  583. }
  584. static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
  585. struct mm_walk *walk)
  586. {
  587. struct vm_area_struct *vma;
  588. struct pagemapread *pm = walk->private;
  589. pte_t *pte;
  590. int err = 0;
  591. split_huge_page_pmd(walk->mm, pmd);
  592. /* find the first VMA at or above 'addr' */
  593. vma = find_vma(walk->mm, addr);
  594. for (; addr != end; addr += PAGE_SIZE) {
  595. u64 pfn = PM_NOT_PRESENT;
  596. /* check to see if we've left 'vma' behind
  597. * and need a new, higher one */
  598. if (vma && (addr >= vma->vm_end))
  599. vma = find_vma(walk->mm, addr);
  600. /* check that 'vma' actually covers this address,
  601. * and that it isn't a huge page vma */
  602. if (vma && (vma->vm_start <= addr) &&
  603. !is_vm_hugetlb_page(vma)) {
  604. pte = pte_offset_map(pmd, addr);
  605. pfn = pte_to_pagemap_entry(*pte);
  606. /* unmap before userspace copy */
  607. pte_unmap(pte);
  608. }
  609. err = add_to_pagemap(addr, pfn, pm);
  610. if (err)
  611. return err;
  612. }
  613. cond_resched();
  614. return err;
  615. }
  616. #ifdef CONFIG_HUGETLB_PAGE
  617. static u64 huge_pte_to_pagemap_entry(pte_t pte, int offset)
  618. {
  619. u64 pme = 0;
  620. if (pte_present(pte))
  621. pme = PM_PFRAME(pte_pfn(pte) + offset)
  622. | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT;
  623. return pme;
  624. }
  625. /* This function walks within one hugetlb entry in the single call */
  626. static int pagemap_hugetlb_range(pte_t *pte, unsigned long hmask,
  627. unsigned long addr, unsigned long end,
  628. struct mm_walk *walk)
  629. {
  630. struct pagemapread *pm = walk->private;
  631. int err = 0;
  632. u64 pfn;
  633. for (; addr != end; addr += PAGE_SIZE) {
  634. int offset = (addr & ~hmask) >> PAGE_SHIFT;
  635. pfn = huge_pte_to_pagemap_entry(*pte, offset);
  636. err = add_to_pagemap(addr, pfn, pm);
  637. if (err)
  638. return err;
  639. }
  640. cond_resched();
  641. return err;
  642. }
  643. #endif /* HUGETLB_PAGE */
  644. /*
  645. * /proc/pid/pagemap - an array mapping virtual pages to pfns
  646. *
  647. * For each page in the address space, this file contains one 64-bit entry
  648. * consisting of the following:
  649. *
  650. * Bits 0-55 page frame number (PFN) if present
  651. * Bits 0-4 swap type if swapped
  652. * Bits 5-55 swap offset if swapped
  653. * Bits 55-60 page shift (page size = 1<<page shift)
  654. * Bit 61 reserved for future use
  655. * Bit 62 page swapped
  656. * Bit 63 page present
  657. *
  658. * If the page is not present but in swap, then the PFN contains an
  659. * encoding of the swap file number and the page's offset into the
  660. * swap. Unmapped pages return a null PFN. This allows determining
  661. * precisely which pages are mapped (or in swap) and comparing mapped
  662. * pages between processes.
  663. *
  664. * Efficient users of this interface will use /proc/pid/maps to
  665. * determine which areas of memory are actually mapped and llseek to
  666. * skip over unmapped regions.
  667. */
  668. #define PAGEMAP_WALK_SIZE (PMD_SIZE)
  669. #define PAGEMAP_WALK_MASK (PMD_MASK)
  670. static ssize_t pagemap_read(struct file *file, char __user *buf,
  671. size_t count, loff_t *ppos)
  672. {
  673. struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
  674. struct mm_struct *mm;
  675. struct pagemapread pm;
  676. int ret = -ESRCH;
  677. struct mm_walk pagemap_walk = {};
  678. unsigned long src;
  679. unsigned long svpfn;
  680. unsigned long start_vaddr;
  681. unsigned long end_vaddr;
  682. int copied = 0;
  683. if (!task)
  684. goto out;
  685. mm = mm_for_maps(task);
  686. ret = PTR_ERR(mm);
  687. if (!mm || IS_ERR(mm))
  688. goto out_task;
  689. ret = -EINVAL;
  690. /* file position must be aligned */
  691. if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
  692. goto out_task;
  693. ret = 0;
  694. if (!count)
  695. goto out_task;
  696. pm.len = PM_ENTRY_BYTES * (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
  697. pm.buffer = kmalloc(pm.len, GFP_TEMPORARY);
  698. ret = -ENOMEM;
  699. if (!pm.buffer)
  700. goto out_mm;
  701. pagemap_walk.pmd_entry = pagemap_pte_range;
  702. pagemap_walk.pte_hole = pagemap_pte_hole;
  703. #ifdef CONFIG_HUGETLB_PAGE
  704. pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
  705. #endif
  706. pagemap_walk.mm = mm;
  707. pagemap_walk.private = &pm;
  708. src = *ppos;
  709. svpfn = src / PM_ENTRY_BYTES;
  710. start_vaddr = svpfn << PAGE_SHIFT;
  711. end_vaddr = TASK_SIZE_OF(task);
  712. /* watch out for wraparound */
  713. if (svpfn > TASK_SIZE_OF(task) >> PAGE_SHIFT)
  714. start_vaddr = end_vaddr;
  715. /*
  716. * The odds are that this will stop walking way
  717. * before end_vaddr, because the length of the
  718. * user buffer is tracked in "pm", and the walk
  719. * will stop when we hit the end of the buffer.
  720. */
  721. ret = 0;
  722. while (count && (start_vaddr < end_vaddr)) {
  723. int len;
  724. unsigned long end;
  725. pm.pos = 0;
  726. end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
  727. /* overflow ? */
  728. if (end < start_vaddr || end > end_vaddr)
  729. end = end_vaddr;
  730. down_read(&mm->mmap_sem);
  731. ret = walk_page_range(start_vaddr, end, &pagemap_walk);
  732. up_read(&mm->mmap_sem);
  733. start_vaddr = end;
  734. len = min(count, PM_ENTRY_BYTES * pm.pos);
  735. if (copy_to_user(buf, pm.buffer, len)) {
  736. ret = -EFAULT;
  737. goto out_free;
  738. }
  739. copied += len;
  740. buf += len;
  741. count -= len;
  742. }
  743. *ppos += copied;
  744. if (!ret || ret == PM_END_OF_BUFFER)
  745. ret = copied;
  746. out_free:
  747. kfree(pm.buffer);
  748. out_mm:
  749. mmput(mm);
  750. out_task:
  751. put_task_struct(task);
  752. out:
  753. return ret;
  754. }
  755. const struct file_operations proc_pagemap_operations = {
  756. .llseek = mem_lseek, /* borrow this */
  757. .read = pagemap_read,
  758. };
  759. #endif /* CONFIG_PROC_PAGE_MONITOR */
  760. #ifdef CONFIG_NUMA
  761. extern int show_numa_map(struct seq_file *m, void *v);
  762. static const struct seq_operations proc_pid_numa_maps_op = {
  763. .start = m_start,
  764. .next = m_next,
  765. .stop = m_stop,
  766. .show = show_numa_map,
  767. };
  768. static int numa_maps_open(struct inode *inode, struct file *file)
  769. {
  770. return do_maps_open(inode, file, &proc_pid_numa_maps_op);
  771. }
  772. const struct file_operations proc_numa_maps_operations = {
  773. .open = numa_maps_open,
  774. .read = seq_read,
  775. .llseek = seq_lseek,
  776. .release = seq_release_private,
  777. };
  778. #endif