task_mmu.c 19 KB

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