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. 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. {
  302. struct vm_area_struct *vma;
  303. unsigned long resident;
  304. unsigned long shared_clean;
  305. unsigned long shared_dirty;
  306. unsigned long private_clean;
  307. unsigned long private_dirty;
  308. unsigned long referenced;
  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 (!pte_present(ptent))
  324. continue;
  325. mss->resident += PAGE_SIZE;
  326. page = vm_normal_page(vma, addr, ptent);
  327. if (!page)
  328. continue;
  329. /* Accumulate the size in pages that have been accessed. */
  330. if (pte_young(ptent) || PageReferenced(page))
  331. mss->referenced += PAGE_SIZE;
  332. mapcount = page_mapcount(page);
  333. if (mapcount >= 2) {
  334. if (pte_dirty(ptent))
  335. mss->shared_dirty += PAGE_SIZE;
  336. else
  337. mss->shared_clean += PAGE_SIZE;
  338. mss->pss += (PAGE_SIZE << PSS_SHIFT) / mapcount;
  339. } else {
  340. if (pte_dirty(ptent))
  341. mss->private_dirty += PAGE_SIZE;
  342. else
  343. mss->private_clean += PAGE_SIZE;
  344. mss->pss += (PAGE_SIZE << PSS_SHIFT);
  345. }
  346. }
  347. pte_unmap_unlock(pte - 1, ptl);
  348. cond_resched();
  349. return 0;
  350. }
  351. static struct mm_walk smaps_walk = { .pmd_entry = smaps_pte_range };
  352. static int show_smap(struct seq_file *m, void *v)
  353. {
  354. struct vm_area_struct *vma = v;
  355. struct mem_size_stats mss;
  356. int ret;
  357. memset(&mss, 0, sizeof mss);
  358. mss.vma = vma;
  359. if (vma->vm_mm && !is_vm_hugetlb_page(vma))
  360. walk_page_range(vma->vm_mm, vma->vm_start, vma->vm_end,
  361. &smaps_walk, &mss);
  362. ret = show_map(m, v);
  363. if (ret)
  364. return ret;
  365. seq_printf(m,
  366. "Size: %8lu kB\n"
  367. "Rss: %8lu kB\n"
  368. "Pss: %8lu kB\n"
  369. "Shared_Clean: %8lu kB\n"
  370. "Shared_Dirty: %8lu kB\n"
  371. "Private_Clean: %8lu kB\n"
  372. "Private_Dirty: %8lu kB\n"
  373. "Referenced: %8lu kB\n",
  374. (vma->vm_end - vma->vm_start) >> 10,
  375. mss.resident >> 10,
  376. (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
  377. mss.shared_clean >> 10,
  378. mss.shared_dirty >> 10,
  379. mss.private_clean >> 10,
  380. mss.private_dirty >> 10,
  381. mss.referenced >> 10);
  382. return ret;
  383. }
  384. static const struct seq_operations proc_pid_smaps_op = {
  385. .start = m_start,
  386. .next = m_next,
  387. .stop = m_stop,
  388. .show = show_smap
  389. };
  390. static int smaps_open(struct inode *inode, struct file *file)
  391. {
  392. return do_maps_open(inode, file, &proc_pid_smaps_op);
  393. }
  394. const struct file_operations proc_smaps_operations = {
  395. .open = smaps_open,
  396. .read = seq_read,
  397. .llseek = seq_lseek,
  398. .release = seq_release_private,
  399. };
  400. static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
  401. unsigned long end, void *private)
  402. {
  403. struct vm_area_struct *vma = private;
  404. pte_t *pte, ptent;
  405. spinlock_t *ptl;
  406. struct page *page;
  407. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  408. for (; addr != end; pte++, addr += PAGE_SIZE) {
  409. ptent = *pte;
  410. if (!pte_present(ptent))
  411. continue;
  412. page = vm_normal_page(vma, addr, ptent);
  413. if (!page)
  414. continue;
  415. /* Clear accessed and referenced bits. */
  416. ptep_test_and_clear_young(vma, addr, pte);
  417. ClearPageReferenced(page);
  418. }
  419. pte_unmap_unlock(pte - 1, ptl);
  420. cond_resched();
  421. return 0;
  422. }
  423. static struct mm_walk clear_refs_walk = { .pmd_entry = clear_refs_pte_range };
  424. static ssize_t clear_refs_write(struct file *file, const char __user *buf,
  425. size_t count, loff_t *ppos)
  426. {
  427. struct task_struct *task;
  428. char buffer[PROC_NUMBUF], *end;
  429. struct mm_struct *mm;
  430. struct vm_area_struct *vma;
  431. memset(buffer, 0, sizeof(buffer));
  432. if (count > sizeof(buffer) - 1)
  433. count = sizeof(buffer) - 1;
  434. if (copy_from_user(buffer, buf, count))
  435. return -EFAULT;
  436. if (!simple_strtol(buffer, &end, 0))
  437. return -EINVAL;
  438. if (*end == '\n')
  439. end++;
  440. task = get_proc_task(file->f_path.dentry->d_inode);
  441. if (!task)
  442. return -ESRCH;
  443. mm = get_task_mm(task);
  444. if (mm) {
  445. down_read(&mm->mmap_sem);
  446. for (vma = mm->mmap; vma; vma = vma->vm_next)
  447. if (!is_vm_hugetlb_page(vma))
  448. walk_page_range(mm, vma->vm_start, vma->vm_end,
  449. &clear_refs_walk, vma);
  450. flush_tlb_mm(mm);
  451. up_read(&mm->mmap_sem);
  452. mmput(mm);
  453. }
  454. put_task_struct(task);
  455. if (end - buffer == 0)
  456. return -EIO;
  457. return end - buffer;
  458. }
  459. const struct file_operations proc_clear_refs_operations = {
  460. .write = clear_refs_write,
  461. };
  462. struct pagemapread {
  463. char __user *out, *end;
  464. };
  465. #define PM_ENTRY_BYTES sizeof(u64)
  466. #define PM_RESERVED_BITS 3
  467. #define PM_RESERVED_OFFSET (64 - PM_RESERVED_BITS)
  468. #define PM_RESERVED_MASK (((1LL<<PM_RESERVED_BITS)-1) << PM_RESERVED_OFFSET)
  469. #define PM_SPECIAL(nr) (((nr) << PM_RESERVED_OFFSET) & PM_RESERVED_MASK)
  470. #define PM_NOT_PRESENT PM_SPECIAL(1LL)
  471. #define PM_SWAP PM_SPECIAL(2LL)
  472. #define PM_END_OF_BUFFER 1
  473. static int add_to_pagemap(unsigned long addr, u64 pfn,
  474. struct pagemapread *pm)
  475. {
  476. /*
  477. * Make sure there's room in the buffer for an
  478. * entire entry. Otherwise, only copy part of
  479. * the pfn.
  480. */
  481. if (pm->out + PM_ENTRY_BYTES >= pm->end) {
  482. if (copy_to_user(pm->out, &pfn, pm->end - pm->out))
  483. return -EFAULT;
  484. pm->out = pm->end;
  485. return PM_END_OF_BUFFER;
  486. }
  487. if (put_user(pfn, pm->out))
  488. return -EFAULT;
  489. pm->out += PM_ENTRY_BYTES;
  490. return 0;
  491. }
  492. static int pagemap_pte_hole(unsigned long start, unsigned long end,
  493. void *private)
  494. {
  495. struct pagemapread *pm = private;
  496. unsigned long addr;
  497. int err = 0;
  498. for (addr = start; addr < end; addr += PAGE_SIZE) {
  499. err = add_to_pagemap(addr, PM_NOT_PRESENT, pm);
  500. if (err)
  501. break;
  502. }
  503. return err;
  504. }
  505. u64 swap_pte_to_pagemap_entry(pte_t pte)
  506. {
  507. swp_entry_t e = pte_to_swp_entry(pte);
  508. return PM_SWAP | swp_type(e) | (swp_offset(e) << MAX_SWAPFILES_SHIFT);
  509. }
  510. static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
  511. void *private)
  512. {
  513. struct pagemapread *pm = private;
  514. pte_t *pte;
  515. int err = 0;
  516. for (; addr != end; addr += PAGE_SIZE) {
  517. u64 pfn = PM_NOT_PRESENT;
  518. pte = pte_offset_map(pmd, addr);
  519. if (is_swap_pte(*pte))
  520. pfn = swap_pte_to_pagemap_entry(*pte);
  521. else if (pte_present(*pte))
  522. pfn = pte_pfn(*pte);
  523. /* unmap so we're not in atomic when we copy to userspace */
  524. pte_unmap(pte);
  525. err = add_to_pagemap(addr, pfn, pm);
  526. if (err)
  527. return err;
  528. }
  529. cond_resched();
  530. return err;
  531. }
  532. static struct mm_walk pagemap_walk = {
  533. .pmd_entry = pagemap_pte_range,
  534. .pte_hole = pagemap_pte_hole
  535. };
  536. /*
  537. * /proc/pid/pagemap - an array mapping virtual pages to pfns
  538. *
  539. * For each page in the address space, this file contains one 64-bit
  540. * entry representing the corresponding physical page frame number
  541. * (PFN) if the page is present. If there is a swap entry for the
  542. * physical page, then an encoding of the swap file number and the
  543. * page's offset into the swap file are returned. If no page is
  544. * present at all, PM_NOT_PRESENT is returned. This allows determining
  545. * precisely which pages are mapped (or in swap) and comparing mapped
  546. * pages between processes.
  547. *
  548. * Efficient users of this interface will use /proc/pid/maps to
  549. * determine which areas of memory are actually mapped and llseek to
  550. * skip over unmapped regions.
  551. */
  552. static ssize_t pagemap_read(struct file *file, char __user *buf,
  553. size_t count, loff_t *ppos)
  554. {
  555. struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
  556. struct page **pages, *page;
  557. unsigned long uaddr, uend;
  558. struct mm_struct *mm;
  559. struct pagemapread pm;
  560. int pagecount;
  561. int ret = -ESRCH;
  562. if (!task)
  563. goto out;
  564. ret = -EACCES;
  565. if (!ptrace_may_attach(task))
  566. goto out;
  567. ret = -EINVAL;
  568. /* file position must be aligned */
  569. if (*ppos % PM_ENTRY_BYTES)
  570. goto out;
  571. ret = 0;
  572. mm = get_task_mm(task);
  573. if (!mm)
  574. goto out;
  575. ret = -ENOMEM;
  576. uaddr = (unsigned long)buf & PAGE_MASK;
  577. uend = (unsigned long)(buf + count);
  578. pagecount = (PAGE_ALIGN(uend) - uaddr) / PAGE_SIZE;
  579. pages = kmalloc(pagecount * sizeof(struct page *), GFP_KERNEL);
  580. if (!pages)
  581. goto out_task;
  582. down_read(&current->mm->mmap_sem);
  583. ret = get_user_pages(current, current->mm, uaddr, pagecount,
  584. 1, 0, pages, NULL);
  585. up_read(&current->mm->mmap_sem);
  586. if (ret < 0)
  587. goto out_free;
  588. pm.out = buf;
  589. pm.end = buf + count;
  590. if (!ptrace_may_attach(task)) {
  591. ret = -EIO;
  592. } else {
  593. unsigned long src = *ppos;
  594. unsigned long svpfn = src / PM_ENTRY_BYTES;
  595. unsigned long start_vaddr = svpfn << PAGE_SHIFT;
  596. unsigned long end_vaddr = TASK_SIZE_OF(task);
  597. /* watch out for wraparound */
  598. if (svpfn > TASK_SIZE_OF(task) >> PAGE_SHIFT)
  599. start_vaddr = end_vaddr;
  600. /*
  601. * The odds are that this will stop walking way
  602. * before end_vaddr, because the length of the
  603. * user buffer is tracked in "pm", and the walk
  604. * will stop when we hit the end of the buffer.
  605. */
  606. ret = walk_page_range(mm, start_vaddr, end_vaddr,
  607. &pagemap_walk, &pm);
  608. if (ret == PM_END_OF_BUFFER)
  609. ret = 0;
  610. /* don't need mmap_sem for these, but this looks cleaner */
  611. *ppos += pm.out - buf;
  612. if (!ret)
  613. ret = pm.out - buf;
  614. }
  615. for (; pagecount; pagecount--) {
  616. page = pages[pagecount-1];
  617. if (!PageReserved(page))
  618. SetPageDirty(page);
  619. page_cache_release(page);
  620. }
  621. mmput(mm);
  622. out_free:
  623. kfree(pages);
  624. out_task:
  625. put_task_struct(task);
  626. out:
  627. return ret;
  628. }
  629. const struct file_operations proc_pagemap_operations = {
  630. .llseek = mem_lseek, /* borrow this */
  631. .read = pagemap_read,
  632. };
  633. #endif /* CONFIG_PROC_PAGE_MONITOR */
  634. #ifdef CONFIG_NUMA
  635. extern int show_numa_map(struct seq_file *m, void *v);
  636. static int show_numa_map_checked(struct seq_file *m, void *v)
  637. {
  638. struct proc_maps_private *priv = m->private;
  639. struct task_struct *task = priv->task;
  640. if (maps_protect && !ptrace_may_attach(task))
  641. return -EACCES;
  642. return show_numa_map(m, v);
  643. }
  644. static const struct seq_operations proc_pid_numa_maps_op = {
  645. .start = m_start,
  646. .next = m_next,
  647. .stop = m_stop,
  648. .show = show_numa_map_checked
  649. };
  650. static int numa_maps_open(struct inode *inode, struct file *file)
  651. {
  652. return do_maps_open(inode, file, &proc_pid_numa_maps_op);
  653. }
  654. const struct file_operations proc_numa_maps_operations = {
  655. .open = numa_maps_open,
  656. .read = seq_read,
  657. .llseek = seq_lseek,
  658. .release = seq_release_private,
  659. };
  660. #endif