task_mmu.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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. vm_flags_t 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. int type;
  478. int rv;
  479. memset(buffer, 0, sizeof(buffer));
  480. if (count > sizeof(buffer) - 1)
  481. count = sizeof(buffer) - 1;
  482. if (copy_from_user(buffer, buf, count))
  483. return -EFAULT;
  484. rv = kstrtoint(strstrip(buffer), 10, &type);
  485. if (rv < 0)
  486. return rv;
  487. if (type < CLEAR_REFS_ALL || type > CLEAR_REFS_MAPPED)
  488. return -EINVAL;
  489. task = get_proc_task(file->f_path.dentry->d_inode);
  490. if (!task)
  491. return -ESRCH;
  492. mm = get_task_mm(task);
  493. if (mm) {
  494. struct mm_walk clear_refs_walk = {
  495. .pmd_entry = clear_refs_pte_range,
  496. .mm = mm,
  497. };
  498. down_read(&mm->mmap_sem);
  499. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  500. clear_refs_walk.private = vma;
  501. if (is_vm_hugetlb_page(vma))
  502. continue;
  503. /*
  504. * Writing 1 to /proc/pid/clear_refs affects all pages.
  505. *
  506. * Writing 2 to /proc/pid/clear_refs only affects
  507. * Anonymous pages.
  508. *
  509. * Writing 3 to /proc/pid/clear_refs only affects file
  510. * mapped pages.
  511. */
  512. if (type == CLEAR_REFS_ANON && vma->vm_file)
  513. continue;
  514. if (type == CLEAR_REFS_MAPPED && !vma->vm_file)
  515. continue;
  516. walk_page_range(vma->vm_start, vma->vm_end,
  517. &clear_refs_walk);
  518. }
  519. flush_tlb_mm(mm);
  520. up_read(&mm->mmap_sem);
  521. mmput(mm);
  522. }
  523. put_task_struct(task);
  524. return count;
  525. }
  526. const struct file_operations proc_clear_refs_operations = {
  527. .write = clear_refs_write,
  528. .llseek = noop_llseek,
  529. };
  530. struct pagemapread {
  531. int pos, len;
  532. u64 *buffer;
  533. };
  534. #define PM_ENTRY_BYTES sizeof(u64)
  535. #define PM_STATUS_BITS 3
  536. #define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
  537. #define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
  538. #define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
  539. #define PM_PSHIFT_BITS 6
  540. #define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
  541. #define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
  542. #define PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
  543. #define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
  544. #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
  545. #define PM_PRESENT PM_STATUS(4LL)
  546. #define PM_SWAP PM_STATUS(2LL)
  547. #define PM_NOT_PRESENT PM_PSHIFT(PAGE_SHIFT)
  548. #define PM_END_OF_BUFFER 1
  549. static int add_to_pagemap(unsigned long addr, u64 pfn,
  550. struct pagemapread *pm)
  551. {
  552. pm->buffer[pm->pos++] = pfn;
  553. if (pm->pos >= pm->len)
  554. return PM_END_OF_BUFFER;
  555. return 0;
  556. }
  557. static int pagemap_pte_hole(unsigned long start, unsigned long end,
  558. struct mm_walk *walk)
  559. {
  560. struct pagemapread *pm = walk->private;
  561. unsigned long addr;
  562. int err = 0;
  563. for (addr = start; addr < end; addr += PAGE_SIZE) {
  564. err = add_to_pagemap(addr, PM_NOT_PRESENT, pm);
  565. if (err)
  566. break;
  567. }
  568. return err;
  569. }
  570. static u64 swap_pte_to_pagemap_entry(pte_t pte)
  571. {
  572. swp_entry_t e = pte_to_swp_entry(pte);
  573. return swp_type(e) | (swp_offset(e) << MAX_SWAPFILES_SHIFT);
  574. }
  575. static u64 pte_to_pagemap_entry(pte_t pte)
  576. {
  577. u64 pme = 0;
  578. if (is_swap_pte(pte))
  579. pme = PM_PFRAME(swap_pte_to_pagemap_entry(pte))
  580. | PM_PSHIFT(PAGE_SHIFT) | PM_SWAP;
  581. else if (pte_present(pte))
  582. pme = PM_PFRAME(pte_pfn(pte))
  583. | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT;
  584. return pme;
  585. }
  586. static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
  587. struct mm_walk *walk)
  588. {
  589. struct vm_area_struct *vma;
  590. struct pagemapread *pm = walk->private;
  591. pte_t *pte;
  592. int err = 0;
  593. split_huge_page_pmd(walk->mm, pmd);
  594. /* find the first VMA at or above 'addr' */
  595. vma = find_vma(walk->mm, addr);
  596. for (; addr != end; addr += PAGE_SIZE) {
  597. u64 pfn = PM_NOT_PRESENT;
  598. /* check to see if we've left 'vma' behind
  599. * and need a new, higher one */
  600. if (vma && (addr >= vma->vm_end))
  601. vma = find_vma(walk->mm, addr);
  602. /* check that 'vma' actually covers this address,
  603. * and that it isn't a huge page vma */
  604. if (vma && (vma->vm_start <= addr) &&
  605. !is_vm_hugetlb_page(vma)) {
  606. pte = pte_offset_map(pmd, addr);
  607. pfn = pte_to_pagemap_entry(*pte);
  608. /* unmap before userspace copy */
  609. pte_unmap(pte);
  610. }
  611. err = add_to_pagemap(addr, pfn, pm);
  612. if (err)
  613. return err;
  614. }
  615. cond_resched();
  616. return err;
  617. }
  618. #ifdef CONFIG_HUGETLB_PAGE
  619. static u64 huge_pte_to_pagemap_entry(pte_t pte, int offset)
  620. {
  621. u64 pme = 0;
  622. if (pte_present(pte))
  623. pme = PM_PFRAME(pte_pfn(pte) + offset)
  624. | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT;
  625. return pme;
  626. }
  627. /* This function walks within one hugetlb entry in the single call */
  628. static int pagemap_hugetlb_range(pte_t *pte, unsigned long hmask,
  629. unsigned long addr, unsigned long end,
  630. struct mm_walk *walk)
  631. {
  632. struct pagemapread *pm = walk->private;
  633. int err = 0;
  634. u64 pfn;
  635. for (; addr != end; addr += PAGE_SIZE) {
  636. int offset = (addr & ~hmask) >> PAGE_SHIFT;
  637. pfn = huge_pte_to_pagemap_entry(*pte, offset);
  638. err = add_to_pagemap(addr, pfn, pm);
  639. if (err)
  640. return err;
  641. }
  642. cond_resched();
  643. return err;
  644. }
  645. #endif /* HUGETLB_PAGE */
  646. /*
  647. * /proc/pid/pagemap - an array mapping virtual pages to pfns
  648. *
  649. * For each page in the address space, this file contains one 64-bit entry
  650. * consisting of the following:
  651. *
  652. * Bits 0-55 page frame number (PFN) if present
  653. * Bits 0-4 swap type if swapped
  654. * Bits 5-55 swap offset if swapped
  655. * Bits 55-60 page shift (page size = 1<<page shift)
  656. * Bit 61 reserved for future use
  657. * Bit 62 page swapped
  658. * Bit 63 page present
  659. *
  660. * If the page is not present but in swap, then the PFN contains an
  661. * encoding of the swap file number and the page's offset into the
  662. * swap. Unmapped pages return a null PFN. This allows determining
  663. * precisely which pages are mapped (or in swap) and comparing mapped
  664. * pages between processes.
  665. *
  666. * Efficient users of this interface will use /proc/pid/maps to
  667. * determine which areas of memory are actually mapped and llseek to
  668. * skip over unmapped regions.
  669. */
  670. #define PAGEMAP_WALK_SIZE (PMD_SIZE)
  671. #define PAGEMAP_WALK_MASK (PMD_MASK)
  672. static ssize_t pagemap_read(struct file *file, char __user *buf,
  673. size_t count, loff_t *ppos)
  674. {
  675. struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
  676. struct mm_struct *mm;
  677. struct pagemapread pm;
  678. int ret = -ESRCH;
  679. struct mm_walk pagemap_walk = {};
  680. unsigned long src;
  681. unsigned long svpfn;
  682. unsigned long start_vaddr;
  683. unsigned long end_vaddr;
  684. int copied = 0;
  685. if (!task)
  686. goto out;
  687. ret = -EINVAL;
  688. /* file position must be aligned */
  689. if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
  690. goto out_task;
  691. ret = 0;
  692. if (!count)
  693. goto out_task;
  694. pm.len = PM_ENTRY_BYTES * (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
  695. pm.buffer = kmalloc(pm.len, GFP_TEMPORARY);
  696. ret = -ENOMEM;
  697. if (!pm.buffer)
  698. goto out_task;
  699. mm = mm_for_maps(task);
  700. ret = PTR_ERR(mm);
  701. if (!mm || IS_ERR(mm))
  702. goto out_free;
  703. pagemap_walk.pmd_entry = pagemap_pte_range;
  704. pagemap_walk.pte_hole = pagemap_pte_hole;
  705. #ifdef CONFIG_HUGETLB_PAGE
  706. pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
  707. #endif
  708. pagemap_walk.mm = mm;
  709. pagemap_walk.private = &pm;
  710. src = *ppos;
  711. svpfn = src / PM_ENTRY_BYTES;
  712. start_vaddr = svpfn << PAGE_SHIFT;
  713. end_vaddr = TASK_SIZE_OF(task);
  714. /* watch out for wraparound */
  715. if (svpfn > TASK_SIZE_OF(task) >> PAGE_SHIFT)
  716. start_vaddr = end_vaddr;
  717. /*
  718. * The odds are that this will stop walking way
  719. * before end_vaddr, because the length of the
  720. * user buffer is tracked in "pm", and the walk
  721. * will stop when we hit the end of the buffer.
  722. */
  723. ret = 0;
  724. while (count && (start_vaddr < end_vaddr)) {
  725. int len;
  726. unsigned long end;
  727. pm.pos = 0;
  728. end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
  729. /* overflow ? */
  730. if (end < start_vaddr || end > end_vaddr)
  731. end = end_vaddr;
  732. down_read(&mm->mmap_sem);
  733. ret = walk_page_range(start_vaddr, end, &pagemap_walk);
  734. up_read(&mm->mmap_sem);
  735. start_vaddr = end;
  736. len = min(count, PM_ENTRY_BYTES * pm.pos);
  737. if (copy_to_user(buf, pm.buffer, len)) {
  738. ret = -EFAULT;
  739. goto out_mm;
  740. }
  741. copied += len;
  742. buf += len;
  743. count -= len;
  744. }
  745. *ppos += copied;
  746. if (!ret || ret == PM_END_OF_BUFFER)
  747. ret = copied;
  748. out_mm:
  749. mmput(mm);
  750. out_free:
  751. kfree(pm.buffer);
  752. out_task:
  753. put_task_struct(task);
  754. out:
  755. return ret;
  756. }
  757. const struct file_operations proc_pagemap_operations = {
  758. .llseek = mem_lseek, /* borrow this */
  759. .read = pagemap_read,
  760. };
  761. #endif /* CONFIG_PROC_PAGE_MONITOR */
  762. #ifdef CONFIG_NUMA
  763. struct numa_maps {
  764. struct vm_area_struct *vma;
  765. unsigned long pages;
  766. unsigned long anon;
  767. unsigned long active;
  768. unsigned long writeback;
  769. unsigned long mapcount_max;
  770. unsigned long dirty;
  771. unsigned long swapcache;
  772. unsigned long node[MAX_NUMNODES];
  773. };
  774. struct numa_maps_private {
  775. struct proc_maps_private proc_maps;
  776. struct numa_maps md;
  777. };
  778. static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty)
  779. {
  780. int count = page_mapcount(page);
  781. md->pages++;
  782. if (pte_dirty || PageDirty(page))
  783. md->dirty++;
  784. if (PageSwapCache(page))
  785. md->swapcache++;
  786. if (PageActive(page) || PageUnevictable(page))
  787. md->active++;
  788. if (PageWriteback(page))
  789. md->writeback++;
  790. if (PageAnon(page))
  791. md->anon++;
  792. if (count > md->mapcount_max)
  793. md->mapcount_max = count;
  794. md->node[page_to_nid(page)]++;
  795. }
  796. static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
  797. unsigned long end, struct mm_walk *walk)
  798. {
  799. struct numa_maps *md;
  800. spinlock_t *ptl;
  801. pte_t *orig_pte;
  802. pte_t *pte;
  803. md = walk->private;
  804. orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
  805. do {
  806. struct page *page;
  807. int nid;
  808. if (!pte_present(*pte))
  809. continue;
  810. page = vm_normal_page(md->vma, addr, *pte);
  811. if (!page)
  812. continue;
  813. if (PageReserved(page))
  814. continue;
  815. nid = page_to_nid(page);
  816. if (!node_isset(nid, node_states[N_HIGH_MEMORY]))
  817. continue;
  818. gather_stats(page, md, pte_dirty(*pte));
  819. } while (pte++, addr += PAGE_SIZE, addr != end);
  820. pte_unmap_unlock(orig_pte, ptl);
  821. return 0;
  822. }
  823. #ifdef CONFIG_HUGETLB_PAGE
  824. static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
  825. unsigned long addr, unsigned long end, struct mm_walk *walk)
  826. {
  827. struct numa_maps *md;
  828. struct page *page;
  829. if (pte_none(*pte))
  830. return 0;
  831. page = pte_page(*pte);
  832. if (!page)
  833. return 0;
  834. md = walk->private;
  835. gather_stats(page, md, pte_dirty(*pte));
  836. return 0;
  837. }
  838. #else
  839. static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
  840. unsigned long addr, unsigned long end, struct mm_walk *walk)
  841. {
  842. return 0;
  843. }
  844. #endif
  845. /*
  846. * Display pages allocated per node and memory policy via /proc.
  847. */
  848. static int show_numa_map(struct seq_file *m, void *v)
  849. {
  850. struct numa_maps_private *numa_priv = m->private;
  851. struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
  852. struct vm_area_struct *vma = v;
  853. struct numa_maps *md = &numa_priv->md;
  854. struct file *file = vma->vm_file;
  855. struct mm_struct *mm = vma->vm_mm;
  856. struct mm_walk walk = {};
  857. struct mempolicy *pol;
  858. int n;
  859. char buffer[50];
  860. if (!mm)
  861. return 0;
  862. /* Ensure we start with an empty set of numa_maps statistics. */
  863. memset(md, 0, sizeof(*md));
  864. md->vma = vma;
  865. walk.hugetlb_entry = gather_hugetbl_stats;
  866. walk.pmd_entry = gather_pte_stats;
  867. walk.private = md;
  868. walk.mm = mm;
  869. pol = get_vma_policy(proc_priv->task, vma, vma->vm_start);
  870. mpol_to_str(buffer, sizeof(buffer), pol, 0);
  871. mpol_cond_put(pol);
  872. seq_printf(m, "%08lx %s", vma->vm_start, buffer);
  873. if (file) {
  874. seq_printf(m, " file=");
  875. seq_path(m, &file->f_path, "\n\t= ");
  876. } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
  877. seq_printf(m, " heap");
  878. } else if (vma->vm_start <= mm->start_stack &&
  879. vma->vm_end >= mm->start_stack) {
  880. seq_printf(m, " stack");
  881. }
  882. walk_page_range(vma->vm_start, vma->vm_end, &walk);
  883. if (!md->pages)
  884. goto out;
  885. if (md->anon)
  886. seq_printf(m, " anon=%lu", md->anon);
  887. if (md->dirty)
  888. seq_printf(m, " dirty=%lu", md->dirty);
  889. if (md->pages != md->anon && md->pages != md->dirty)
  890. seq_printf(m, " mapped=%lu", md->pages);
  891. if (md->mapcount_max > 1)
  892. seq_printf(m, " mapmax=%lu", md->mapcount_max);
  893. if (md->swapcache)
  894. seq_printf(m, " swapcache=%lu", md->swapcache);
  895. if (md->active < md->pages && !is_vm_hugetlb_page(vma))
  896. seq_printf(m, " active=%lu", md->active);
  897. if (md->writeback)
  898. seq_printf(m, " writeback=%lu", md->writeback);
  899. for_each_node_state(n, N_HIGH_MEMORY)
  900. if (md->node[n])
  901. seq_printf(m, " N%d=%lu", n, md->node[n]);
  902. out:
  903. seq_putc(m, '\n');
  904. if (m->count < m->size)
  905. m->version = (vma != proc_priv->tail_vma) ? vma->vm_start : 0;
  906. return 0;
  907. }
  908. static const struct seq_operations proc_pid_numa_maps_op = {
  909. .start = m_start,
  910. .next = m_next,
  911. .stop = m_stop,
  912. .show = show_numa_map,
  913. };
  914. static int numa_maps_open(struct inode *inode, struct file *file)
  915. {
  916. struct numa_maps_private *priv;
  917. int ret = -ENOMEM;
  918. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  919. if (priv) {
  920. priv->proc_maps.pid = proc_pid(inode);
  921. ret = seq_open(file, &proc_pid_numa_maps_op);
  922. if (!ret) {
  923. struct seq_file *m = file->private_data;
  924. m->private = priv;
  925. } else {
  926. kfree(priv);
  927. }
  928. }
  929. return ret;
  930. }
  931. const struct file_operations proc_numa_maps_operations = {
  932. .open = numa_maps_open,
  933. .read = seq_read,
  934. .llseek = seq_lseek,
  935. .release = seq_release_private,
  936. };
  937. #endif /* CONFIG_NUMA */