task_mmu.c 39 KB

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