task_mmu.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  1. #include <linux/mm.h>
  2. #include <linux/hugetlb.h>
  3. #include <linux/huge_mm.h>
  4. #include <linux/mount.h>
  5. #include <linux/seq_file.h>
  6. #include <linux/highmem.h>
  7. #include <linux/ptrace.h>
  8. #include <linux/slab.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/mempolicy.h>
  11. #include <linux/rmap.h>
  12. #include <linux/swap.h>
  13. #include <linux/swapops.h>
  14. #include <asm/elf.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/tlbflush.h>
  17. #include "internal.h"
  18. void task_mem(struct seq_file *m, struct mm_struct *mm)
  19. {
  20. unsigned long data, text, lib, swap;
  21. unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
  22. /*
  23. * Note: to minimize their overhead, mm maintains hiwater_vm and
  24. * hiwater_rss only when about to *lower* total_vm or rss. Any
  25. * collector of these hiwater stats must therefore get total_vm
  26. * and rss too, which will usually be the higher. Barriers? not
  27. * worth the effort, such snapshots can always be inconsistent.
  28. */
  29. hiwater_vm = total_vm = mm->total_vm;
  30. if (hiwater_vm < mm->hiwater_vm)
  31. hiwater_vm = mm->hiwater_vm;
  32. hiwater_rss = total_rss = get_mm_rss(mm);
  33. if (hiwater_rss < mm->hiwater_rss)
  34. hiwater_rss = mm->hiwater_rss;
  35. data = mm->total_vm - mm->shared_vm - mm->stack_vm;
  36. text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
  37. lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
  38. swap = get_mm_counter(mm, MM_SWAPENTS);
  39. seq_printf(m,
  40. "VmPeak:\t%8lu kB\n"
  41. "VmSize:\t%8lu kB\n"
  42. "VmLck:\t%8lu kB\n"
  43. "VmPin:\t%8lu kB\n"
  44. "VmHWM:\t%8lu kB\n"
  45. "VmRSS:\t%8lu kB\n"
  46. "VmData:\t%8lu kB\n"
  47. "VmStk:\t%8lu kB\n"
  48. "VmExe:\t%8lu kB\n"
  49. "VmLib:\t%8lu kB\n"
  50. "VmPTE:\t%8lu kB\n"
  51. "VmSwap:\t%8lu kB\n",
  52. hiwater_vm << (PAGE_SHIFT-10),
  53. (total_vm - mm->reserved_vm) << (PAGE_SHIFT-10),
  54. mm->locked_vm << (PAGE_SHIFT-10),
  55. mm->pinned_vm << (PAGE_SHIFT-10),
  56. hiwater_rss << (PAGE_SHIFT-10),
  57. total_rss << (PAGE_SHIFT-10),
  58. data << (PAGE_SHIFT-10),
  59. mm->stack_vm << (PAGE_SHIFT-10), text, lib,
  60. (PTRS_PER_PTE * sizeof(pte_t) *
  61. atomic_long_read(&mm->nr_ptes)) >> 10,
  62. swap << (PAGE_SHIFT-10));
  63. }
  64. unsigned long task_vsize(struct mm_struct *mm)
  65. {
  66. return PAGE_SIZE * mm->total_vm;
  67. }
  68. unsigned long task_statm(struct mm_struct *mm,
  69. unsigned long *shared, unsigned long *text,
  70. unsigned long *data, unsigned long *resident)
  71. {
  72. *shared = get_mm_counter(mm, MM_FILEPAGES);
  73. *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
  74. >> PAGE_SHIFT;
  75. *data = mm->total_vm - mm->shared_vm;
  76. *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
  77. return mm->total_vm;
  78. }
  79. static void seq_print_vma_name(struct seq_file *m, struct vm_area_struct *vma)
  80. {
  81. const char __user *name = vma_get_anon_name(vma);
  82. struct mm_struct *mm = vma->vm_mm;
  83. unsigned long page_start_vaddr;
  84. unsigned long page_offset;
  85. unsigned long num_pages;
  86. unsigned long max_len = NAME_MAX;
  87. int i;
  88. page_start_vaddr = (unsigned long)name & PAGE_MASK;
  89. page_offset = (unsigned long)name - page_start_vaddr;
  90. num_pages = DIV_ROUND_UP(page_offset + max_len, PAGE_SIZE);
  91. seq_puts(m, "[anon:");
  92. for (i = 0; i < num_pages; i++) {
  93. int len;
  94. int write_len;
  95. const char *kaddr;
  96. long pages_pinned;
  97. struct page *page;
  98. pages_pinned = get_user_pages(current, mm, page_start_vaddr,
  99. 1, 0, 0, &page, NULL);
  100. if (pages_pinned < 1) {
  101. seq_puts(m, "<fault>]");
  102. return;
  103. }
  104. kaddr = (const char *)kmap(page);
  105. len = min(max_len, PAGE_SIZE - page_offset);
  106. write_len = strnlen(kaddr + page_offset, len);
  107. seq_write(m, kaddr + page_offset, write_len);
  108. kunmap(page);
  109. put_page(page);
  110. /* if strnlen hit a null terminator then we're done */
  111. if (write_len != len)
  112. break;
  113. max_len -= len;
  114. page_offset = 0;
  115. page_start_vaddr += PAGE_SIZE;
  116. }
  117. seq_putc(m, ']');
  118. }
  119. static void vma_stop(struct proc_maps_private *priv, struct vm_area_struct *vma)
  120. {
  121. if (vma && vma != priv->tail_vma) {
  122. struct mm_struct *mm = vma->vm_mm;
  123. up_read(&mm->mmap_sem);
  124. mmput(mm);
  125. }
  126. }
  127. static void *m_start(struct seq_file *m, loff_t *pos)
  128. {
  129. struct proc_maps_private *priv = m->private;
  130. unsigned long last_addr = m->version;
  131. struct mm_struct *mm;
  132. struct vm_area_struct *vma, *tail_vma = NULL;
  133. loff_t l = *pos;
  134. /* Clear the per syscall fields in priv */
  135. priv->task = NULL;
  136. priv->tail_vma = NULL;
  137. /*
  138. * We remember last_addr rather than next_addr to hit with
  139. * mmap_cache most of the time. We have zero last_addr at
  140. * the beginning and also after lseek. We will have -1 last_addr
  141. * after the end of the vmas.
  142. */
  143. if (last_addr == -1UL)
  144. return NULL;
  145. priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
  146. if (!priv->task)
  147. return ERR_PTR(-ESRCH);
  148. mm = priv->mm;
  149. if (!mm || !atomic_inc_not_zero(&mm->mm_users))
  150. return NULL;
  151. down_read(&mm->mmap_sem);
  152. tail_vma = get_gate_vma(priv->task->mm);
  153. priv->tail_vma = tail_vma;
  154. /* Start with last addr hint */
  155. vma = find_vma(mm, last_addr);
  156. if (last_addr && vma) {
  157. vma = vma->vm_next;
  158. goto out;
  159. }
  160. /*
  161. * Check the vma index is within the range and do
  162. * sequential scan until m_index.
  163. */
  164. vma = NULL;
  165. if ((unsigned long)l < mm->map_count) {
  166. vma = mm->mmap;
  167. while (l-- && vma)
  168. vma = vma->vm_next;
  169. goto out;
  170. }
  171. if (l != mm->map_count)
  172. tail_vma = NULL; /* After gate vma */
  173. out:
  174. if (vma)
  175. return vma;
  176. /* End of vmas has been reached */
  177. m->version = (tail_vma != NULL)? 0: -1UL;
  178. up_read(&mm->mmap_sem);
  179. mmput(mm);
  180. return tail_vma;
  181. }
  182. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  183. {
  184. struct proc_maps_private *priv = m->private;
  185. struct vm_area_struct *vma = v;
  186. struct vm_area_struct *tail_vma = priv->tail_vma;
  187. (*pos)++;
  188. if (vma && (vma != tail_vma) && vma->vm_next)
  189. return vma->vm_next;
  190. vma_stop(priv, vma);
  191. return (vma != tail_vma)? tail_vma: NULL;
  192. }
  193. static void m_stop(struct seq_file *m, void *v)
  194. {
  195. struct proc_maps_private *priv = m->private;
  196. struct vm_area_struct *vma = v;
  197. if (!IS_ERR(vma))
  198. vma_stop(priv, vma);
  199. if (priv->task)
  200. put_task_struct(priv->task);
  201. }
  202. static int proc_maps_open(struct inode *inode, struct file *file,
  203. const struct seq_operations *ops, int psize)
  204. {
  205. struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
  206. if (!priv)
  207. return -ENOMEM;
  208. priv->pid = proc_pid(inode);
  209. priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
  210. if (IS_ERR(priv->mm)) {
  211. int err = PTR_ERR(priv->mm);
  212. seq_release_private(inode, file);
  213. return err;
  214. }
  215. return 0;
  216. }
  217. static int proc_map_release(struct inode *inode, struct file *file)
  218. {
  219. struct seq_file *seq = file->private_data;
  220. struct proc_maps_private *priv = seq->private;
  221. if (priv->mm)
  222. mmdrop(priv->mm);
  223. return seq_release_private(inode, file);
  224. }
  225. static int do_maps_open(struct inode *inode, struct file *file,
  226. const struct seq_operations *ops)
  227. {
  228. return proc_maps_open(inode, file, ops,
  229. sizeof(struct proc_maps_private));
  230. }
  231. static void
  232. show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
  233. {
  234. struct mm_struct *mm = vma->vm_mm;
  235. struct file *file = vma->vm_file;
  236. struct proc_maps_private *priv = m->private;
  237. struct task_struct *task = priv->task;
  238. vm_flags_t flags = vma->vm_flags;
  239. unsigned long ino = 0;
  240. unsigned long long pgoff = 0;
  241. unsigned long start, end;
  242. dev_t dev = 0;
  243. const char *name = NULL;
  244. if (file) {
  245. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  246. dev = inode->i_sb->s_dev;
  247. ino = inode->i_ino;
  248. pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
  249. }
  250. /* We don't show the stack guard page in /proc/maps */
  251. start = vma->vm_start;
  252. end = vma->vm_end;
  253. seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
  254. seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
  255. start,
  256. end,
  257. flags & VM_READ ? 'r' : '-',
  258. flags & VM_WRITE ? 'w' : '-',
  259. flags & VM_EXEC ? 'x' : '-',
  260. flags & VM_MAYSHARE ? 's' : 'p',
  261. pgoff,
  262. MAJOR(dev), MINOR(dev), ino);
  263. /*
  264. * Print the dentry name for named mappings, and a
  265. * special [heap] marker for the heap:
  266. */
  267. if (file) {
  268. seq_pad(m, ' ');
  269. seq_path(m, &file->f_path, "\n");
  270. goto done;
  271. }
  272. name = arch_vma_name(vma);
  273. if (!name) {
  274. pid_t tid;
  275. if (!mm) {
  276. name = "[vdso]";
  277. goto done;
  278. }
  279. if (vma->vm_start <= mm->brk &&
  280. vma->vm_end >= mm->start_brk) {
  281. name = "[heap]";
  282. goto done;
  283. }
  284. tid = vm_is_stack(task, vma, is_pid);
  285. if (tid != 0) {
  286. /*
  287. * Thread stack in /proc/PID/task/TID/maps or
  288. * the main process stack.
  289. */
  290. if (!is_pid || (vma->vm_start <= mm->start_stack &&
  291. vma->vm_end >= mm->start_stack)) {
  292. name = "[stack]";
  293. } else {
  294. /* Thread stack in /proc/PID/maps */
  295. seq_pad(m, ' ');
  296. seq_printf(m, "[stack:%d]", tid);
  297. }
  298. goto done;
  299. }
  300. if (vma_get_anon_name(vma)) {
  301. seq_pad(m, ' ');
  302. seq_print_vma_name(m, vma);
  303. }
  304. }
  305. done:
  306. if (name) {
  307. seq_pad(m, ' ');
  308. seq_puts(m, name);
  309. }
  310. seq_putc(m, '\n');
  311. }
  312. static int show_map(struct seq_file *m, void *v, int is_pid)
  313. {
  314. struct vm_area_struct *vma = v;
  315. struct proc_maps_private *priv = m->private;
  316. struct task_struct *task = priv->task;
  317. show_map_vma(m, vma, is_pid);
  318. if (m->count < m->size) /* vma is copied successfully */
  319. m->version = (vma != get_gate_vma(task->mm))
  320. ? vma->vm_start : 0;
  321. return 0;
  322. }
  323. static int show_pid_map(struct seq_file *m, void *v)
  324. {
  325. return show_map(m, v, 1);
  326. }
  327. static int show_tid_map(struct seq_file *m, void *v)
  328. {
  329. return show_map(m, v, 0);
  330. }
  331. static const struct seq_operations proc_pid_maps_op = {
  332. .start = m_start,
  333. .next = m_next,
  334. .stop = m_stop,
  335. .show = show_pid_map
  336. };
  337. static const struct seq_operations proc_tid_maps_op = {
  338. .start = m_start,
  339. .next = m_next,
  340. .stop = m_stop,
  341. .show = show_tid_map
  342. };
  343. static int pid_maps_open(struct inode *inode, struct file *file)
  344. {
  345. return do_maps_open(inode, file, &proc_pid_maps_op);
  346. }
  347. static int tid_maps_open(struct inode *inode, struct file *file)
  348. {
  349. return do_maps_open(inode, file, &proc_tid_maps_op);
  350. }
  351. const struct file_operations proc_pid_maps_operations = {
  352. .open = pid_maps_open,
  353. .read = seq_read,
  354. .llseek = seq_lseek,
  355. .release = proc_map_release,
  356. };
  357. const struct file_operations proc_tid_maps_operations = {
  358. .open = tid_maps_open,
  359. .read = seq_read,
  360. .llseek = seq_lseek,
  361. .release = proc_map_release,
  362. };
  363. /*
  364. * Proportional Set Size(PSS): my share of RSS.
  365. *
  366. * PSS of a process is the count of pages it has in memory, where each
  367. * page is divided by the number of processes sharing it. So if a
  368. * process has 1000 pages all to itself, and 1000 shared with one other
  369. * process, its PSS will be 1500.
  370. *
  371. * To keep (accumulated) division errors low, we adopt a 64bit
  372. * fixed-point pss counter to minimize division errors. So (pss >>
  373. * PSS_SHIFT) would be the real byte count.
  374. *
  375. * A shift of 12 before division means (assuming 4K page size):
  376. * - 1M 3-user-pages add up to 8KB errors;
  377. * - supports mapcount up to 2^24, or 16M;
  378. * - supports PSS up to 2^52 bytes, or 4PB.
  379. */
  380. #define PSS_SHIFT 12
  381. #ifdef CONFIG_PROC_PAGE_MONITOR
  382. struct mem_size_stats {
  383. struct vm_area_struct *vma;
  384. unsigned long resident;
  385. unsigned long shared_clean;
  386. unsigned long shared_dirty;
  387. unsigned long private_clean;
  388. unsigned long private_dirty;
  389. unsigned long referenced;
  390. unsigned long anonymous;
  391. unsigned long anonymous_thp;
  392. unsigned long swap;
  393. unsigned long nonlinear;
  394. u64 pss;
  395. u64 swap_pss;
  396. };
  397. static void smaps_pte_entry(pte_t ptent, unsigned long addr,
  398. unsigned long ptent_size, struct mm_walk *walk)
  399. {
  400. struct mem_size_stats *mss = walk->private;
  401. struct vm_area_struct *vma = mss->vma;
  402. pgoff_t pgoff = linear_page_index(vma, addr);
  403. struct page *page = NULL;
  404. int mapcount;
  405. if (pte_present(ptent)) {
  406. page = vm_normal_page(vma, addr, ptent);
  407. } else if (is_swap_pte(ptent)) {
  408. swp_entry_t swpent = pte_to_swp_entry(ptent);
  409. if (!non_swap_entry(swpent)) {
  410. int mapcount;
  411. mss->swap += ptent_size;
  412. mapcount = swp_swapcount(swpent);
  413. if (mapcount >= 2) {
  414. u64 pss_delta = (u64)ptent_size << PSS_SHIFT;
  415. do_div(pss_delta, mapcount);
  416. mss->swap_pss += pss_delta;
  417. } else {
  418. mss->swap_pss += (u64)ptent_size << PSS_SHIFT;
  419. }
  420. } else if (is_migration_entry(swpent))
  421. page = migration_entry_to_page(swpent);
  422. } else if (pte_file(ptent)) {
  423. if (pte_to_pgoff(ptent) != pgoff)
  424. mss->nonlinear += ptent_size;
  425. }
  426. if (!page)
  427. return;
  428. if (PageAnon(page))
  429. mss->anonymous += ptent_size;
  430. if (page->index != pgoff)
  431. mss->nonlinear += ptent_size;
  432. mss->resident += ptent_size;
  433. /* Accumulate the size in pages that have been accessed. */
  434. if (pte_young(ptent) || PageReferenced(page))
  435. mss->referenced += ptent_size;
  436. mapcount = page_mapcount(page);
  437. if (mapcount >= 2) {
  438. if (pte_dirty(ptent) || PageDirty(page))
  439. mss->shared_dirty += ptent_size;
  440. else
  441. mss->shared_clean += ptent_size;
  442. mss->pss += (ptent_size << PSS_SHIFT) / mapcount;
  443. } else {
  444. if (pte_dirty(ptent) || PageDirty(page))
  445. mss->private_dirty += ptent_size;
  446. else
  447. mss->private_clean += ptent_size;
  448. mss->pss += (ptent_size << PSS_SHIFT);
  449. }
  450. }
  451. static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
  452. struct mm_walk *walk)
  453. {
  454. struct mem_size_stats *mss = walk->private;
  455. struct vm_area_struct *vma = mss->vma;
  456. pte_t *pte;
  457. spinlock_t *ptl;
  458. if (pmd_trans_huge_lock(pmd, vma) == 1) {
  459. smaps_pte_entry(*(pte_t *)pmd, addr, HPAGE_PMD_SIZE, walk);
  460. spin_unlock(&walk->mm->page_table_lock);
  461. mss->anonymous_thp += HPAGE_PMD_SIZE;
  462. return 0;
  463. }
  464. if (pmd_trans_unstable(pmd))
  465. return 0;
  466. /*
  467. * The mmap_sem held all the way back in m_start() is what
  468. * keeps khugepaged out of here and from collapsing things
  469. * in here.
  470. */
  471. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  472. for (; addr != end; pte++, addr += PAGE_SIZE)
  473. smaps_pte_entry(*pte, addr, PAGE_SIZE, walk);
  474. pte_unmap_unlock(pte - 1, ptl);
  475. cond_resched();
  476. return 0;
  477. }
  478. static int show_smap(struct seq_file *m, void *v, int is_pid)
  479. {
  480. struct proc_maps_private *priv = m->private;
  481. struct task_struct *task = priv->task;
  482. struct vm_area_struct *vma = v;
  483. struct mem_size_stats mss;
  484. struct mm_walk smaps_walk = {
  485. .pmd_entry = smaps_pte_range,
  486. .mm = vma->vm_mm,
  487. .private = &mss,
  488. };
  489. memset(&mss, 0, sizeof mss);
  490. mss.vma = vma;
  491. /* mmap_sem is held in m_start */
  492. if (vma->vm_mm && !is_vm_hugetlb_page(vma))
  493. walk_page_range(vma->vm_start, vma->vm_end, &smaps_walk);
  494. show_map_vma(m, vma, is_pid);
  495. seq_printf(m,
  496. "Size: %8lu kB\n"
  497. "Rss: %8lu kB\n"
  498. "Pss: %8lu kB\n"
  499. "Shared_Clean: %8lu kB\n"
  500. "Shared_Dirty: %8lu kB\n"
  501. "Private_Clean: %8lu kB\n"
  502. "Private_Dirty: %8lu kB\n"
  503. "Referenced: %8lu kB\n"
  504. "Anonymous: %8lu kB\n"
  505. "AnonHugePages: %8lu kB\n"
  506. "Swap: %8lu kB\n"
  507. "SwapPss: %8lu kB\n"
  508. "KernelPageSize: %8lu kB\n"
  509. "MMUPageSize: %8lu kB\n"
  510. "Locked: %8lu kB\n",
  511. (vma->vm_end - vma->vm_start) >> 10,
  512. mss.resident >> 10,
  513. (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
  514. mss.shared_clean >> 10,
  515. mss.shared_dirty >> 10,
  516. mss.private_clean >> 10,
  517. mss.private_dirty >> 10,
  518. mss.referenced >> 10,
  519. mss.anonymous >> 10,
  520. mss.anonymous_thp >> 10,
  521. mss.swap >> 10,
  522. (unsigned long)(mss.swap_pss >> (10 + PSS_SHIFT)),
  523. vma_kernel_pagesize(vma) >> 10,
  524. vma_mmu_pagesize(vma) >> 10,
  525. (vma->vm_flags & VM_LOCKED) ?
  526. (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
  527. if (vma_get_anon_name(vma)) {
  528. seq_puts(m, "Name: ");
  529. seq_print_vma_name(m, vma);
  530. seq_putc(m, '\n');
  531. }
  532. if (vma->vm_flags & VM_NONLINEAR)
  533. seq_printf(m, "Nonlinear: %8lu kB\n",
  534. mss.nonlinear >> 10);
  535. if (m->count < m->size) /* vma is copied successfully */
  536. m->version = (vma != get_gate_vma(task->mm))
  537. ? vma->vm_start : 0;
  538. return 0;
  539. }
  540. static int show_pid_smap(struct seq_file *m, void *v)
  541. {
  542. return show_smap(m, v, 1);
  543. }
  544. static int show_tid_smap(struct seq_file *m, void *v)
  545. {
  546. return show_smap(m, v, 0);
  547. }
  548. static const struct seq_operations proc_pid_smaps_op = {
  549. .start = m_start,
  550. .next = m_next,
  551. .stop = m_stop,
  552. .show = show_pid_smap
  553. };
  554. static const struct seq_operations proc_tid_smaps_op = {
  555. .start = m_start,
  556. .next = m_next,
  557. .stop = m_stop,
  558. .show = show_tid_smap
  559. };
  560. static int pid_smaps_open(struct inode *inode, struct file *file)
  561. {
  562. return do_maps_open(inode, file, &proc_pid_smaps_op);
  563. }
  564. static int tid_smaps_open(struct inode *inode, struct file *file)
  565. {
  566. return do_maps_open(inode, file, &proc_tid_smaps_op);
  567. }
  568. const struct file_operations proc_pid_smaps_operations = {
  569. .open = pid_smaps_open,
  570. .read = seq_read,
  571. .llseek = seq_lseek,
  572. .release = proc_map_release,
  573. };
  574. static int proc_pid_smaps_simple_show(struct seq_file *m, void *v)
  575. {
  576. struct pid *pid = (struct pid *)m->private;
  577. struct task_struct *task;
  578. struct mm_struct *mm;
  579. struct vm_area_struct *vma;
  580. struct mem_size_stats mss_total;
  581. struct mem_size_stats mss;
  582. int ret = 0;
  583. struct mm_walk smaps_walk = {
  584. .pmd_entry = smaps_pte_range,
  585. .private = &mss,
  586. };
  587. task = get_pid_task(pid, PIDTYPE_PID);
  588. if (!task) {
  589. ret = -1;
  590. goto error_task;
  591. }
  592. mm = mm_access(task, PTRACE_MODE_READ);
  593. if (!mm || IS_ERR(mm)) {
  594. ret = -2;
  595. goto error_mm;
  596. }
  597. memset(&mss_total, 0, sizeof mss_total);
  598. down_read(&mm->mmap_sem);
  599. vma = mm->mmap;
  600. while (vma) {
  601. memset(&mss, 0, sizeof mss);
  602. mss.vma = vma;
  603. smaps_walk.mm = vma->vm_mm;
  604. if (vma->vm_mm && !is_vm_hugetlb_page(vma)) {
  605. walk_page_range(vma->vm_start, vma->vm_end, &smaps_walk);
  606. mss_total.pss += mss.pss;
  607. mss_total.swap_pss += mss.swap_pss;
  608. }
  609. vma = vma->vm_next;
  610. }
  611. up_read(&mm->mmap_sem);
  612. mmput(mm);
  613. seq_printf(m,
  614. "Pss: %8lu kB\n"
  615. "SwapPss: %8lu kB\n",
  616. (unsigned long)(mss_total.pss >> (10 + PSS_SHIFT)),
  617. (unsigned long)(mss_total.swap_pss >> (10 + PSS_SHIFT)));
  618. error_mm:
  619. put_task_struct(task);
  620. error_task:
  621. return 0;
  622. }
  623. static int proc_pid_smaps_simple_open(struct inode *inode, struct file *file)
  624. {
  625. return single_open(file, proc_pid_smaps_simple_show, proc_pid(inode));
  626. }
  627. const struct file_operations proc_pid_smaps_simple_operations = {
  628. .open = proc_pid_smaps_simple_open,
  629. .read = seq_read,
  630. .llseek = seq_lseek,
  631. .release = single_release,
  632. };
  633. const struct file_operations proc_tid_smaps_operations = {
  634. .open = tid_smaps_open,
  635. .read = seq_read,
  636. .llseek = seq_lseek,
  637. .release = proc_map_release,
  638. };
  639. static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
  640. unsigned long end, struct mm_walk *walk)
  641. {
  642. struct vm_area_struct *vma = walk->private;
  643. pte_t *pte, ptent;
  644. spinlock_t *ptl;
  645. struct page *page;
  646. split_huge_page_pmd(walk->mm, pmd);
  647. if (pmd_trans_unstable(pmd))
  648. return 0;
  649. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  650. for (; addr != end; pte++, addr += PAGE_SIZE) {
  651. ptent = *pte;
  652. if (!pte_present(ptent))
  653. continue;
  654. page = vm_normal_page(vma, addr, ptent);
  655. if (!page)
  656. continue;
  657. /* Clear accessed and referenced bits. */
  658. ptep_test_and_clear_young(vma, addr, pte);
  659. ClearPageReferenced(page);
  660. }
  661. pte_unmap_unlock(pte - 1, ptl);
  662. cond_resched();
  663. return 0;
  664. }
  665. #define CLEAR_REFS_ALL 1
  666. #define CLEAR_REFS_ANON 2
  667. #define CLEAR_REFS_MAPPED 3
  668. #define CLEAR_REFS_MM_HIWATER_RSS 5
  669. static ssize_t clear_refs_write(struct file *file, const char __user *buf,
  670. size_t count, loff_t *ppos)
  671. {
  672. struct task_struct *task;
  673. char buffer[PROC_NUMBUF];
  674. struct mm_struct *mm;
  675. struct vm_area_struct *vma;
  676. int type;
  677. int rv;
  678. memset(buffer, 0, sizeof(buffer));
  679. if (count > sizeof(buffer) - 1)
  680. count = sizeof(buffer) - 1;
  681. if (copy_from_user(buffer, buf, count))
  682. return -EFAULT;
  683. rv = kstrtoint(strstrip(buffer), 10, &type);
  684. if (rv < 0)
  685. return rv;
  686. if ((type < CLEAR_REFS_ALL || type > CLEAR_REFS_MAPPED) &&
  687. type != CLEAR_REFS_MM_HIWATER_RSS)
  688. return -EINVAL;
  689. task = get_proc_task(file->f_path.dentry->d_inode);
  690. if (!task)
  691. return -ESRCH;
  692. mm = get_task_mm(task);
  693. if (mm) {
  694. struct mm_walk clear_refs_walk = {
  695. .pmd_entry = clear_refs_pte_range,
  696. .mm = mm,
  697. };
  698. if (type == CLEAR_REFS_MM_HIWATER_RSS) {
  699. /*
  700. * Writing 5 to /proc/pid/clear_refs resets the peak
  701. * resident set size to this mm's current rss value.
  702. */
  703. down_write(&mm->mmap_sem);
  704. reset_mm_hiwater_rss(mm);
  705. up_write(&mm->mmap_sem);
  706. goto out_mm;
  707. }
  708. down_read(&mm->mmap_sem);
  709. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  710. clear_refs_walk.private = vma;
  711. if (is_vm_hugetlb_page(vma))
  712. continue;
  713. /*
  714. * Writing 1 to /proc/pid/clear_refs affects all pages.
  715. *
  716. * Writing 2 to /proc/pid/clear_refs only affects
  717. * Anonymous pages.
  718. *
  719. * Writing 3 to /proc/pid/clear_refs only affects file
  720. * mapped pages.
  721. */
  722. if (type == CLEAR_REFS_ANON && vma->vm_file)
  723. continue;
  724. if (type == CLEAR_REFS_MAPPED && !vma->vm_file)
  725. continue;
  726. walk_page_range(vma->vm_start, vma->vm_end,
  727. &clear_refs_walk);
  728. }
  729. flush_tlb_mm(mm);
  730. up_read(&mm->mmap_sem);
  731. out_mm:
  732. mmput(mm);
  733. }
  734. put_task_struct(task);
  735. return count;
  736. }
  737. const struct file_operations proc_clear_refs_operations = {
  738. .write = clear_refs_write,
  739. .llseek = noop_llseek,
  740. };
  741. typedef struct {
  742. u64 pme;
  743. } pagemap_entry_t;
  744. struct pagemapread {
  745. int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
  746. pagemap_entry_t *buffer;
  747. };
  748. #define PAGEMAP_WALK_SIZE (PMD_SIZE)
  749. #define PAGEMAP_WALK_MASK (PMD_MASK)
  750. #define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
  751. #define PM_STATUS_BITS 3
  752. #define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
  753. #define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
  754. #define PM_STATUS(nr) (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
  755. #define PM_PSHIFT_BITS 6
  756. #define PM_PSHIFT_OFFSET (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
  757. #define PM_PSHIFT_MASK (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
  758. #define PM_PSHIFT(x) (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
  759. #define PM_PFRAME_MASK ((1LL << PM_PSHIFT_OFFSET) - 1)
  760. #define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
  761. #define PM_PRESENT PM_STATUS(4LL)
  762. #define PM_SWAP PM_STATUS(2LL)
  763. #define PM_FILE PM_STATUS(1LL)
  764. #define PM_NOT_PRESENT PM_PSHIFT(PAGE_SHIFT)
  765. #define PM_END_OF_BUFFER 1
  766. static inline pagemap_entry_t make_pme(u64 val)
  767. {
  768. return (pagemap_entry_t) { .pme = val };
  769. }
  770. static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
  771. struct pagemapread *pm)
  772. {
  773. pm->buffer[pm->pos++] = *pme;
  774. if (pm->pos >= pm->len)
  775. return PM_END_OF_BUFFER;
  776. return 0;
  777. }
  778. static int pagemap_pte_hole(unsigned long start, unsigned long end,
  779. struct mm_walk *walk)
  780. {
  781. struct pagemapread *pm = walk->private;
  782. unsigned long addr;
  783. int err = 0;
  784. pagemap_entry_t pme = make_pme(PM_NOT_PRESENT);
  785. for (addr = start; addr < end; addr += PAGE_SIZE) {
  786. err = add_to_pagemap(addr, &pme, pm);
  787. if (err)
  788. break;
  789. }
  790. return err;
  791. }
  792. static void pte_to_pagemap_entry(pagemap_entry_t *pme,
  793. struct vm_area_struct *vma, unsigned long addr, pte_t pte)
  794. {
  795. u64 frame, flags;
  796. struct page *page = NULL;
  797. if (pte_present(pte)) {
  798. frame = pte_pfn(pte);
  799. flags = PM_PRESENT;
  800. page = vm_normal_page(vma, addr, pte);
  801. } else if (is_swap_pte(pte)) {
  802. swp_entry_t entry = pte_to_swp_entry(pte);
  803. frame = swp_type(entry) |
  804. (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
  805. flags = PM_SWAP;
  806. if (is_migration_entry(entry))
  807. page = migration_entry_to_page(entry);
  808. } else {
  809. *pme = make_pme(PM_NOT_PRESENT);
  810. return;
  811. }
  812. if (page && !PageAnon(page))
  813. flags |= PM_FILE;
  814. *pme = make_pme(PM_PFRAME(frame) | PM_PSHIFT(PAGE_SHIFT) | flags);
  815. }
  816. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  817. static void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme,
  818. pmd_t pmd, int offset)
  819. {
  820. /*
  821. * Currently pmd for thp is always present because thp can not be
  822. * swapped-out, migrated, or HWPOISONed (split in such cases instead.)
  823. * This if-check is just to prepare for future implementation.
  824. */
  825. if (pmd_present(pmd))
  826. *pme = make_pme(PM_PFRAME(pmd_pfn(pmd) + offset)
  827. | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT);
  828. else
  829. *pme = make_pme(PM_NOT_PRESENT);
  830. }
  831. #else
  832. static inline void thp_pmd_to_pagemap_entry(pagemap_entry_t *pme,
  833. pmd_t pmd, int offset)
  834. {
  835. }
  836. #endif
  837. static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
  838. struct mm_walk *walk)
  839. {
  840. struct vm_area_struct *vma;
  841. struct pagemapread *pm = walk->private;
  842. pte_t *pte;
  843. int err = 0;
  844. pagemap_entry_t pme = make_pme(PM_NOT_PRESENT);
  845. /* find the first VMA at or above 'addr' */
  846. vma = find_vma(walk->mm, addr);
  847. if (vma && pmd_trans_huge_lock(pmd, vma) == 1) {
  848. for (; addr != end; addr += PAGE_SIZE) {
  849. unsigned long offset;
  850. offset = (addr & ~PAGEMAP_WALK_MASK) >>
  851. PAGE_SHIFT;
  852. thp_pmd_to_pagemap_entry(&pme, *pmd, offset);
  853. err = add_to_pagemap(addr, &pme, pm);
  854. if (err)
  855. break;
  856. }
  857. spin_unlock(&walk->mm->page_table_lock);
  858. return err;
  859. }
  860. if (pmd_trans_unstable(pmd))
  861. return 0;
  862. for (; addr != end; addr += PAGE_SIZE) {
  863. /* check to see if we've left 'vma' behind
  864. * and need a new, higher one */
  865. if (vma && (addr >= vma->vm_end)) {
  866. vma = find_vma(walk->mm, addr);
  867. pme = make_pme(PM_NOT_PRESENT);
  868. }
  869. /* check that 'vma' actually covers this address,
  870. * and that it isn't a huge page vma */
  871. if (vma && (vma->vm_start <= addr) &&
  872. !is_vm_hugetlb_page(vma)) {
  873. pte = pte_offset_map(pmd, addr);
  874. pte_to_pagemap_entry(&pme, vma, addr, *pte);
  875. /* unmap before userspace copy */
  876. pte_unmap(pte);
  877. }
  878. err = add_to_pagemap(addr, &pme, pm);
  879. if (err)
  880. return err;
  881. }
  882. cond_resched();
  883. return err;
  884. }
  885. #ifdef CONFIG_HUGETLB_PAGE
  886. static void huge_pte_to_pagemap_entry(pagemap_entry_t *pme,
  887. pte_t pte, int offset)
  888. {
  889. if (pte_present(pte))
  890. *pme = make_pme(PM_PFRAME(pte_pfn(pte) + offset)
  891. | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT);
  892. else
  893. *pme = make_pme(PM_NOT_PRESENT);
  894. }
  895. /* This function walks within one hugetlb entry in the single call */
  896. static int pagemap_hugetlb_range(pte_t *pte, unsigned long hmask,
  897. unsigned long addr, unsigned long end,
  898. struct mm_walk *walk)
  899. {
  900. struct pagemapread *pm = walk->private;
  901. int err = 0;
  902. pagemap_entry_t pme;
  903. for (; addr != end; addr += PAGE_SIZE) {
  904. int offset = (addr & ~hmask) >> PAGE_SHIFT;
  905. huge_pte_to_pagemap_entry(&pme, *pte, offset);
  906. err = add_to_pagemap(addr, &pme, pm);
  907. if (err)
  908. return err;
  909. }
  910. cond_resched();
  911. return err;
  912. }
  913. #endif /* HUGETLB_PAGE */
  914. /*
  915. * /proc/pid/pagemap - an array mapping virtual pages to pfns
  916. *
  917. * For each page in the address space, this file contains one 64-bit entry
  918. * consisting of the following:
  919. *
  920. * Bits 0-54 page frame number (PFN) if present
  921. * Bits 0-4 swap type if swapped
  922. * Bits 5-54 swap offset if swapped
  923. * Bits 55-60 page shift (page size = 1<<page shift)
  924. * Bit 61 page is file-page or shared-anon
  925. * Bit 62 page swapped
  926. * Bit 63 page present
  927. *
  928. * If the page is not present but in swap, then the PFN contains an
  929. * encoding of the swap file number and the page's offset into the
  930. * swap. Unmapped pages return a null PFN. This allows determining
  931. * precisely which pages are mapped (or in swap) and comparing mapped
  932. * pages between processes.
  933. *
  934. * Efficient users of this interface will use /proc/pid/maps to
  935. * determine which areas of memory are actually mapped and llseek to
  936. * skip over unmapped regions.
  937. */
  938. static ssize_t pagemap_read(struct file *file, char __user *buf,
  939. size_t count, loff_t *ppos)
  940. {
  941. struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
  942. struct mm_struct *mm;
  943. struct pagemapread pm;
  944. int ret = -ESRCH;
  945. struct mm_walk pagemap_walk = {};
  946. unsigned long src;
  947. unsigned long svpfn;
  948. unsigned long start_vaddr;
  949. unsigned long end_vaddr;
  950. int copied = 0;
  951. if (!task)
  952. goto out;
  953. ret = -EINVAL;
  954. /* file position must be aligned */
  955. if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
  956. goto out_task;
  957. ret = 0;
  958. if (!count)
  959. goto out_task;
  960. pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
  961. pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
  962. ret = -ENOMEM;
  963. if (!pm.buffer)
  964. goto out_task;
  965. mm = mm_access(task, PTRACE_MODE_READ);
  966. ret = PTR_ERR(mm);
  967. if (!mm || IS_ERR(mm))
  968. goto out_free;
  969. pagemap_walk.pmd_entry = pagemap_pte_range;
  970. pagemap_walk.pte_hole = pagemap_pte_hole;
  971. #ifdef CONFIG_HUGETLB_PAGE
  972. pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
  973. #endif
  974. pagemap_walk.mm = mm;
  975. pagemap_walk.private = &pm;
  976. src = *ppos;
  977. svpfn = src / PM_ENTRY_BYTES;
  978. start_vaddr = svpfn << PAGE_SHIFT;
  979. end_vaddr = TASK_SIZE_OF(task);
  980. /* watch out for wraparound */
  981. if (svpfn > TASK_SIZE_OF(task) >> PAGE_SHIFT)
  982. start_vaddr = end_vaddr;
  983. /*
  984. * The odds are that this will stop walking way
  985. * before end_vaddr, because the length of the
  986. * user buffer is tracked in "pm", and the walk
  987. * will stop when we hit the end of the buffer.
  988. */
  989. ret = 0;
  990. while (count && (start_vaddr < end_vaddr)) {
  991. int len;
  992. unsigned long end;
  993. pm.pos = 0;
  994. end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
  995. /* overflow ? */
  996. if (end < start_vaddr || end > end_vaddr)
  997. end = end_vaddr;
  998. down_read(&mm->mmap_sem);
  999. ret = walk_page_range(start_vaddr, end, &pagemap_walk);
  1000. up_read(&mm->mmap_sem);
  1001. start_vaddr = end;
  1002. len = min(count, PM_ENTRY_BYTES * pm.pos);
  1003. if (copy_to_user(buf, pm.buffer, len)) {
  1004. ret = -EFAULT;
  1005. goto out_mm;
  1006. }
  1007. copied += len;
  1008. buf += len;
  1009. count -= len;
  1010. }
  1011. *ppos += copied;
  1012. if (!ret || ret == PM_END_OF_BUFFER)
  1013. ret = copied;
  1014. out_mm:
  1015. mmput(mm);
  1016. out_free:
  1017. kfree(pm.buffer);
  1018. out_task:
  1019. put_task_struct(task);
  1020. out:
  1021. return ret;
  1022. }
  1023. static int pagemap_open(struct inode *inode, struct file *file)
  1024. {
  1025. /* do not disclose physical addresses to unprivileged
  1026. userspace (closes a rowhammer attack vector) */
  1027. if (!capable(CAP_SYS_ADMIN))
  1028. return -EPERM;
  1029. return 0;
  1030. }
  1031. const struct file_operations proc_pagemap_operations = {
  1032. .llseek = mem_lseek, /* borrow this */
  1033. .read = pagemap_read,
  1034. .open = pagemap_open,
  1035. };
  1036. #endif /* CONFIG_PROC_PAGE_MONITOR */
  1037. #ifdef CONFIG_NUMA
  1038. struct numa_maps {
  1039. struct vm_area_struct *vma;
  1040. unsigned long pages;
  1041. unsigned long anon;
  1042. unsigned long active;
  1043. unsigned long writeback;
  1044. unsigned long mapcount_max;
  1045. unsigned long dirty;
  1046. unsigned long swapcache;
  1047. unsigned long node[MAX_NUMNODES];
  1048. };
  1049. struct numa_maps_private {
  1050. struct proc_maps_private proc_maps;
  1051. struct numa_maps md;
  1052. };
  1053. static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
  1054. unsigned long nr_pages)
  1055. {
  1056. int count = page_mapcount(page);
  1057. md->pages += nr_pages;
  1058. if (pte_dirty || PageDirty(page))
  1059. md->dirty += nr_pages;
  1060. if (PageSwapCache(page))
  1061. md->swapcache += nr_pages;
  1062. if (PageActive(page) || PageUnevictable(page))
  1063. md->active += nr_pages;
  1064. if (PageWriteback(page))
  1065. md->writeback += nr_pages;
  1066. if (PageAnon(page))
  1067. md->anon += nr_pages;
  1068. if (count > md->mapcount_max)
  1069. md->mapcount_max = count;
  1070. md->node[page_to_nid(page)] += nr_pages;
  1071. }
  1072. static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
  1073. unsigned long addr)
  1074. {
  1075. struct page *page;
  1076. int nid;
  1077. if (!pte_present(pte))
  1078. return NULL;
  1079. page = vm_normal_page(vma, addr, pte);
  1080. if (!page)
  1081. return NULL;
  1082. if (PageReserved(page))
  1083. return NULL;
  1084. nid = page_to_nid(page);
  1085. if (!node_isset(nid, node_states[N_MEMORY]))
  1086. return NULL;
  1087. return page;
  1088. }
  1089. static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
  1090. unsigned long end, struct mm_walk *walk)
  1091. {
  1092. struct numa_maps *md;
  1093. spinlock_t *ptl;
  1094. pte_t *orig_pte;
  1095. pte_t *pte;
  1096. md = walk->private;
  1097. if (pmd_trans_huge_lock(pmd, md->vma) == 1) {
  1098. pte_t huge_pte = *(pte_t *)pmd;
  1099. struct page *page;
  1100. page = can_gather_numa_stats(huge_pte, md->vma, addr);
  1101. if (page)
  1102. gather_stats(page, md, pte_dirty(huge_pte),
  1103. HPAGE_PMD_SIZE/PAGE_SIZE);
  1104. spin_unlock(&walk->mm->page_table_lock);
  1105. return 0;
  1106. }
  1107. if (pmd_trans_unstable(pmd))
  1108. return 0;
  1109. orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
  1110. do {
  1111. struct page *page = can_gather_numa_stats(*pte, md->vma, addr);
  1112. if (!page)
  1113. continue;
  1114. gather_stats(page, md, pte_dirty(*pte), 1);
  1115. } while (pte++, addr += PAGE_SIZE, addr != end);
  1116. pte_unmap_unlock(orig_pte, ptl);
  1117. return 0;
  1118. }
  1119. #ifdef CONFIG_HUGETLB_PAGE
  1120. static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
  1121. unsigned long addr, unsigned long end, struct mm_walk *walk)
  1122. {
  1123. struct numa_maps *md;
  1124. struct page *page;
  1125. if (pte_none(*pte))
  1126. return 0;
  1127. page = pte_page(*pte);
  1128. if (!page)
  1129. return 0;
  1130. md = walk->private;
  1131. gather_stats(page, md, pte_dirty(*pte), 1);
  1132. return 0;
  1133. }
  1134. #else
  1135. static int gather_hugetbl_stats(pte_t *pte, unsigned long hmask,
  1136. unsigned long addr, unsigned long end, struct mm_walk *walk)
  1137. {
  1138. return 0;
  1139. }
  1140. #endif
  1141. /*
  1142. * Display pages allocated per node and memory policy via /proc.
  1143. */
  1144. static int show_numa_map(struct seq_file *m, void *v, int is_pid)
  1145. {
  1146. struct numa_maps_private *numa_priv = m->private;
  1147. struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
  1148. struct vm_area_struct *vma = v;
  1149. struct numa_maps *md = &numa_priv->md;
  1150. struct file *file = vma->vm_file;
  1151. struct mm_struct *mm = vma->vm_mm;
  1152. struct mm_walk walk = {};
  1153. struct mempolicy *pol;
  1154. int n;
  1155. char buffer[50];
  1156. if (!mm)
  1157. return 0;
  1158. /* Ensure we start with an empty set of numa_maps statistics. */
  1159. memset(md, 0, sizeof(*md));
  1160. md->vma = vma;
  1161. walk.hugetlb_entry = gather_hugetbl_stats;
  1162. walk.pmd_entry = gather_pte_stats;
  1163. walk.private = md;
  1164. walk.mm = mm;
  1165. pol = get_vma_policy(proc_priv->task, vma, vma->vm_start);
  1166. mpol_to_str(buffer, sizeof(buffer), pol, 0);
  1167. mpol_cond_put(pol);
  1168. seq_printf(m, "%08lx %s", vma->vm_start, buffer);
  1169. if (file) {
  1170. seq_printf(m, " file=");
  1171. seq_path(m, &file->f_path, "\n\t= ");
  1172. } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
  1173. seq_printf(m, " heap");
  1174. } else {
  1175. pid_t tid = vm_is_stack(proc_priv->task, vma, is_pid);
  1176. if (tid != 0) {
  1177. /*
  1178. * Thread stack in /proc/PID/task/TID/maps or
  1179. * the main process stack.
  1180. */
  1181. if (!is_pid || (vma->vm_start <= mm->start_stack &&
  1182. vma->vm_end >= mm->start_stack))
  1183. seq_printf(m, " stack");
  1184. else
  1185. seq_printf(m, " stack:%d", tid);
  1186. }
  1187. }
  1188. if (is_vm_hugetlb_page(vma))
  1189. seq_printf(m, " huge");
  1190. walk_page_range(vma->vm_start, vma->vm_end, &walk);
  1191. if (!md->pages)
  1192. goto out;
  1193. if (md->anon)
  1194. seq_printf(m, " anon=%lu", md->anon);
  1195. if (md->dirty)
  1196. seq_printf(m, " dirty=%lu", md->dirty);
  1197. if (md->pages != md->anon && md->pages != md->dirty)
  1198. seq_printf(m, " mapped=%lu", md->pages);
  1199. if (md->mapcount_max > 1)
  1200. seq_printf(m, " mapmax=%lu", md->mapcount_max);
  1201. if (md->swapcache)
  1202. seq_printf(m, " swapcache=%lu", md->swapcache);
  1203. if (md->active < md->pages && !is_vm_hugetlb_page(vma))
  1204. seq_printf(m, " active=%lu", md->active);
  1205. if (md->writeback)
  1206. seq_printf(m, " writeback=%lu", md->writeback);
  1207. for_each_node_state(n, N_MEMORY)
  1208. if (md->node[n])
  1209. seq_printf(m, " N%d=%lu", n, md->node[n]);
  1210. out:
  1211. seq_putc(m, '\n');
  1212. if (m->count < m->size)
  1213. m->version = (vma != proc_priv->tail_vma) ? vma->vm_start : 0;
  1214. return 0;
  1215. }
  1216. static int show_pid_numa_map(struct seq_file *m, void *v)
  1217. {
  1218. return show_numa_map(m, v, 1);
  1219. }
  1220. static int show_tid_numa_map(struct seq_file *m, void *v)
  1221. {
  1222. return show_numa_map(m, v, 0);
  1223. }
  1224. static const struct seq_operations proc_pid_numa_maps_op = {
  1225. .start = m_start,
  1226. .next = m_next,
  1227. .stop = m_stop,
  1228. .show = show_pid_numa_map,
  1229. };
  1230. static const struct seq_operations proc_tid_numa_maps_op = {
  1231. .start = m_start,
  1232. .next = m_next,
  1233. .stop = m_stop,
  1234. .show = show_tid_numa_map,
  1235. };
  1236. static int numa_maps_open(struct inode *inode, struct file *file,
  1237. const struct seq_operations *ops)
  1238. {
  1239. return proc_maps_open(inode, file, ops,
  1240. sizeof(struct numa_maps_private));
  1241. }
  1242. static int pid_numa_maps_open(struct inode *inode, struct file *file)
  1243. {
  1244. return numa_maps_open(inode, file, &proc_pid_numa_maps_op);
  1245. }
  1246. static int tid_numa_maps_open(struct inode *inode, struct file *file)
  1247. {
  1248. return numa_maps_open(inode, file, &proc_tid_numa_maps_op);
  1249. }
  1250. const struct file_operations proc_pid_numa_maps_operations = {
  1251. .open = pid_numa_maps_open,
  1252. .read = seq_read,
  1253. .llseek = seq_lseek,
  1254. .release = proc_map_release,
  1255. };
  1256. const struct file_operations proc_tid_numa_maps_operations = {
  1257. .open = tid_numa_maps_open,
  1258. .read = seq_read,
  1259. .llseek = seq_lseek,
  1260. .release = proc_map_release,
  1261. };
  1262. #endif /* CONFIG_NUMA */