task_mmu.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. #include <linux/mm.h>
  2. #include <linux/vmacache.h>
  3. #include <linux/hugetlb.h>
  4. #include <linux/huge_mm.h>
  5. #include <linux/mount.h>
  6. #include <linux/seq_file.h>
  7. #include <linux/highmem.h>
  8. #include <linux/ptrace.h>
  9. #include <linux/slab.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/mempolicy.h>
  12. #include <linux/rmap.h>
  13. #include <linux/swap.h>
  14. #include <linux/swapops.h>
  15. #include <linux/mmu_notifier.h>
  16. #include <linux/page_idle.h>
  17. #include <linux/shmem_fs.h>
  18. #include <asm/elf.h>
  19. #include <asm/uaccess.h>
  20. #include <asm/tlbflush.h>
  21. #include "internal.h"
  22. void task_mem(struct seq_file *m, struct mm_struct *mm)
  23. {
  24. unsigned long text, lib, swap, ptes, pmds, anon, file, shmem;
  25. unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
  26. anon = get_mm_counter(mm, MM_ANONPAGES);
  27. file = get_mm_counter(mm, MM_FILEPAGES);
  28. shmem = get_mm_counter(mm, MM_SHMEMPAGES);
  29. /*
  30. * Note: to minimize their overhead, mm maintains hiwater_vm and
  31. * hiwater_rss only when about to *lower* total_vm or rss. Any
  32. * collector of these hiwater stats must therefore get total_vm
  33. * and rss too, which will usually be the higher. Barriers? not
  34. * worth the effort, such snapshots can always be inconsistent.
  35. */
  36. hiwater_vm = total_vm = mm->total_vm;
  37. if (hiwater_vm < mm->hiwater_vm)
  38. hiwater_vm = mm->hiwater_vm;
  39. hiwater_rss = total_rss = anon + file + shmem;
  40. if (hiwater_rss < mm->hiwater_rss)
  41. hiwater_rss = mm->hiwater_rss;
  42. text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
  43. lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
  44. swap = get_mm_counter(mm, MM_SWAPENTS);
  45. ptes = PTRS_PER_PTE * sizeof(pte_t) * atomic_long_read(&mm->nr_ptes);
  46. pmds = PTRS_PER_PMD * sizeof(pmd_t) * mm_nr_pmds(mm);
  47. seq_printf(m,
  48. "VmPeak:\t%8lu kB\n"
  49. "VmSize:\t%8lu kB\n"
  50. "VmLck:\t%8lu kB\n"
  51. "VmPin:\t%8lu kB\n"
  52. "VmHWM:\t%8lu kB\n"
  53. "VmRSS:\t%8lu kB\n"
  54. "RssAnon:\t%8lu kB\n"
  55. "RssFile:\t%8lu kB\n"
  56. "RssShmem:\t%8lu kB\n"
  57. "VmData:\t%8lu kB\n"
  58. "VmStk:\t%8lu kB\n"
  59. "VmExe:\t%8lu kB\n"
  60. "VmLib:\t%8lu kB\n"
  61. "VmPTE:\t%8lu kB\n"
  62. "VmPMD:\t%8lu kB\n"
  63. "VmSwap:\t%8lu kB\n",
  64. hiwater_vm << (PAGE_SHIFT-10),
  65. total_vm << (PAGE_SHIFT-10),
  66. mm->locked_vm << (PAGE_SHIFT-10),
  67. mm->pinned_vm << (PAGE_SHIFT-10),
  68. hiwater_rss << (PAGE_SHIFT-10),
  69. total_rss << (PAGE_SHIFT-10),
  70. anon << (PAGE_SHIFT-10),
  71. file << (PAGE_SHIFT-10),
  72. shmem << (PAGE_SHIFT-10),
  73. mm->data_vm << (PAGE_SHIFT-10),
  74. mm->stack_vm << (PAGE_SHIFT-10), text, lib,
  75. ptes >> 10,
  76. pmds >> 10,
  77. swap << (PAGE_SHIFT-10));
  78. hugetlb_report_usage(m, mm);
  79. }
  80. unsigned long task_vsize(struct mm_struct *mm)
  81. {
  82. return PAGE_SIZE * mm->total_vm;
  83. }
  84. unsigned long task_statm(struct mm_struct *mm,
  85. unsigned long *shared, unsigned long *text,
  86. unsigned long *data, unsigned long *resident)
  87. {
  88. *shared = get_mm_counter(mm, MM_FILEPAGES) +
  89. get_mm_counter(mm, MM_SHMEMPAGES);
  90. *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
  91. >> PAGE_SHIFT;
  92. *data = mm->data_vm + mm->stack_vm;
  93. *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
  94. return mm->total_vm;
  95. }
  96. #ifdef CONFIG_NUMA
  97. /*
  98. * Save get_task_policy() for show_numa_map().
  99. */
  100. static void hold_task_mempolicy(struct proc_maps_private *priv)
  101. {
  102. struct task_struct *task = priv->task;
  103. task_lock(task);
  104. priv->task_mempolicy = get_task_policy(task);
  105. mpol_get(priv->task_mempolicy);
  106. task_unlock(task);
  107. }
  108. static void release_task_mempolicy(struct proc_maps_private *priv)
  109. {
  110. mpol_put(priv->task_mempolicy);
  111. }
  112. #else
  113. static void hold_task_mempolicy(struct proc_maps_private *priv)
  114. {
  115. }
  116. static void release_task_mempolicy(struct proc_maps_private *priv)
  117. {
  118. }
  119. #endif
  120. static void vma_stop(struct proc_maps_private *priv)
  121. {
  122. struct mm_struct *mm = priv->mm;
  123. release_task_mempolicy(priv);
  124. up_read(&mm->mmap_sem);
  125. mmput(mm);
  126. }
  127. static struct vm_area_struct *
  128. m_next_vma(struct proc_maps_private *priv, struct vm_area_struct *vma)
  129. {
  130. if (vma == priv->tail_vma)
  131. return NULL;
  132. return vma->vm_next ?: priv->tail_vma;
  133. }
  134. static void m_cache_vma(struct seq_file *m, struct vm_area_struct *vma)
  135. {
  136. if (m->count < m->size) /* vma is copied successfully */
  137. m->version = m_next_vma(m->private, vma) ? vma->vm_end : -1UL;
  138. }
  139. static void *m_start(struct seq_file *m, loff_t *ppos)
  140. {
  141. struct proc_maps_private *priv = m->private;
  142. unsigned long last_addr = m->version;
  143. struct mm_struct *mm;
  144. struct vm_area_struct *vma;
  145. unsigned int pos = *ppos;
  146. /* See m_cache_vma(). Zero at the start or after lseek. */
  147. if (last_addr == -1UL)
  148. return NULL;
  149. priv->task = get_proc_task(priv->inode);
  150. if (!priv->task)
  151. return ERR_PTR(-ESRCH);
  152. mm = priv->mm;
  153. if (!mm || !atomic_inc_not_zero(&mm->mm_users))
  154. return NULL;
  155. down_read(&mm->mmap_sem);
  156. hold_task_mempolicy(priv);
  157. priv->tail_vma = get_gate_vma(mm);
  158. if (last_addr) {
  159. vma = find_vma(mm, last_addr - 1);
  160. if (vma && vma->vm_start <= last_addr)
  161. vma = m_next_vma(priv, vma);
  162. if (vma)
  163. return vma;
  164. }
  165. m->version = 0;
  166. if (pos < mm->map_count) {
  167. for (vma = mm->mmap; pos; pos--) {
  168. m->version = vma->vm_start;
  169. vma = vma->vm_next;
  170. }
  171. return vma;
  172. }
  173. /* we do not bother to update m->version in this case */
  174. if (pos == mm->map_count && priv->tail_vma)
  175. return priv->tail_vma;
  176. vma_stop(priv);
  177. return NULL;
  178. }
  179. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  180. {
  181. struct proc_maps_private *priv = m->private;
  182. struct vm_area_struct *next;
  183. (*pos)++;
  184. next = m_next_vma(priv, v);
  185. if (!next)
  186. vma_stop(priv);
  187. return next;
  188. }
  189. static void m_stop(struct seq_file *m, void *v)
  190. {
  191. struct proc_maps_private *priv = m->private;
  192. if (!IS_ERR_OR_NULL(v))
  193. vma_stop(priv);
  194. if (priv->task) {
  195. put_task_struct(priv->task);
  196. priv->task = NULL;
  197. }
  198. }
  199. static int proc_maps_open(struct inode *inode, struct file *file,
  200. const struct seq_operations *ops, int psize)
  201. {
  202. struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
  203. if (!priv)
  204. return -ENOMEM;
  205. priv->inode = inode;
  206. priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
  207. if (IS_ERR(priv->mm)) {
  208. int err = PTR_ERR(priv->mm);
  209. seq_release_private(inode, file);
  210. return err;
  211. }
  212. return 0;
  213. }
  214. static int proc_map_release(struct inode *inode, struct file *file)
  215. {
  216. struct seq_file *seq = file->private_data;
  217. struct proc_maps_private *priv = seq->private;
  218. if (priv->mm)
  219. mmdrop(priv->mm);
  220. return seq_release_private(inode, file);
  221. }
  222. static int do_maps_open(struct inode *inode, struct file *file,
  223. const struct seq_operations *ops)
  224. {
  225. return proc_maps_open(inode, file, ops,
  226. sizeof(struct proc_maps_private));
  227. }
  228. /*
  229. * Indicate if the VMA is a stack for the given task; for
  230. * /proc/PID/maps that is the stack of the main task.
  231. */
  232. static int is_stack(struct proc_maps_private *priv,
  233. struct vm_area_struct *vma)
  234. {
  235. /*
  236. * We make no effort to guess what a given thread considers to be
  237. * its "stack". It's not even well-defined for programs written
  238. * languages like Go.
  239. */
  240. return vma->vm_start <= vma->vm_mm->start_stack &&
  241. vma->vm_end >= vma->vm_mm->start_stack;
  242. }
  243. static void
  244. show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
  245. {
  246. struct mm_struct *mm = vma->vm_mm;
  247. struct file *file = vma->vm_file;
  248. struct proc_maps_private *priv = m->private;
  249. vm_flags_t flags = vma->vm_flags;
  250. unsigned long ino = 0;
  251. unsigned long long pgoff = 0;
  252. unsigned long start, end;
  253. dev_t dev = 0;
  254. const char *name = NULL;
  255. if (file) {
  256. struct inode *inode = file_inode(vma->vm_file);
  257. dev = inode->i_sb->s_dev;
  258. ino = inode->i_ino;
  259. pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
  260. }
  261. /* We don't show the stack guard page in /proc/maps */
  262. start = vma->vm_start;
  263. end = vma->vm_end;
  264. seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
  265. seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
  266. start,
  267. end,
  268. flags & VM_READ ? 'r' : '-',
  269. flags & VM_WRITE ? 'w' : '-',
  270. flags & VM_EXEC ? 'x' : '-',
  271. flags & VM_MAYSHARE ? 's' : 'p',
  272. pgoff,
  273. MAJOR(dev), MINOR(dev), ino);
  274. /*
  275. * Print the dentry name for named mappings, and a
  276. * special [heap] marker for the heap:
  277. */
  278. if (file) {
  279. seq_pad(m, ' ');
  280. seq_file_path(m, file, "\n");
  281. goto done;
  282. }
  283. if (vma->vm_ops && vma->vm_ops->name) {
  284. name = vma->vm_ops->name(vma);
  285. if (name)
  286. goto done;
  287. }
  288. name = arch_vma_name(vma);
  289. if (!name) {
  290. if (!mm) {
  291. name = "[vdso]";
  292. goto done;
  293. }
  294. if (vma->vm_start <= mm->brk &&
  295. vma->vm_end >= mm->start_brk) {
  296. name = "[heap]";
  297. goto done;
  298. }
  299. if (is_stack(priv, vma))
  300. name = "[stack]";
  301. }
  302. done:
  303. if (name) {
  304. seq_pad(m, ' ');
  305. seq_puts(m, name);
  306. }
  307. seq_putc(m, '\n');
  308. }
  309. static int show_map(struct seq_file *m, void *v, int is_pid)
  310. {
  311. show_map_vma(m, v, is_pid);
  312. m_cache_vma(m, v);
  313. return 0;
  314. }
  315. static int show_pid_map(struct seq_file *m, void *v)
  316. {
  317. return show_map(m, v, 1);
  318. }
  319. static int show_tid_map(struct seq_file *m, void *v)
  320. {
  321. return show_map(m, v, 0);
  322. }
  323. static const struct seq_operations proc_pid_maps_op = {
  324. .start = m_start,
  325. .next = m_next,
  326. .stop = m_stop,
  327. .show = show_pid_map
  328. };
  329. static const struct seq_operations proc_tid_maps_op = {
  330. .start = m_start,
  331. .next = m_next,
  332. .stop = m_stop,
  333. .show = show_tid_map
  334. };
  335. static int pid_maps_open(struct inode *inode, struct file *file)
  336. {
  337. return do_maps_open(inode, file, &proc_pid_maps_op);
  338. }
  339. static int tid_maps_open(struct inode *inode, struct file *file)
  340. {
  341. return do_maps_open(inode, file, &proc_tid_maps_op);
  342. }
  343. const struct file_operations proc_pid_maps_operations = {
  344. .open = pid_maps_open,
  345. .read = seq_read,
  346. .llseek = seq_lseek,
  347. .release = proc_map_release,
  348. };
  349. const struct file_operations proc_tid_maps_operations = {
  350. .open = tid_maps_open,
  351. .read = seq_read,
  352. .llseek = seq_lseek,
  353. .release = proc_map_release,
  354. };
  355. /*
  356. * Proportional Set Size(PSS): my share of RSS.
  357. *
  358. * PSS of a process is the count of pages it has in memory, where each
  359. * page is divided by the number of processes sharing it. So if a
  360. * process has 1000 pages all to itself, and 1000 shared with one other
  361. * process, its PSS will be 1500.
  362. *
  363. * To keep (accumulated) division errors low, we adopt a 64bit
  364. * fixed-point pss counter to minimize division errors. So (pss >>
  365. * PSS_SHIFT) would be the real byte count.
  366. *
  367. * A shift of 12 before division means (assuming 4K page size):
  368. * - 1M 3-user-pages add up to 8KB errors;
  369. * - supports mapcount up to 2^24, or 16M;
  370. * - supports PSS up to 2^52 bytes, or 4PB.
  371. */
  372. #define PSS_SHIFT 12
  373. #ifdef CONFIG_PROC_PAGE_MONITOR
  374. struct mem_size_stats {
  375. unsigned long resident;
  376. unsigned long shared_clean;
  377. unsigned long shared_dirty;
  378. unsigned long private_clean;
  379. unsigned long private_dirty;
  380. unsigned long referenced;
  381. unsigned long anonymous;
  382. unsigned long anonymous_thp;
  383. unsigned long shmem_thp;
  384. unsigned long swap;
  385. unsigned long shared_hugetlb;
  386. unsigned long private_hugetlb;
  387. u64 pss;
  388. u64 swap_pss;
  389. bool check_shmem_swap;
  390. };
  391. static void smaps_account(struct mem_size_stats *mss, struct page *page,
  392. bool compound, bool young, bool dirty)
  393. {
  394. int i, nr = compound ? 1 << compound_order(page) : 1;
  395. unsigned long size = nr * PAGE_SIZE;
  396. if (PageAnon(page))
  397. mss->anonymous += size;
  398. mss->resident += size;
  399. /* Accumulate the size in pages that have been accessed. */
  400. if (young || page_is_young(page) || PageReferenced(page))
  401. mss->referenced += size;
  402. /*
  403. * page_count(page) == 1 guarantees the page is mapped exactly once.
  404. * If any subpage of the compound page mapped with PTE it would elevate
  405. * page_count().
  406. */
  407. if (page_count(page) == 1) {
  408. if (dirty || PageDirty(page))
  409. mss->private_dirty += size;
  410. else
  411. mss->private_clean += size;
  412. mss->pss += (u64)size << PSS_SHIFT;
  413. return;
  414. }
  415. for (i = 0; i < nr; i++, page++) {
  416. int mapcount = page_mapcount(page);
  417. if (mapcount >= 2) {
  418. if (dirty || PageDirty(page))
  419. mss->shared_dirty += PAGE_SIZE;
  420. else
  421. mss->shared_clean += PAGE_SIZE;
  422. mss->pss += (PAGE_SIZE << PSS_SHIFT) / mapcount;
  423. } else {
  424. if (dirty || PageDirty(page))
  425. mss->private_dirty += PAGE_SIZE;
  426. else
  427. mss->private_clean += PAGE_SIZE;
  428. mss->pss += PAGE_SIZE << PSS_SHIFT;
  429. }
  430. }
  431. }
  432. #ifdef CONFIG_SHMEM
  433. static int smaps_pte_hole(unsigned long addr, unsigned long end,
  434. struct mm_walk *walk)
  435. {
  436. struct mem_size_stats *mss = walk->private;
  437. mss->swap += shmem_partial_swap_usage(
  438. walk->vma->vm_file->f_mapping, addr, end);
  439. return 0;
  440. }
  441. #endif
  442. static void smaps_pte_entry(pte_t *pte, unsigned long addr,
  443. struct mm_walk *walk)
  444. {
  445. struct mem_size_stats *mss = walk->private;
  446. struct vm_area_struct *vma = walk->vma;
  447. struct page *page = NULL;
  448. if (pte_present(*pte)) {
  449. page = vm_normal_page(vma, addr, *pte);
  450. } else if (is_swap_pte(*pte)) {
  451. swp_entry_t swpent = pte_to_swp_entry(*pte);
  452. if (!non_swap_entry(swpent)) {
  453. int mapcount;
  454. mss->swap += PAGE_SIZE;
  455. mapcount = swp_swapcount(swpent);
  456. if (mapcount >= 2) {
  457. u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
  458. do_div(pss_delta, mapcount);
  459. mss->swap_pss += pss_delta;
  460. } else {
  461. mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
  462. }
  463. } else if (is_migration_entry(swpent))
  464. page = migration_entry_to_page(swpent);
  465. } else if (unlikely(IS_ENABLED(CONFIG_SHMEM) && mss->check_shmem_swap
  466. && pte_none(*pte))) {
  467. page = find_get_entry(vma->vm_file->f_mapping,
  468. linear_page_index(vma, addr));
  469. if (!page)
  470. return;
  471. if (radix_tree_exceptional_entry(page))
  472. mss->swap += PAGE_SIZE;
  473. else
  474. put_page(page);
  475. return;
  476. }
  477. if (!page)
  478. return;
  479. smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte));
  480. }
  481. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  482. static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
  483. struct mm_walk *walk)
  484. {
  485. struct mem_size_stats *mss = walk->private;
  486. struct vm_area_struct *vma = walk->vma;
  487. struct page *page;
  488. /* FOLL_DUMP will return -EFAULT on huge zero page */
  489. page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
  490. if (IS_ERR_OR_NULL(page))
  491. return;
  492. if (PageAnon(page))
  493. mss->anonymous_thp += HPAGE_PMD_SIZE;
  494. else if (PageSwapBacked(page))
  495. mss->shmem_thp += HPAGE_PMD_SIZE;
  496. else if (is_zone_device_page(page))
  497. /* pass */;
  498. else
  499. VM_BUG_ON_PAGE(1, page);
  500. smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd));
  501. }
  502. #else
  503. static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
  504. struct mm_walk *walk)
  505. {
  506. }
  507. #endif
  508. static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
  509. struct mm_walk *walk)
  510. {
  511. struct vm_area_struct *vma = walk->vma;
  512. pte_t *pte;
  513. spinlock_t *ptl;
  514. ptl = pmd_trans_huge_lock(pmd, vma);
  515. if (ptl) {
  516. smaps_pmd_entry(pmd, addr, walk);
  517. spin_unlock(ptl);
  518. return 0;
  519. }
  520. if (pmd_trans_unstable(pmd))
  521. return 0;
  522. /*
  523. * The mmap_sem held all the way back in m_start() is what
  524. * keeps khugepaged out of here and from collapsing things
  525. * in here.
  526. */
  527. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  528. for (; addr != end; pte++, addr += PAGE_SIZE)
  529. smaps_pte_entry(pte, addr, walk);
  530. pte_unmap_unlock(pte - 1, ptl);
  531. cond_resched();
  532. return 0;
  533. }
  534. static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
  535. {
  536. /*
  537. * Don't forget to update Documentation/ on changes.
  538. */
  539. static const char mnemonics[BITS_PER_LONG][2] = {
  540. /*
  541. * In case if we meet a flag we don't know about.
  542. */
  543. [0 ... (BITS_PER_LONG-1)] = "??",
  544. [ilog2(VM_READ)] = "rd",
  545. [ilog2(VM_WRITE)] = "wr",
  546. [ilog2(VM_EXEC)] = "ex",
  547. [ilog2(VM_SHARED)] = "sh",
  548. [ilog2(VM_MAYREAD)] = "mr",
  549. [ilog2(VM_MAYWRITE)] = "mw",
  550. [ilog2(VM_MAYEXEC)] = "me",
  551. [ilog2(VM_MAYSHARE)] = "ms",
  552. [ilog2(VM_GROWSDOWN)] = "gd",
  553. [ilog2(VM_PFNMAP)] = "pf",
  554. [ilog2(VM_DENYWRITE)] = "dw",
  555. #ifdef CONFIG_X86_INTEL_MPX
  556. [ilog2(VM_MPX)] = "mp",
  557. #endif
  558. [ilog2(VM_LOCKED)] = "lo",
  559. [ilog2(VM_IO)] = "io",
  560. [ilog2(VM_SEQ_READ)] = "sr",
  561. [ilog2(VM_RAND_READ)] = "rr",
  562. [ilog2(VM_DONTCOPY)] = "dc",
  563. [ilog2(VM_DONTEXPAND)] = "de",
  564. [ilog2(VM_ACCOUNT)] = "ac",
  565. [ilog2(VM_NORESERVE)] = "nr",
  566. [ilog2(VM_HUGETLB)] = "ht",
  567. [ilog2(VM_ARCH_1)] = "ar",
  568. [ilog2(VM_DONTDUMP)] = "dd",
  569. #ifdef CONFIG_MEM_SOFT_DIRTY
  570. [ilog2(VM_SOFTDIRTY)] = "sd",
  571. #endif
  572. [ilog2(VM_MIXEDMAP)] = "mm",
  573. [ilog2(VM_HUGEPAGE)] = "hg",
  574. [ilog2(VM_NOHUGEPAGE)] = "nh",
  575. [ilog2(VM_MERGEABLE)] = "mg",
  576. [ilog2(VM_UFFD_MISSING)]= "um",
  577. [ilog2(VM_UFFD_WP)] = "uw",
  578. #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
  579. /* These come out via ProtectionKey: */
  580. [ilog2(VM_PKEY_BIT0)] = "",
  581. [ilog2(VM_PKEY_BIT1)] = "",
  582. [ilog2(VM_PKEY_BIT2)] = "",
  583. [ilog2(VM_PKEY_BIT3)] = "",
  584. #endif
  585. };
  586. size_t i;
  587. seq_puts(m, "VmFlags: ");
  588. for (i = 0; i < BITS_PER_LONG; i++) {
  589. if (!mnemonics[i][0])
  590. continue;
  591. if (vma->vm_flags & (1UL << i)) {
  592. seq_printf(m, "%c%c ",
  593. mnemonics[i][0], mnemonics[i][1]);
  594. }
  595. }
  596. seq_putc(m, '\n');
  597. }
  598. #ifdef CONFIG_HUGETLB_PAGE
  599. static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
  600. unsigned long addr, unsigned long end,
  601. struct mm_walk *walk)
  602. {
  603. struct mem_size_stats *mss = walk->private;
  604. struct vm_area_struct *vma = walk->vma;
  605. struct page *page = NULL;
  606. if (pte_present(*pte)) {
  607. page = vm_normal_page(vma, addr, *pte);
  608. } else if (is_swap_pte(*pte)) {
  609. swp_entry_t swpent = pte_to_swp_entry(*pte);
  610. if (is_migration_entry(swpent))
  611. page = migration_entry_to_page(swpent);
  612. }
  613. if (page) {
  614. int mapcount = page_mapcount(page);
  615. if (mapcount >= 2)
  616. mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
  617. else
  618. mss->private_hugetlb += huge_page_size(hstate_vma(vma));
  619. }
  620. return 0;
  621. }
  622. #endif /* HUGETLB_PAGE */
  623. void __weak arch_show_smap(struct seq_file *m, struct vm_area_struct *vma)
  624. {
  625. }
  626. static int show_smap(struct seq_file *m, void *v, int is_pid)
  627. {
  628. struct vm_area_struct *vma = v;
  629. struct mem_size_stats mss;
  630. struct mm_walk smaps_walk = {
  631. .pmd_entry = smaps_pte_range,
  632. #ifdef CONFIG_HUGETLB_PAGE
  633. .hugetlb_entry = smaps_hugetlb_range,
  634. #endif
  635. .mm = vma->vm_mm,
  636. .private = &mss,
  637. };
  638. memset(&mss, 0, sizeof mss);
  639. #ifdef CONFIG_SHMEM
  640. if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) {
  641. /*
  642. * For shared or readonly shmem mappings we know that all
  643. * swapped out pages belong to the shmem object, and we can
  644. * obtain the swap value much more efficiently. For private
  645. * writable mappings, we might have COW pages that are
  646. * not affected by the parent swapped out pages of the shmem
  647. * object, so we have to distinguish them during the page walk.
  648. * Unless we know that the shmem object (or the part mapped by
  649. * our VMA) has no swapped out pages at all.
  650. */
  651. unsigned long shmem_swapped = shmem_swap_usage(vma);
  652. if (!shmem_swapped || (vma->vm_flags & VM_SHARED) ||
  653. !(vma->vm_flags & VM_WRITE)) {
  654. mss.swap = shmem_swapped;
  655. } else {
  656. mss.check_shmem_swap = true;
  657. smaps_walk.pte_hole = smaps_pte_hole;
  658. }
  659. }
  660. #endif
  661. /* mmap_sem is held in m_start */
  662. walk_page_vma(vma, &smaps_walk);
  663. show_map_vma(m, vma, is_pid);
  664. seq_printf(m,
  665. "Size: %8lu kB\n"
  666. "Rss: %8lu kB\n"
  667. "Pss: %8lu kB\n"
  668. "Shared_Clean: %8lu kB\n"
  669. "Shared_Dirty: %8lu kB\n"
  670. "Private_Clean: %8lu kB\n"
  671. "Private_Dirty: %8lu kB\n"
  672. "Referenced: %8lu kB\n"
  673. "Anonymous: %8lu kB\n"
  674. "AnonHugePages: %8lu kB\n"
  675. "ShmemPmdMapped: %8lu kB\n"
  676. "Shared_Hugetlb: %8lu kB\n"
  677. "Private_Hugetlb: %7lu kB\n"
  678. "Swap: %8lu kB\n"
  679. "SwapPss: %8lu kB\n"
  680. "KernelPageSize: %8lu kB\n"
  681. "MMUPageSize: %8lu kB\n"
  682. "Locked: %8lu kB\n",
  683. (vma->vm_end - vma->vm_start) >> 10,
  684. mss.resident >> 10,
  685. (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
  686. mss.shared_clean >> 10,
  687. mss.shared_dirty >> 10,
  688. mss.private_clean >> 10,
  689. mss.private_dirty >> 10,
  690. mss.referenced >> 10,
  691. mss.anonymous >> 10,
  692. mss.anonymous_thp >> 10,
  693. mss.shmem_thp >> 10,
  694. mss.shared_hugetlb >> 10,
  695. mss.private_hugetlb >> 10,
  696. mss.swap >> 10,
  697. (unsigned long)(mss.swap_pss >> (10 + PSS_SHIFT)),
  698. vma_kernel_pagesize(vma) >> 10,
  699. vma_mmu_pagesize(vma) >> 10,
  700. (vma->vm_flags & VM_LOCKED) ?
  701. (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
  702. arch_show_smap(m, vma);
  703. show_smap_vma_flags(m, vma);
  704. m_cache_vma(m, vma);
  705. return 0;
  706. }
  707. static int show_pid_smap(struct seq_file *m, void *v)
  708. {
  709. return show_smap(m, v, 1);
  710. }
  711. static int show_tid_smap(struct seq_file *m, void *v)
  712. {
  713. return show_smap(m, v, 0);
  714. }
  715. static const struct seq_operations proc_pid_smaps_op = {
  716. .start = m_start,
  717. .next = m_next,
  718. .stop = m_stop,
  719. .show = show_pid_smap
  720. };
  721. static const struct seq_operations proc_tid_smaps_op = {
  722. .start = m_start,
  723. .next = m_next,
  724. .stop = m_stop,
  725. .show = show_tid_smap
  726. };
  727. static int pid_smaps_open(struct inode *inode, struct file *file)
  728. {
  729. return do_maps_open(inode, file, &proc_pid_smaps_op);
  730. }
  731. static int tid_smaps_open(struct inode *inode, struct file *file)
  732. {
  733. return do_maps_open(inode, file, &proc_tid_smaps_op);
  734. }
  735. const struct file_operations proc_pid_smaps_operations = {
  736. .open = pid_smaps_open,
  737. .read = seq_read,
  738. .llseek = seq_lseek,
  739. .release = proc_map_release,
  740. };
  741. const struct file_operations proc_tid_smaps_operations = {
  742. .open = tid_smaps_open,
  743. .read = seq_read,
  744. .llseek = seq_lseek,
  745. .release = proc_map_release,
  746. };
  747. enum clear_refs_types {
  748. CLEAR_REFS_ALL = 1,
  749. CLEAR_REFS_ANON,
  750. CLEAR_REFS_MAPPED,
  751. CLEAR_REFS_SOFT_DIRTY,
  752. CLEAR_REFS_MM_HIWATER_RSS,
  753. CLEAR_REFS_LAST,
  754. };
  755. struct clear_refs_private {
  756. enum clear_refs_types type;
  757. };
  758. #ifdef CONFIG_MEM_SOFT_DIRTY
  759. static inline void clear_soft_dirty(struct vm_area_struct *vma,
  760. unsigned long addr, pte_t *pte)
  761. {
  762. /*
  763. * The soft-dirty tracker uses #PF-s to catch writes
  764. * to pages, so write-protect the pte as well. See the
  765. * Documentation/vm/soft-dirty.txt for full description
  766. * of how soft-dirty works.
  767. */
  768. pte_t ptent = *pte;
  769. if (pte_present(ptent)) {
  770. ptent = ptep_modify_prot_start(vma->vm_mm, addr, pte);
  771. ptent = pte_wrprotect(ptent);
  772. ptent = pte_clear_soft_dirty(ptent);
  773. ptep_modify_prot_commit(vma->vm_mm, addr, pte, ptent);
  774. } else if (is_swap_pte(ptent)) {
  775. ptent = pte_swp_clear_soft_dirty(ptent);
  776. set_pte_at(vma->vm_mm, addr, pte, ptent);
  777. }
  778. }
  779. #else
  780. static inline void clear_soft_dirty(struct vm_area_struct *vma,
  781. unsigned long addr, pte_t *pte)
  782. {
  783. }
  784. #endif
  785. #if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
  786. static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
  787. unsigned long addr, pmd_t *pmdp)
  788. {
  789. pmd_t pmd = *pmdp;
  790. /* See comment in change_huge_pmd() */
  791. pmdp_invalidate(vma, addr, pmdp);
  792. if (pmd_dirty(*pmdp))
  793. pmd = pmd_mkdirty(pmd);
  794. if (pmd_young(*pmdp))
  795. pmd = pmd_mkyoung(pmd);
  796. pmd = pmd_wrprotect(pmd);
  797. pmd = pmd_clear_soft_dirty(pmd);
  798. set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
  799. }
  800. #else
  801. static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
  802. unsigned long addr, pmd_t *pmdp)
  803. {
  804. }
  805. #endif
  806. static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
  807. unsigned long end, struct mm_walk *walk)
  808. {
  809. struct clear_refs_private *cp = walk->private;
  810. struct vm_area_struct *vma = walk->vma;
  811. pte_t *pte, ptent;
  812. spinlock_t *ptl;
  813. struct page *page;
  814. ptl = pmd_trans_huge_lock(pmd, vma);
  815. if (ptl) {
  816. if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
  817. clear_soft_dirty_pmd(vma, addr, pmd);
  818. goto out;
  819. }
  820. page = pmd_page(*pmd);
  821. /* Clear accessed and referenced bits. */
  822. pmdp_test_and_clear_young(vma, addr, pmd);
  823. test_and_clear_page_young(page);
  824. ClearPageReferenced(page);
  825. out:
  826. spin_unlock(ptl);
  827. return 0;
  828. }
  829. if (pmd_trans_unstable(pmd))
  830. return 0;
  831. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  832. for (; addr != end; pte++, addr += PAGE_SIZE) {
  833. ptent = *pte;
  834. if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
  835. clear_soft_dirty(vma, addr, pte);
  836. continue;
  837. }
  838. if (!pte_present(ptent))
  839. continue;
  840. page = vm_normal_page(vma, addr, ptent);
  841. if (!page)
  842. continue;
  843. /* Clear accessed and referenced bits. */
  844. ptep_test_and_clear_young(vma, addr, pte);
  845. test_and_clear_page_young(page);
  846. ClearPageReferenced(page);
  847. }
  848. pte_unmap_unlock(pte - 1, ptl);
  849. cond_resched();
  850. return 0;
  851. }
  852. static int clear_refs_test_walk(unsigned long start, unsigned long end,
  853. struct mm_walk *walk)
  854. {
  855. struct clear_refs_private *cp = walk->private;
  856. struct vm_area_struct *vma = walk->vma;
  857. if (vma->vm_flags & VM_PFNMAP)
  858. return 1;
  859. /*
  860. * Writing 1 to /proc/pid/clear_refs affects all pages.
  861. * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
  862. * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
  863. * Writing 4 to /proc/pid/clear_refs affects all pages.
  864. */
  865. if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
  866. return 1;
  867. if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
  868. return 1;
  869. return 0;
  870. }
  871. static ssize_t clear_refs_write(struct file *file, const char __user *buf,
  872. size_t count, loff_t *ppos)
  873. {
  874. struct task_struct *task;
  875. char buffer[PROC_NUMBUF];
  876. struct mm_struct *mm;
  877. struct vm_area_struct *vma;
  878. enum clear_refs_types type;
  879. int itype;
  880. int rv;
  881. memset(buffer, 0, sizeof(buffer));
  882. if (count > sizeof(buffer) - 1)
  883. count = sizeof(buffer) - 1;
  884. if (copy_from_user(buffer, buf, count))
  885. return -EFAULT;
  886. rv = kstrtoint(strstrip(buffer), 10, &itype);
  887. if (rv < 0)
  888. return rv;
  889. type = (enum clear_refs_types)itype;
  890. if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
  891. return -EINVAL;
  892. task = get_proc_task(file_inode(file));
  893. if (!task)
  894. return -ESRCH;
  895. mm = get_task_mm(task);
  896. if (mm) {
  897. struct clear_refs_private cp = {
  898. .type = type,
  899. };
  900. struct mm_walk clear_refs_walk = {
  901. .pmd_entry = clear_refs_pte_range,
  902. .test_walk = clear_refs_test_walk,
  903. .mm = mm,
  904. .private = &cp,
  905. };
  906. if (type == CLEAR_REFS_MM_HIWATER_RSS) {
  907. if (down_write_killable(&mm->mmap_sem)) {
  908. count = -EINTR;
  909. goto out_mm;
  910. }
  911. /*
  912. * Writing 5 to /proc/pid/clear_refs resets the peak
  913. * resident set size to this mm's current rss value.
  914. */
  915. reset_mm_hiwater_rss(mm);
  916. up_write(&mm->mmap_sem);
  917. goto out_mm;
  918. }
  919. down_read(&mm->mmap_sem);
  920. if (type == CLEAR_REFS_SOFT_DIRTY) {
  921. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  922. if (!(vma->vm_flags & VM_SOFTDIRTY))
  923. continue;
  924. up_read(&mm->mmap_sem);
  925. if (down_write_killable(&mm->mmap_sem)) {
  926. count = -EINTR;
  927. goto out_mm;
  928. }
  929. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  930. vma->vm_flags &= ~VM_SOFTDIRTY;
  931. vma_set_page_prot(vma);
  932. }
  933. downgrade_write(&mm->mmap_sem);
  934. break;
  935. }
  936. mmu_notifier_invalidate_range_start(mm, 0, -1);
  937. }
  938. walk_page_range(0, mm->highest_vm_end, &clear_refs_walk);
  939. if (type == CLEAR_REFS_SOFT_DIRTY)
  940. mmu_notifier_invalidate_range_end(mm, 0, -1);
  941. flush_tlb_mm(mm);
  942. up_read(&mm->mmap_sem);
  943. out_mm:
  944. mmput(mm);
  945. }
  946. put_task_struct(task);
  947. return count;
  948. }
  949. const struct file_operations proc_clear_refs_operations = {
  950. .write = clear_refs_write,
  951. .llseek = noop_llseek,
  952. };
  953. typedef struct {
  954. u64 pme;
  955. } pagemap_entry_t;
  956. struct pagemapread {
  957. int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
  958. pagemap_entry_t *buffer;
  959. bool show_pfn;
  960. };
  961. #define PAGEMAP_WALK_SIZE (PMD_SIZE)
  962. #define PAGEMAP_WALK_MASK (PMD_MASK)
  963. #define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
  964. #define PM_PFRAME_BITS 55
  965. #define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
  966. #define PM_SOFT_DIRTY BIT_ULL(55)
  967. #define PM_MMAP_EXCLUSIVE BIT_ULL(56)
  968. #define PM_FILE BIT_ULL(61)
  969. #define PM_SWAP BIT_ULL(62)
  970. #define PM_PRESENT BIT_ULL(63)
  971. #define PM_END_OF_BUFFER 1
  972. static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
  973. {
  974. return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
  975. }
  976. static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
  977. struct pagemapread *pm)
  978. {
  979. pm->buffer[pm->pos++] = *pme;
  980. if (pm->pos >= pm->len)
  981. return PM_END_OF_BUFFER;
  982. return 0;
  983. }
  984. static int pagemap_pte_hole(unsigned long start, unsigned long end,
  985. struct mm_walk *walk)
  986. {
  987. struct pagemapread *pm = walk->private;
  988. unsigned long addr = start;
  989. int err = 0;
  990. while (addr < end) {
  991. struct vm_area_struct *vma = find_vma(walk->mm, addr);
  992. pagemap_entry_t pme = make_pme(0, 0);
  993. /* End of address space hole, which we mark as non-present. */
  994. unsigned long hole_end;
  995. if (vma)
  996. hole_end = min(end, vma->vm_start);
  997. else
  998. hole_end = end;
  999. for (; addr < hole_end; addr += PAGE_SIZE) {
  1000. err = add_to_pagemap(addr, &pme, pm);
  1001. if (err)
  1002. goto out;
  1003. }
  1004. if (!vma)
  1005. break;
  1006. /* Addresses in the VMA. */
  1007. if (vma->vm_flags & VM_SOFTDIRTY)
  1008. pme = make_pme(0, PM_SOFT_DIRTY);
  1009. for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
  1010. err = add_to_pagemap(addr, &pme, pm);
  1011. if (err)
  1012. goto out;
  1013. }
  1014. }
  1015. out:
  1016. return err;
  1017. }
  1018. static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
  1019. struct vm_area_struct *vma, unsigned long addr, pte_t pte)
  1020. {
  1021. u64 frame = 0, flags = 0;
  1022. struct page *page = NULL;
  1023. if (pte_present(pte)) {
  1024. if (pm->show_pfn)
  1025. frame = pte_pfn(pte);
  1026. flags |= PM_PRESENT;
  1027. page = vm_normal_page(vma, addr, pte);
  1028. if (pte_soft_dirty(pte))
  1029. flags |= PM_SOFT_DIRTY;
  1030. } else if (is_swap_pte(pte)) {
  1031. swp_entry_t entry;
  1032. if (pte_swp_soft_dirty(pte))
  1033. flags |= PM_SOFT_DIRTY;
  1034. entry = pte_to_swp_entry(pte);
  1035. frame = swp_type(entry) |
  1036. (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
  1037. flags |= PM_SWAP;
  1038. if (is_migration_entry(entry))
  1039. page = migration_entry_to_page(entry);
  1040. }
  1041. if (page && !PageAnon(page))
  1042. flags |= PM_FILE;
  1043. if (page && page_mapcount(page) == 1)
  1044. flags |= PM_MMAP_EXCLUSIVE;
  1045. if (vma->vm_flags & VM_SOFTDIRTY)
  1046. flags |= PM_SOFT_DIRTY;
  1047. return make_pme(frame, flags);
  1048. }
  1049. static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
  1050. struct mm_walk *walk)
  1051. {
  1052. struct vm_area_struct *vma = walk->vma;
  1053. struct pagemapread *pm = walk->private;
  1054. spinlock_t *ptl;
  1055. pte_t *pte, *orig_pte;
  1056. int err = 0;
  1057. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1058. ptl = pmd_trans_huge_lock(pmdp, vma);
  1059. if (ptl) {
  1060. u64 flags = 0, frame = 0;
  1061. pmd_t pmd = *pmdp;
  1062. if ((vma->vm_flags & VM_SOFTDIRTY) || pmd_soft_dirty(pmd))
  1063. flags |= PM_SOFT_DIRTY;
  1064. /*
  1065. * Currently pmd for thp is always present because thp
  1066. * can not be swapped-out, migrated, or HWPOISONed
  1067. * (split in such cases instead.)
  1068. * This if-check is just to prepare for future implementation.
  1069. */
  1070. if (pmd_present(pmd)) {
  1071. struct page *page = pmd_page(pmd);
  1072. if (page_mapcount(page) == 1)
  1073. flags |= PM_MMAP_EXCLUSIVE;
  1074. flags |= PM_PRESENT;
  1075. if (pm->show_pfn)
  1076. frame = pmd_pfn(pmd) +
  1077. ((addr & ~PMD_MASK) >> PAGE_SHIFT);
  1078. }
  1079. for (; addr != end; addr += PAGE_SIZE) {
  1080. pagemap_entry_t pme = make_pme(frame, flags);
  1081. err = add_to_pagemap(addr, &pme, pm);
  1082. if (err)
  1083. break;
  1084. if (pm->show_pfn && (flags & PM_PRESENT))
  1085. frame++;
  1086. }
  1087. spin_unlock(ptl);
  1088. return err;
  1089. }
  1090. if (pmd_trans_unstable(pmdp))
  1091. return 0;
  1092. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  1093. /*
  1094. * We can assume that @vma always points to a valid one and @end never
  1095. * goes beyond vma->vm_end.
  1096. */
  1097. orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
  1098. for (; addr < end; pte++, addr += PAGE_SIZE) {
  1099. pagemap_entry_t pme;
  1100. pme = pte_to_pagemap_entry(pm, vma, addr, *pte);
  1101. err = add_to_pagemap(addr, &pme, pm);
  1102. if (err)
  1103. break;
  1104. }
  1105. pte_unmap_unlock(orig_pte, ptl);
  1106. cond_resched();
  1107. return err;
  1108. }
  1109. #ifdef CONFIG_HUGETLB_PAGE
  1110. /* This function walks within one hugetlb entry in the single call */
  1111. static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
  1112. unsigned long addr, unsigned long end,
  1113. struct mm_walk *walk)
  1114. {
  1115. struct pagemapread *pm = walk->private;
  1116. struct vm_area_struct *vma = walk->vma;
  1117. u64 flags = 0, frame = 0;
  1118. int err = 0;
  1119. pte_t pte;
  1120. if (vma->vm_flags & VM_SOFTDIRTY)
  1121. flags |= PM_SOFT_DIRTY;
  1122. pte = huge_ptep_get(ptep);
  1123. if (pte_present(pte)) {
  1124. struct page *page = pte_page(pte);
  1125. if (!PageAnon(page))
  1126. flags |= PM_FILE;
  1127. if (page_mapcount(page) == 1)
  1128. flags |= PM_MMAP_EXCLUSIVE;
  1129. flags |= PM_PRESENT;
  1130. if (pm->show_pfn)
  1131. frame = pte_pfn(pte) +
  1132. ((addr & ~hmask) >> PAGE_SHIFT);
  1133. }
  1134. for (; addr != end; addr += PAGE_SIZE) {
  1135. pagemap_entry_t pme = make_pme(frame, flags);
  1136. err = add_to_pagemap(addr, &pme, pm);
  1137. if (err)
  1138. return err;
  1139. if (pm->show_pfn && (flags & PM_PRESENT))
  1140. frame++;
  1141. }
  1142. cond_resched();
  1143. return err;
  1144. }
  1145. #endif /* HUGETLB_PAGE */
  1146. /*
  1147. * /proc/pid/pagemap - an array mapping virtual pages to pfns
  1148. *
  1149. * For each page in the address space, this file contains one 64-bit entry
  1150. * consisting of the following:
  1151. *
  1152. * Bits 0-54 page frame number (PFN) if present
  1153. * Bits 0-4 swap type if swapped
  1154. * Bits 5-54 swap offset if swapped
  1155. * Bit 55 pte is soft-dirty (see Documentation/vm/soft-dirty.txt)
  1156. * Bit 56 page exclusively mapped
  1157. * Bits 57-60 zero
  1158. * Bit 61 page is file-page or shared-anon
  1159. * Bit 62 page swapped
  1160. * Bit 63 page present
  1161. *
  1162. * If the page is not present but in swap, then the PFN contains an
  1163. * encoding of the swap file number and the page's offset into the
  1164. * swap. Unmapped pages return a null PFN. This allows determining
  1165. * precisely which pages are mapped (or in swap) and comparing mapped
  1166. * pages between processes.
  1167. *
  1168. * Efficient users of this interface will use /proc/pid/maps to
  1169. * determine which areas of memory are actually mapped and llseek to
  1170. * skip over unmapped regions.
  1171. */
  1172. static ssize_t pagemap_read(struct file *file, char __user *buf,
  1173. size_t count, loff_t *ppos)
  1174. {
  1175. struct mm_struct *mm = file->private_data;
  1176. struct pagemapread pm;
  1177. struct mm_walk pagemap_walk = {};
  1178. unsigned long src;
  1179. unsigned long svpfn;
  1180. unsigned long start_vaddr;
  1181. unsigned long end_vaddr;
  1182. int ret = 0, copied = 0;
  1183. if (!mm || !atomic_inc_not_zero(&mm->mm_users))
  1184. goto out;
  1185. ret = -EINVAL;
  1186. /* file position must be aligned */
  1187. if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
  1188. goto out_mm;
  1189. ret = 0;
  1190. if (!count)
  1191. goto out_mm;
  1192. /* do not disclose physical addresses: attack vector */
  1193. pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
  1194. pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
  1195. pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
  1196. ret = -ENOMEM;
  1197. if (!pm.buffer)
  1198. goto out_mm;
  1199. pagemap_walk.pmd_entry = pagemap_pmd_range;
  1200. pagemap_walk.pte_hole = pagemap_pte_hole;
  1201. #ifdef CONFIG_HUGETLB_PAGE
  1202. pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
  1203. #endif
  1204. pagemap_walk.mm = mm;
  1205. pagemap_walk.private = &pm;
  1206. src = *ppos;
  1207. svpfn = src / PM_ENTRY_BYTES;
  1208. start_vaddr = svpfn << PAGE_SHIFT;
  1209. end_vaddr = mm->task_size;
  1210. /* watch out for wraparound */
  1211. if (svpfn > mm->task_size >> PAGE_SHIFT)
  1212. start_vaddr = end_vaddr;
  1213. /*
  1214. * The odds are that this will stop walking way
  1215. * before end_vaddr, because the length of the
  1216. * user buffer is tracked in "pm", and the walk
  1217. * will stop when we hit the end of the buffer.
  1218. */
  1219. ret = 0;
  1220. while (count && (start_vaddr < end_vaddr)) {
  1221. int len;
  1222. unsigned long end;
  1223. pm.pos = 0;
  1224. end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
  1225. /* overflow ? */
  1226. if (end < start_vaddr || end > end_vaddr)
  1227. end = end_vaddr;
  1228. down_read(&mm->mmap_sem);
  1229. ret = walk_page_range(start_vaddr, end, &pagemap_walk);
  1230. up_read(&mm->mmap_sem);
  1231. start_vaddr = end;
  1232. len = min(count, PM_ENTRY_BYTES * pm.pos);
  1233. if (copy_to_user(buf, pm.buffer, len)) {
  1234. ret = -EFAULT;
  1235. goto out_free;
  1236. }
  1237. copied += len;
  1238. buf += len;
  1239. count -= len;
  1240. }
  1241. *ppos += copied;
  1242. if (!ret || ret == PM_END_OF_BUFFER)
  1243. ret = copied;
  1244. out_free:
  1245. kfree(pm.buffer);
  1246. out_mm:
  1247. mmput(mm);
  1248. out:
  1249. return ret;
  1250. }
  1251. static int pagemap_open(struct inode *inode, struct file *file)
  1252. {
  1253. struct mm_struct *mm;
  1254. mm = proc_mem_open(inode, PTRACE_MODE_READ);
  1255. if (IS_ERR(mm))
  1256. return PTR_ERR(mm);
  1257. file->private_data = mm;
  1258. return 0;
  1259. }
  1260. static int pagemap_release(struct inode *inode, struct file *file)
  1261. {
  1262. struct mm_struct *mm = file->private_data;
  1263. if (mm)
  1264. mmdrop(mm);
  1265. return 0;
  1266. }
  1267. const struct file_operations proc_pagemap_operations = {
  1268. .llseek = mem_lseek, /* borrow this */
  1269. .read = pagemap_read,
  1270. .open = pagemap_open,
  1271. .release = pagemap_release,
  1272. };
  1273. #endif /* CONFIG_PROC_PAGE_MONITOR */
  1274. #ifdef CONFIG_NUMA
  1275. struct numa_maps {
  1276. unsigned long pages;
  1277. unsigned long anon;
  1278. unsigned long active;
  1279. unsigned long writeback;
  1280. unsigned long mapcount_max;
  1281. unsigned long dirty;
  1282. unsigned long swapcache;
  1283. unsigned long node[MAX_NUMNODES];
  1284. };
  1285. struct numa_maps_private {
  1286. struct proc_maps_private proc_maps;
  1287. struct numa_maps md;
  1288. };
  1289. static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
  1290. unsigned long nr_pages)
  1291. {
  1292. int count = page_mapcount(page);
  1293. md->pages += nr_pages;
  1294. if (pte_dirty || PageDirty(page))
  1295. md->dirty += nr_pages;
  1296. if (PageSwapCache(page))
  1297. md->swapcache += nr_pages;
  1298. if (PageActive(page) || PageUnevictable(page))
  1299. md->active += nr_pages;
  1300. if (PageWriteback(page))
  1301. md->writeback += nr_pages;
  1302. if (PageAnon(page))
  1303. md->anon += nr_pages;
  1304. if (count > md->mapcount_max)
  1305. md->mapcount_max = count;
  1306. md->node[page_to_nid(page)] += nr_pages;
  1307. }
  1308. static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
  1309. unsigned long addr)
  1310. {
  1311. struct page *page;
  1312. int nid;
  1313. if (!pte_present(pte))
  1314. return NULL;
  1315. page = vm_normal_page(vma, addr, pte);
  1316. if (!page)
  1317. return NULL;
  1318. if (PageReserved(page))
  1319. return NULL;
  1320. nid = page_to_nid(page);
  1321. if (!node_isset(nid, node_states[N_MEMORY]))
  1322. return NULL;
  1323. return page;
  1324. }
  1325. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1326. static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
  1327. struct vm_area_struct *vma,
  1328. unsigned long addr)
  1329. {
  1330. struct page *page;
  1331. int nid;
  1332. if (!pmd_present(pmd))
  1333. return NULL;
  1334. page = vm_normal_page_pmd(vma, addr, pmd);
  1335. if (!page)
  1336. return NULL;
  1337. if (PageReserved(page))
  1338. return NULL;
  1339. nid = page_to_nid(page);
  1340. if (!node_isset(nid, node_states[N_MEMORY]))
  1341. return NULL;
  1342. return page;
  1343. }
  1344. #endif
  1345. static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
  1346. unsigned long end, struct mm_walk *walk)
  1347. {
  1348. struct numa_maps *md = walk->private;
  1349. struct vm_area_struct *vma = walk->vma;
  1350. spinlock_t *ptl;
  1351. pte_t *orig_pte;
  1352. pte_t *pte;
  1353. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1354. ptl = pmd_trans_huge_lock(pmd, vma);
  1355. if (ptl) {
  1356. struct page *page;
  1357. page = can_gather_numa_stats_pmd(*pmd, vma, addr);
  1358. if (page)
  1359. gather_stats(page, md, pmd_dirty(*pmd),
  1360. HPAGE_PMD_SIZE/PAGE_SIZE);
  1361. spin_unlock(ptl);
  1362. return 0;
  1363. }
  1364. if (pmd_trans_unstable(pmd))
  1365. return 0;
  1366. #endif
  1367. orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
  1368. do {
  1369. struct page *page = can_gather_numa_stats(*pte, vma, addr);
  1370. if (!page)
  1371. continue;
  1372. gather_stats(page, md, pte_dirty(*pte), 1);
  1373. } while (pte++, addr += PAGE_SIZE, addr != end);
  1374. pte_unmap_unlock(orig_pte, ptl);
  1375. return 0;
  1376. }
  1377. #ifdef CONFIG_HUGETLB_PAGE
  1378. static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
  1379. unsigned long addr, unsigned long end, struct mm_walk *walk)
  1380. {
  1381. pte_t huge_pte = huge_ptep_get(pte);
  1382. struct numa_maps *md;
  1383. struct page *page;
  1384. if (!pte_present(huge_pte))
  1385. return 0;
  1386. page = pte_page(huge_pte);
  1387. if (!page)
  1388. return 0;
  1389. md = walk->private;
  1390. gather_stats(page, md, pte_dirty(huge_pte), 1);
  1391. return 0;
  1392. }
  1393. #else
  1394. static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
  1395. unsigned long addr, unsigned long end, struct mm_walk *walk)
  1396. {
  1397. return 0;
  1398. }
  1399. #endif
  1400. /*
  1401. * Display pages allocated per node and memory policy via /proc.
  1402. */
  1403. static int show_numa_map(struct seq_file *m, void *v, int is_pid)
  1404. {
  1405. struct numa_maps_private *numa_priv = m->private;
  1406. struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
  1407. struct vm_area_struct *vma = v;
  1408. struct numa_maps *md = &numa_priv->md;
  1409. struct file *file = vma->vm_file;
  1410. struct mm_struct *mm = vma->vm_mm;
  1411. struct mm_walk walk = {
  1412. .hugetlb_entry = gather_hugetlb_stats,
  1413. .pmd_entry = gather_pte_stats,
  1414. .private = md,
  1415. .mm = mm,
  1416. };
  1417. struct mempolicy *pol;
  1418. char buffer[64];
  1419. int nid;
  1420. if (!mm)
  1421. return 0;
  1422. /* Ensure we start with an empty set of numa_maps statistics. */
  1423. memset(md, 0, sizeof(*md));
  1424. pol = __get_vma_policy(vma, vma->vm_start);
  1425. if (pol) {
  1426. mpol_to_str(buffer, sizeof(buffer), pol);
  1427. mpol_cond_put(pol);
  1428. } else {
  1429. mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
  1430. }
  1431. seq_printf(m, "%08lx %s", vma->vm_start, buffer);
  1432. if (file) {
  1433. seq_puts(m, " file=");
  1434. seq_file_path(m, file, "\n\t= ");
  1435. } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
  1436. seq_puts(m, " heap");
  1437. } else if (is_stack(proc_priv, vma)) {
  1438. seq_puts(m, " stack");
  1439. }
  1440. if (is_vm_hugetlb_page(vma))
  1441. seq_puts(m, " huge");
  1442. /* mmap_sem is held by m_start */
  1443. walk_page_vma(vma, &walk);
  1444. if (!md->pages)
  1445. goto out;
  1446. if (md->anon)
  1447. seq_printf(m, " anon=%lu", md->anon);
  1448. if (md->dirty)
  1449. seq_printf(m, " dirty=%lu", md->dirty);
  1450. if (md->pages != md->anon && md->pages != md->dirty)
  1451. seq_printf(m, " mapped=%lu", md->pages);
  1452. if (md->mapcount_max > 1)
  1453. seq_printf(m, " mapmax=%lu", md->mapcount_max);
  1454. if (md->swapcache)
  1455. seq_printf(m, " swapcache=%lu", md->swapcache);
  1456. if (md->active < md->pages && !is_vm_hugetlb_page(vma))
  1457. seq_printf(m, " active=%lu", md->active);
  1458. if (md->writeback)
  1459. seq_printf(m, " writeback=%lu", md->writeback);
  1460. for_each_node_state(nid, N_MEMORY)
  1461. if (md->node[nid])
  1462. seq_printf(m, " N%d=%lu", nid, md->node[nid]);
  1463. seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
  1464. out:
  1465. seq_putc(m, '\n');
  1466. m_cache_vma(m, vma);
  1467. return 0;
  1468. }
  1469. static int show_pid_numa_map(struct seq_file *m, void *v)
  1470. {
  1471. return show_numa_map(m, v, 1);
  1472. }
  1473. static int show_tid_numa_map(struct seq_file *m, void *v)
  1474. {
  1475. return show_numa_map(m, v, 0);
  1476. }
  1477. static const struct seq_operations proc_pid_numa_maps_op = {
  1478. .start = m_start,
  1479. .next = m_next,
  1480. .stop = m_stop,
  1481. .show = show_pid_numa_map,
  1482. };
  1483. static const struct seq_operations proc_tid_numa_maps_op = {
  1484. .start = m_start,
  1485. .next = m_next,
  1486. .stop = m_stop,
  1487. .show = show_tid_numa_map,
  1488. };
  1489. static int numa_maps_open(struct inode *inode, struct file *file,
  1490. const struct seq_operations *ops)
  1491. {
  1492. return proc_maps_open(inode, file, ops,
  1493. sizeof(struct numa_maps_private));
  1494. }
  1495. static int pid_numa_maps_open(struct inode *inode, struct file *file)
  1496. {
  1497. return numa_maps_open(inode, file, &proc_pid_numa_maps_op);
  1498. }
  1499. static int tid_numa_maps_open(struct inode *inode, struct file *file)
  1500. {
  1501. return numa_maps_open(inode, file, &proc_tid_numa_maps_op);
  1502. }
  1503. const struct file_operations proc_pid_numa_maps_operations = {
  1504. .open = pid_numa_maps_open,
  1505. .read = seq_read,
  1506. .llseek = seq_lseek,
  1507. .release = proc_map_release,
  1508. };
  1509. const struct file_operations proc_tid_numa_maps_operations = {
  1510. .open = tid_numa_maps_open,
  1511. .read = seq_read,
  1512. .llseek = seq_lseek,
  1513. .release = proc_map_release,
  1514. };
  1515. #endif /* CONFIG_NUMA */