task_mmu.c 41 KB

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