task_mmu.c 18 KB

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