task_mmu.c 18 KB

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