task_mmu.c 39 KB

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