blob: 18f8c3acbb85e8a90bf9a5395042bf5975987ae9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwiga5201102019-08-28 16:19:53 +02002#include <linux/pagewalk.h>
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07003#include <linux/vmacache.h>
Arnd Bergmann17fca132022-01-14 14:06:07 -08004#include <linux/mm_inline.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/hugetlb.h>
Dave Hansen22e057c2011-03-22 16:33:00 -07006#include <linux/huge_mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/mount.h>
8#include <linux/seq_file.h>
Mauricio Line070ad42005-09-03 15:55:10 -07009#include <linux/highmem.h>
Kees Cook5096add2007-05-08 00:26:04 -070010#include <linux/ptrace.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Christoph Lameter6e21c8f2005-09-03 15:54:45 -070012#include <linux/pagemap.h>
13#include <linux/mempolicy.h>
Dave Hansen22e057c2011-03-22 16:33:00 -070014#include <linux/rmap.h>
Matt Mackall85863e42008-02-04 22:29:04 -080015#include <linux/swap.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010016#include <linux/sched/mm.h>
Matt Mackall85863e42008-02-04 22:29:04 -080017#include <linux/swapops.h>
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -070018#include <linux/mmu_notifier.h>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070019#include <linux/page_idle.h>
Vlastimil Babka6a15a372016-01-14 15:19:20 -080020#include <linux/shmem_fs.h>
Minchan Kimb3a81d02017-08-10 15:24:15 -070021#include <linux/uaccess.h>
Ram Pai27cca862018-04-13 23:55:07 +100022#include <linux/pkeys.h>
Mauricio Line070ad42005-09-03 15:55:10 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/elf.h>
Minchan Kimb3a81d02017-08-10 15:24:15 -070025#include <asm/tlb.h>
Mauricio Line070ad42005-09-03 15:55:10 -070026#include <asm/tlbflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "internal.h"
28
Andrei Vagind1be35c2018-04-10 16:31:16 -070029#define SEQ_PUT_DEC(str, val) \
30 seq_put_decimal_ull_width(m, str, (val) << (PAGE_SHIFT-10), 8)
Eric W. Biedermandf5f8312008-02-08 04:18:33 -080031void task_mem(struct seq_file *m, struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -080033 unsigned long text, lib, swap, anon, file, shmem;
Hugh Dickins365e9c872005-10-29 18:16:18 -070034 unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
35
Jerome Marchand8cee8522016-01-14 15:19:29 -080036 anon = get_mm_counter(mm, MM_ANONPAGES);
37 file = get_mm_counter(mm, MM_FILEPAGES);
38 shmem = get_mm_counter(mm, MM_SHMEMPAGES);
39
Hugh Dickins365e9c872005-10-29 18:16:18 -070040 /*
41 * Note: to minimize their overhead, mm maintains hiwater_vm and
42 * hiwater_rss only when about to *lower* total_vm or rss. Any
43 * collector of these hiwater stats must therefore get total_vm
44 * and rss too, which will usually be the higher. Barriers? not
45 * worth the effort, such snapshots can always be inconsistent.
46 */
47 hiwater_vm = total_vm = mm->total_vm;
48 if (hiwater_vm < mm->hiwater_vm)
49 hiwater_vm = mm->hiwater_vm;
Jerome Marchand8cee8522016-01-14 15:19:29 -080050 hiwater_rss = total_rss = anon + file + shmem;
Hugh Dickins365e9c872005-10-29 18:16:18 -070051 if (hiwater_rss < mm->hiwater_rss)
52 hiwater_rss = mm->hiwater_rss;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Konstantin Khlebnikov8526d842018-01-31 16:17:22 -080054 /* split executable areas between text and lib */
55 text = PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK);
56 text = min(text, mm->exec_vm << PAGE_SHIFT);
57 lib = (mm->exec_vm << PAGE_SHIFT) - text;
58
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -080059 swap = get_mm_counter(mm, MM_SWAPENTS);
Andrei Vagind1be35c2018-04-10 16:31:16 -070060 SEQ_PUT_DEC("VmPeak:\t", hiwater_vm);
61 SEQ_PUT_DEC(" kB\nVmSize:\t", total_vm);
62 SEQ_PUT_DEC(" kB\nVmLck:\t", mm->locked_vm);
Davidlohr Bueso70f8a3c2019-02-06 09:59:15 -080063 SEQ_PUT_DEC(" kB\nVmPin:\t", atomic64_read(&mm->pinned_vm));
Andrei Vagind1be35c2018-04-10 16:31:16 -070064 SEQ_PUT_DEC(" kB\nVmHWM:\t", hiwater_rss);
65 SEQ_PUT_DEC(" kB\nVmRSS:\t", total_rss);
66 SEQ_PUT_DEC(" kB\nRssAnon:\t", anon);
67 SEQ_PUT_DEC(" kB\nRssFile:\t", file);
68 SEQ_PUT_DEC(" kB\nRssShmem:\t", shmem);
69 SEQ_PUT_DEC(" kB\nVmData:\t", mm->data_vm);
70 SEQ_PUT_DEC(" kB\nVmStk:\t", mm->stack_vm);
71 seq_put_decimal_ull_width(m,
72 " kB\nVmExe:\t", text >> 10, 8);
73 seq_put_decimal_ull_width(m,
74 " kB\nVmLib:\t", lib >> 10, 8);
75 seq_put_decimal_ull_width(m,
76 " kB\nVmPTE:\t", mm_pgtables_bytes(mm) >> 10, 8);
77 SEQ_PUT_DEC(" kB\nVmSwap:\t", swap);
78 seq_puts(m, " kB\n");
Naoya Horiguchi5d317b22015-11-05 18:47:14 -080079 hugetlb_report_usage(m, mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
Andrei Vagind1be35c2018-04-10 16:31:16 -070081#undef SEQ_PUT_DEC
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83unsigned long task_vsize(struct mm_struct *mm)
84{
85 return PAGE_SIZE * mm->total_vm;
86}
87
Alexey Dobriyana2ade7b2011-01-12 17:00:32 -080088unsigned long task_statm(struct mm_struct *mm,
89 unsigned long *shared, unsigned long *text,
90 unsigned long *data, unsigned long *resident)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Jerome Marchandeca56ff2016-01-14 15:19:26 -080092 *shared = get_mm_counter(mm, MM_FILEPAGES) +
93 get_mm_counter(mm, MM_SHMEMPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
95 >> PAGE_SHIFT;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -080096 *data = mm->data_vm + mm->stack_vm;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -080097 *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return mm->total_vm;
99}
100
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900101#ifdef CONFIG_NUMA
102/*
Oleg Nesterov498f2372014-10-09 15:27:52 -0700103 * Save get_task_policy() for show_numa_map().
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900104 */
105static void hold_task_mempolicy(struct proc_maps_private *priv)
106{
107 struct task_struct *task = priv->task;
108
109 task_lock(task);
Oleg Nesterov498f2372014-10-09 15:27:52 -0700110 priv->task_mempolicy = get_task_policy(task);
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900111 mpol_get(priv->task_mempolicy);
112 task_unlock(task);
113}
114static void release_task_mempolicy(struct proc_maps_private *priv)
115{
116 mpol_put(priv->task_mempolicy);
117}
118#else
119static void hold_task_mempolicy(struct proc_maps_private *priv)
120{
121}
122static void release_task_mempolicy(struct proc_maps_private *priv)
123{
124}
125#endif
126
Oleg Nesterov0c255322014-10-09 15:25:36 -0700127static void *m_start(struct seq_file *m, loff_t *ppos)
Matt Mackalla6198792008-02-04 22:29:03 -0800128{
129 struct proc_maps_private *priv = m->private;
Matthew Wilcox (Oracle)4781f2c2020-04-06 20:09:14 -0700130 unsigned long last_addr = *ppos;
Matt Mackalla6198792008-02-04 22:29:03 -0800131 struct mm_struct *mm;
Oleg Nesterov0c255322014-10-09 15:25:36 -0700132 struct vm_area_struct *vma;
Matt Mackalla6198792008-02-04 22:29:03 -0800133
Matthew Wilcox (Oracle)c2e88d22020-04-06 20:09:11 -0700134 /* See m_next(). Zero at the start or after lseek. */
Oleg Nesterovb8c20a92014-10-09 15:25:41 -0700135 if (last_addr == -1UL)
136 return NULL;
137
Oleg Nesterov2c033762014-10-09 15:25:51 -0700138 priv->task = get_proc_task(priv->inode);
Matt Mackalla6198792008-02-04 22:29:03 -0800139 if (!priv->task)
Al Viroec6fd8a2011-02-15 22:22:54 -0500140 return ERR_PTR(-ESRCH);
Matt Mackalla6198792008-02-04 22:29:03 -0800141
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700142 mm = priv->mm;
Matthew Wilcox (Oracle)d07ded62020-04-06 20:09:08 -0700143 if (!mm || !mmget_not_zero(mm)) {
144 put_task_struct(priv->task);
145 priv->task = NULL;
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700146 return NULL;
Matthew Wilcox (Oracle)d07ded62020-04-06 20:09:08 -0700147 }
Oleg Nesterov0c255322014-10-09 15:25:36 -0700148
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700149 if (mmap_read_lock_killable(mm)) {
Konstantin Khlebnikov8a713e72019-07-11 20:59:50 -0700150 mmput(mm);
Matthew Wilcox (Oracle)d07ded62020-04-06 20:09:08 -0700151 put_task_struct(priv->task);
152 priv->task = NULL;
Konstantin Khlebnikov8a713e72019-07-11 20:59:50 -0700153 return ERR_PTR(-EINTR);
154 }
155
KAMEZAWA Hiroyuki9e781442012-10-19 17:00:55 +0900156 hold_task_mempolicy(priv);
Oleg Nesterov0c255322014-10-09 15:25:36 -0700157 priv->tail_vma = get_gate_vma(mm);
Matt Mackalla6198792008-02-04 22:29:03 -0800158
Matthew Wilcox (Oracle)c2e88d22020-04-06 20:09:11 -0700159 vma = find_vma(mm, last_addr);
160 if (vma)
Oleg Nesterov0c255322014-10-09 15:25:36 -0700161 return vma;
Matt Mackalla6198792008-02-04 22:29:03 -0800162
Matthew Wilcox (Oracle)c2e88d22020-04-06 20:09:11 -0700163 return priv->tail_vma;
Matt Mackalla6198792008-02-04 22:29:03 -0800164}
165
Matthew Wilcox (Oracle)4781f2c2020-04-06 20:09:14 -0700166static void *m_next(struct seq_file *m, void *v, loff_t *ppos)
Matt Mackalla6198792008-02-04 22:29:03 -0800167{
168 struct proc_maps_private *priv = m->private;
Matthew Wilcox (Oracle)fad95502020-04-06 20:09:20 -0700169 struct vm_area_struct *next, *vma = v;
Matt Mackalla6198792008-02-04 22:29:03 -0800170
Matthew Wilcox (Oracle)fad95502020-04-06 20:09:20 -0700171 if (vma == priv->tail_vma)
172 next = NULL;
173 else if (vma->vm_next)
174 next = vma->vm_next;
175 else
176 next = priv->tail_vma;
177
Matthew Wilcox (Oracle)4781f2c2020-04-06 20:09:14 -0700178 *ppos = next ? next->vm_start : -1UL;
Matthew Wilcox (Oracle)c2e88d22020-04-06 20:09:11 -0700179
Oleg Nesterov59b4bf12014-10-09 15:25:28 -0700180 return next;
Matt Mackalla6198792008-02-04 22:29:03 -0800181}
182
183static void m_stop(struct seq_file *m, void *v)
184{
185 struct proc_maps_private *priv = m->private;
Matthew Wilcox (Oracle)d07ded62020-04-06 20:09:08 -0700186 struct mm_struct *mm = priv->mm;
Matt Mackalla6198792008-02-04 22:29:03 -0800187
Matthew Wilcox (Oracle)d07ded62020-04-06 20:09:08 -0700188 if (!priv->task)
189 return;
190
191 release_task_mempolicy(priv);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700192 mmap_read_unlock(mm);
Matthew Wilcox (Oracle)d07ded62020-04-06 20:09:08 -0700193 mmput(mm);
194 put_task_struct(priv->task);
195 priv->task = NULL;
Matt Mackalla6198792008-02-04 22:29:03 -0800196}
197
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -0700198static int proc_maps_open(struct inode *inode, struct file *file,
199 const struct seq_operations *ops, int psize)
200{
201 struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
202
203 if (!priv)
204 return -ENOMEM;
205
Oleg Nesterov2c033762014-10-09 15:25:51 -0700206 priv->inode = inode;
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700207 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
208 if (IS_ERR(priv->mm)) {
209 int err = PTR_ERR(priv->mm);
210
211 seq_release_private(inode, file);
212 return err;
213 }
214
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -0700215 return 0;
216}
217
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700218static int proc_map_release(struct inode *inode, struct file *file)
219{
220 struct seq_file *seq = file->private_data;
221 struct proc_maps_private *priv = seq->private;
222
223 if (priv->mm)
224 mmdrop(priv->mm);
225
226 return seq_release_private(inode, file);
227}
228
Matt Mackalla6198792008-02-04 22:29:03 -0800229static int do_maps_open(struct inode *inode, struct file *file,
Jan Engelhardt03a44822008-02-08 04:21:19 -0800230 const struct seq_operations *ops)
Matt Mackalla6198792008-02-04 22:29:03 -0800231{
Oleg Nesterov4db7d0e2014-10-09 15:25:21 -0700232 return proc_maps_open(inode, file, ops,
233 sizeof(struct proc_maps_private));
Matt Mackalla6198792008-02-04 22:29:03 -0800234}
Mauricio Line070ad42005-09-03 15:55:10 -0700235
Johannes Weiner65376df2016-02-02 16:57:29 -0800236/*
237 * Indicate if the VMA is a stack for the given task; for
238 * /proc/PID/maps that is the stack of the main task.
239 */
Michal Hocko1240ea02017-09-08 16:13:35 -0700240static int is_stack(struct vm_area_struct *vma)
Oleg Nesterov58cb6542014-10-09 15:25:54 -0700241{
Andy Lutomirskib18cb642016-09-30 10:58:57 -0700242 /*
243 * We make no effort to guess what a given thread considers to be
244 * its "stack". It's not even well-defined for programs written
245 * languages like Go.
246 */
247 return vma->vm_start <= vma->vm_mm->start_stack &&
248 vma->vm_end >= vma->vm_mm->start_stack;
Oleg Nesterov58cb6542014-10-09 15:25:54 -0700249}
250
Daniel Colascione493b0e92017-09-06 16:25:08 -0700251static void show_vma_header_prefix(struct seq_file *m,
252 unsigned long start, unsigned long end,
253 vm_flags_t flags, unsigned long long pgoff,
254 dev_t dev, unsigned long ino)
255{
256 seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
Andrei Vagin0e3dc012018-04-10 16:30:44 -0700257 seq_put_hex_ll(m, NULL, start, 8);
258 seq_put_hex_ll(m, "-", end, 8);
259 seq_putc(m, ' ');
260 seq_putc(m, flags & VM_READ ? 'r' : '-');
261 seq_putc(m, flags & VM_WRITE ? 'w' : '-');
262 seq_putc(m, flags & VM_EXEC ? 'x' : '-');
263 seq_putc(m, flags & VM_MAYSHARE ? 's' : 'p');
264 seq_put_hex_ll(m, " ", pgoff, 8);
265 seq_put_hex_ll(m, " ", MAJOR(dev), 2);
266 seq_put_hex_ll(m, ":", MINOR(dev), 2);
267 seq_put_decimal_ull(m, " ", ino);
268 seq_putc(m, ' ');
Daniel Colascione493b0e92017-09-06 16:25:08 -0700269}
270
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700271static void
Vlastimil Babka871305b2018-08-21 21:52:48 -0700272show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Mauricio Line070ad42005-09-03 15:55:10 -0700274 struct mm_struct *mm = vma->vm_mm;
275 struct file *file = vma->vm_file;
KOSAKI Motohiroca16d142011-05-26 19:16:19 +0900276 vm_flags_t flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 unsigned long ino = 0;
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700278 unsigned long long pgoff = 0;
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200279 unsigned long start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 dev_t dev = 0;
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700281 const char *name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if (file) {
Al Viro496ad9a2013-01-23 17:07:38 -0500284 struct inode *inode = file_inode(vma->vm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 dev = inode->i_sb->s_dev;
286 ino = inode->i_ino;
KAMEZAWA Hiroyuki6260a4b2009-04-06 19:00:30 -0700287 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
Linus Torvaldsd7824372010-08-15 11:35:52 -0700290 start = vma->vm_start;
Mikulas Patockaa09a79f2011-05-09 13:01:09 +0200291 end = vma->vm_end;
Daniel Colascione493b0e92017-09-06 16:25:08 -0700292 show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 /*
295 * Print the dentry name for named mappings, and a
296 * special [heap] marker for the heap:
297 */
Mauricio Line070ad42005-09-03 15:55:10 -0700298 if (file) {
Tetsuo Handa652586d2013-11-14 14:31:57 -0800299 seq_pad(m, ' ');
Miklos Szeredi2726d562015-06-19 10:30:28 +0200300 seq_file_path(m, file, "\n");
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700301 goto done;
302 }
303
Andy Lutomirski78d683e2014-05-19 15:58:32 -0700304 if (vma->vm_ops && vma->vm_ops->name) {
305 name = vma->vm_ops->name(vma);
306 if (name)
307 goto done;
308 }
309
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700310 name = arch_vma_name(vma);
311 if (!name) {
Colin Cross9a100642022-01-14 14:05:59 -0800312 const char *anon_name;
313
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700314 if (!mm) {
315 name = "[vdso]";
316 goto done;
317 }
318
319 if (vma->vm_start <= mm->brk &&
320 vma->vm_end >= mm->start_brk) {
321 name = "[heap]";
322 goto done;
323 }
324
Colin Cross9a100642022-01-14 14:05:59 -0800325 if (is_stack(vma)) {
Johannes Weiner65376df2016-02-02 16:57:29 -0800326 name = "[stack]";
Colin Cross9a100642022-01-14 14:05:59 -0800327 goto done;
328 }
329
330 anon_name = vma_anon_name(vma);
331 if (anon_name) {
332 seq_pad(m, ' ');
333 seq_printf(m, "[anon:%s]", anon_name);
334 }
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700335 }
336
337done:
338 if (name) {
Tetsuo Handa652586d2013-11-14 14:31:57 -0800339 seq_pad(m, ' ');
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700340 seq_puts(m, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 seq_putc(m, '\n');
Joe Korty7c88db02008-10-16 15:27:09 +0400343}
344
Vlastimil Babka871305b2018-08-21 21:52:48 -0700345static int show_map(struct seq_file *m, void *v)
Joe Korty7c88db02008-10-16 15:27:09 +0400346{
Vlastimil Babka871305b2018-08-21 21:52:48 -0700347 show_map_vma(m, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return 0;
349}
350
Jan Engelhardt03a44822008-02-08 04:21:19 -0800351static const struct seq_operations proc_pid_maps_op = {
Matt Mackalla6198792008-02-04 22:29:03 -0800352 .start = m_start,
353 .next = m_next,
354 .stop = m_stop,
Vlastimil Babka871305b2018-08-21 21:52:48 -0700355 .show = show_map
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700356};
357
358static int pid_maps_open(struct inode *inode, struct file *file)
Matt Mackalla6198792008-02-04 22:29:03 -0800359{
360 return do_maps_open(inode, file, &proc_pid_maps_op);
361}
362
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700363const struct file_operations proc_pid_maps_operations = {
364 .open = pid_maps_open,
365 .read = seq_read,
366 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -0700367 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700368};
369
Matt Mackalla6198792008-02-04 22:29:03 -0800370/*
371 * Proportional Set Size(PSS): my share of RSS.
372 *
373 * PSS of a process is the count of pages it has in memory, where each
374 * page is divided by the number of processes sharing it. So if a
375 * process has 1000 pages all to itself, and 1000 shared with one other
376 * process, its PSS will be 1500.
377 *
378 * To keep (accumulated) division errors low, we adopt a 64bit
379 * fixed-point pss counter to minimize division errors. So (pss >>
380 * PSS_SHIFT) would be the real byte count.
381 *
382 * A shift of 12 before division means (assuming 4K page size):
383 * - 1M 3-user-pages add up to 8KB errors;
384 * - supports mapcount up to 2^24, or 16M;
385 * - supports PSS up to 2^52 bytes, or 4PB.
386 */
387#define PSS_SHIFT 12
388
Matt Mackall1e883282008-02-04 22:29:07 -0800389#ifdef CONFIG_PROC_PAGE_MONITOR
Peter Zijlstra214e4712008-04-28 02:12:55 -0700390struct mem_size_stats {
Matt Mackalla6198792008-02-04 22:29:03 -0800391 unsigned long resident;
392 unsigned long shared_clean;
393 unsigned long shared_dirty;
394 unsigned long private_clean;
395 unsigned long private_dirty;
396 unsigned long referenced;
Nikanth Karthikesanb40d4f82010-10-27 15:34:10 -0700397 unsigned long anonymous;
Shaohua Licf8496e2017-05-03 14:52:42 -0700398 unsigned long lazyfree;
Dave Hansen4031a212011-03-22 16:33:01 -0700399 unsigned long anonymous_thp;
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700400 unsigned long shmem_thp;
Song Liu60fbf0a2019-09-23 15:37:54 -0700401 unsigned long file_thp;
Peter Zijlstra214e4712008-04-28 02:12:55 -0700402 unsigned long swap;
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800403 unsigned long shared_hugetlb;
404 unsigned long private_hugetlb;
Matt Mackalla6198792008-02-04 22:29:03 -0800405 u64 pss;
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700406 u64 pss_anon;
407 u64 pss_file;
408 u64 pss_shmem;
Daniel Colascione493b0e92017-09-06 16:25:08 -0700409 u64 pss_locked;
Minchan Kim8334b962015-09-08 15:00:24 -0700410 u64 swap_pss;
Matt Mackalla6198792008-02-04 22:29:03 -0800411};
412
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700413static void smaps_page_accumulate(struct mem_size_stats *mss,
414 struct page *page, unsigned long size, unsigned long pss,
415 bool dirty, bool locked, bool private)
416{
417 mss->pss += pss;
418
419 if (PageAnon(page))
420 mss->pss_anon += pss;
421 else if (PageSwapBacked(page))
422 mss->pss_shmem += pss;
423 else
424 mss->pss_file += pss;
425
426 if (locked)
427 mss->pss_locked += pss;
428
429 if (dirty || PageDirty(page)) {
430 if (private)
431 mss->private_dirty += size;
432 else
433 mss->shared_dirty += size;
434 } else {
435 if (private)
436 mss->private_clean += size;
437 else
438 mss->shared_clean += size;
439 }
440}
441
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800442static void smaps_account(struct mem_size_stats *mss, struct page *page,
Sandeep Patil27dd7682019-02-12 15:36:11 -0800443 bool compound, bool young, bool dirty, bool locked)
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800444{
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -0700445 int i, nr = compound ? compound_nr(page) : 1;
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800446 unsigned long size = nr * PAGE_SIZE;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700447
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700448 /*
449 * First accumulate quantities that depend only on |size| and the type
450 * of the compound page.
451 */
Shaohua Licf8496e2017-05-03 14:52:42 -0700452 if (PageAnon(page)) {
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800453 mss->anonymous += size;
Shaohua Licf8496e2017-05-03 14:52:42 -0700454 if (!PageSwapBacked(page) && !dirty && !PageDirty(page))
455 mss->lazyfree += size;
456 }
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800457
458 mss->resident += size;
459 /* Accumulate the size in pages that have been accessed. */
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700460 if (young || page_is_young(page) || PageReferenced(page))
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800461 mss->referenced += size;
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800462
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800463 /*
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700464 * Then accumulate quantities that may depend on sharing, or that may
465 * differ page-by-page.
466 *
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800467 * page_count(page) == 1 guarantees the page is mapped exactly once.
468 * If any subpage of the compound page mapped with PTE it would elevate
469 * page_count().
470 */
471 if (page_count(page) == 1) {
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700472 smaps_page_accumulate(mss, page, size, size << PSS_SHIFT, dirty,
473 locked, true);
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800474 return;
475 }
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800476 for (i = 0; i < nr; i++, page++) {
477 int mapcount = page_mapcount(page);
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700478 unsigned long pss = PAGE_SIZE << PSS_SHIFT;
479 if (mapcount >= 2)
480 pss /= mapcount;
481 smaps_page_accumulate(mss, page, PAGE_SIZE, pss, dirty, locked,
482 mapcount < 2);
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800483 }
484}
485
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800486#ifdef CONFIG_SHMEM
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800487static int smaps_pte_hole(unsigned long addr, unsigned long end,
Steven Priceb7a16c72020-02-03 17:36:03 -0800488 __always_unused int depth, struct mm_walk *walk)
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800489{
490 struct mem_size_stats *mss = walk->private;
Peter Xu10c848c82021-11-05 13:35:59 -0700491 struct vm_area_struct *vma = walk->vma;
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800492
Peter Xu10c848c82021-11-05 13:35:59 -0700493 mss->swap += shmem_partial_swap_usage(walk->vma->vm_file->f_mapping,
494 linear_page_index(vma, addr),
495 linear_page_index(vma, end));
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800496
497 return 0;
498}
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200499#else
500#define smaps_pte_hole NULL
501#endif /* CONFIG_SHMEM */
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800502
Peter Xu23010032021-11-05 13:36:05 -0700503static void smaps_pte_hole_lookup(unsigned long addr, struct mm_walk *walk)
504{
505#ifdef CONFIG_SHMEM
506 if (walk->ops->pte_hole) {
507 /* depth is not used */
508 smaps_pte_hole(addr, addr + PAGE_SIZE, 0, walk);
509 }
510#endif
511}
512
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800513static void smaps_pte_entry(pte_t *pte, unsigned long addr,
514 struct mm_walk *walk)
Dave Hansenae11c4d2011-03-22 16:32:58 -0700515{
516 struct mem_size_stats *mss = walk->private;
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800517 struct vm_area_struct *vma = walk->vma;
Sandeep Patil27dd7682019-02-12 15:36:11 -0800518 bool locked = !!(vma->vm_flags & VM_LOCKED);
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700519 struct page *page = NULL;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700520
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800521 if (pte_present(*pte)) {
522 page = vm_normal_page(vma, addr, *pte);
523 } else if (is_swap_pte(*pte)) {
524 swp_entry_t swpent = pte_to_swp_entry(*pte);
Konstantin Khlebnikovb1d4d9e2012-05-31 16:26:20 -0700525
Minchan Kim8334b962015-09-08 15:00:24 -0700526 if (!non_swap_entry(swpent)) {
527 int mapcount;
528
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800529 mss->swap += PAGE_SIZE;
Minchan Kim8334b962015-09-08 15:00:24 -0700530 mapcount = swp_swapcount(swpent);
531 if (mapcount >= 2) {
532 u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
533
534 do_div(pss_delta, mapcount);
535 mss->swap_pss += pss_delta;
536 } else {
537 mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
538 }
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -0700539 } else if (is_pfn_swap_entry(swpent))
540 page = pfn_swap_entry_to_page(swpent);
Peter Xu23010032021-11-05 13:36:05 -0700541 } else {
542 smaps_pte_hole_lookup(addr, walk);
Vlastimil Babka48131e02016-01-14 15:19:23 -0800543 return;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700544 }
545
Dave Hansenae11c4d2011-03-22 16:32:58 -0700546 if (!page)
547 return;
Kirill A. Shutemovafd98832016-01-15 16:52:13 -0800548
Sandeep Patil27dd7682019-02-12 15:36:11 -0800549 smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte), locked);
Dave Hansenae11c4d2011-03-22 16:32:58 -0700550}
551
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800552#ifdef CONFIG_TRANSPARENT_HUGEPAGE
553static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
554 struct mm_walk *walk)
555{
556 struct mem_size_stats *mss = walk->private;
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800557 struct vm_area_struct *vma = walk->vma;
Sandeep Patil27dd7682019-02-12 15:36:11 -0800558 bool locked = !!(vma->vm_flags & VM_LOCKED);
Huang Yingc94b6922020-06-01 21:50:05 -0700559 struct page *page = NULL;
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800560
Huang Yingc94b6922020-06-01 21:50:05 -0700561 if (pmd_present(*pmd)) {
562 /* FOLL_DUMP will return -EFAULT on huge zero page */
563 page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
564 } else if (unlikely(thp_migration_supported() && is_swap_pmd(*pmd))) {
565 swp_entry_t entry = pmd_to_swp_entry(*pmd);
566
567 if (is_migration_entry(entry))
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -0700568 page = pfn_swap_entry_to_page(entry);
Huang Yingc94b6922020-06-01 21:50:05 -0700569 }
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800570 if (IS_ERR_OR_NULL(page))
571 return;
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700572 if (PageAnon(page))
573 mss->anonymous_thp += HPAGE_PMD_SIZE;
574 else if (PageSwapBacked(page))
575 mss->shmem_thp += HPAGE_PMD_SIZE;
Dan Williamsca120cf2016-09-03 10:38:03 -0700576 else if (is_zone_device_page(page))
577 /* pass */;
Kirill A. Shutemov65c45372016-07-26 15:26:10 -0700578 else
Song Liu60fbf0a2019-09-23 15:37:54 -0700579 mss->file_thp += HPAGE_PMD_SIZE;
Sandeep Patil27dd7682019-02-12 15:36:11 -0800580 smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd), locked);
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800581}
582#else
583static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
584 struct mm_walk *walk)
585{
586}
587#endif
588
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800589static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -0700590 struct mm_walk *walk)
Mauricio Line070ad42005-09-03 15:55:10 -0700591{
Naoya Horiguchi14eb6fd2015-02-11 15:27:43 -0800592 struct vm_area_struct *vma = walk->vma;
Dave Hansenae11c4d2011-03-22 16:32:58 -0700593 pte_t *pte;
Hugh Dickins705e87c2005-10-29 18:16:27 -0700594 spinlock_t *ptl;
Mauricio Line070ad42005-09-03 15:55:10 -0700595
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -0800596 ptl = pmd_trans_huge_lock(pmd, vma);
597 if (ptl) {
Huang Yingc94b6922020-06-01 21:50:05 -0700598 smaps_pmd_entry(pmd, addr, walk);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -0800599 spin_unlock(ptl);
David Rientjes14038302017-09-08 16:13:41 -0700600 goto out;
Dave Hansen22e057c2011-03-22 16:33:00 -0700601 }
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -0700602
603 if (pmd_trans_unstable(pmd))
David Rientjes14038302017-09-08 16:13:41 -0700604 goto out;
Dave Hansen22e057c2011-03-22 16:33:00 -0700605 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700606 * The mmap_lock held all the way back in m_start() is what
Dave Hansen22e057c2011-03-22 16:33:00 -0700607 * keeps khugepaged out of here and from collapsing things
608 * in here.
609 */
Hugh Dickins705e87c2005-10-29 18:16:27 -0700610 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
Dave Hansenae11c4d2011-03-22 16:32:58 -0700611 for (; addr != end; pte++, addr += PAGE_SIZE)
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800612 smaps_pte_entry(pte, addr, walk);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700613 pte_unmap_unlock(pte - 1, ptl);
David Rientjes14038302017-09-08 16:13:41 -0700614out:
Hugh Dickins705e87c2005-10-29 18:16:27 -0700615 cond_resched();
Matt Mackallb3ae5ac2008-02-04 22:29:01 -0800616 return 0;
Mauricio Line070ad42005-09-03 15:55:10 -0700617}
618
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800619static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
620{
621 /*
622 * Don't forget to update Documentation/ on changes.
623 */
624 static const char mnemonics[BITS_PER_LONG][2] = {
625 /*
626 * In case if we meet a flag we don't know about.
627 */
628 [0 ... (BITS_PER_LONG-1)] = "??",
629
630 [ilog2(VM_READ)] = "rd",
631 [ilog2(VM_WRITE)] = "wr",
632 [ilog2(VM_EXEC)] = "ex",
633 [ilog2(VM_SHARED)] = "sh",
634 [ilog2(VM_MAYREAD)] = "mr",
635 [ilog2(VM_MAYWRITE)] = "mw",
636 [ilog2(VM_MAYEXEC)] = "me",
637 [ilog2(VM_MAYSHARE)] = "ms",
638 [ilog2(VM_GROWSDOWN)] = "gd",
639 [ilog2(VM_PFNMAP)] = "pf",
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800640 [ilog2(VM_LOCKED)] = "lo",
641 [ilog2(VM_IO)] = "io",
642 [ilog2(VM_SEQ_READ)] = "sr",
643 [ilog2(VM_RAND_READ)] = "rr",
644 [ilog2(VM_DONTCOPY)] = "dc",
645 [ilog2(VM_DONTEXPAND)] = "de",
646 [ilog2(VM_ACCOUNT)] = "ac",
647 [ilog2(VM_NORESERVE)] = "nr",
648 [ilog2(VM_HUGETLB)] = "ht",
Jan Karab6fb2932017-11-01 16:36:41 +0100649 [ilog2(VM_SYNC)] = "sf",
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800650 [ilog2(VM_ARCH_1)] = "ar",
Rik van Rield2cd9ed2017-09-06 16:25:15 -0700651 [ilog2(VM_WIPEONFORK)] = "wf",
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800652 [ilog2(VM_DONTDUMP)] = "dd",
Daniel Kiss424037b2020-03-16 16:50:54 +0000653#ifdef CONFIG_ARM64_BTI
654 [ilog2(VM_ARM64_BTI)] = "bt",
655#endif
Naoya Horiguchiec8e41a2013-11-12 15:07:49 -0800656#ifdef CONFIG_MEM_SOFT_DIRTY
657 [ilog2(VM_SOFTDIRTY)] = "sd",
658#endif
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800659 [ilog2(VM_MIXEDMAP)] = "mm",
660 [ilog2(VM_HUGEPAGE)] = "hg",
661 [ilog2(VM_NOHUGEPAGE)] = "nh",
662 [ilog2(VM_MERGEABLE)] = "mg",
Andrea Arcangeli16ba6f82015-09-04 15:46:17 -0700663 [ilog2(VM_UFFD_MISSING)]= "um",
664 [ilog2(VM_UFFD_WP)] = "uw",
Catalin Marinas9f341932019-11-27 10:00:27 +0000665#ifdef CONFIG_ARM64_MTE
666 [ilog2(VM_MTE)] = "mt",
667 [ilog2(VM_MTE_ALLOWED)] = "",
668#endif
Ram Pai52122132018-03-27 02:09:26 -0700669#ifdef CONFIG_ARCH_HAS_PKEYS
Dave Hansenc1192f82016-02-12 13:02:27 -0800670 /* These come out via ProtectionKey: */
671 [ilog2(VM_PKEY_BIT0)] = "",
672 [ilog2(VM_PKEY_BIT1)] = "",
673 [ilog2(VM_PKEY_BIT2)] = "",
674 [ilog2(VM_PKEY_BIT3)] = "",
Ram Pai2c9e0a62018-03-27 02:09:27 -0700675#if VM_PKEY_BIT4
676 [ilog2(VM_PKEY_BIT4)] = "",
Dave Hansenc1192f82016-02-12 13:02:27 -0800677#endif
Ram Pai52122132018-03-27 02:09:26 -0700678#endif /* CONFIG_ARCH_HAS_PKEYS */
Axel Rasmussen7677f7f2021-05-04 18:35:36 -0700679#ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR
680 [ilog2(VM_UFFD_MINOR)] = "ui",
681#endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800682 };
683 size_t i;
684
685 seq_puts(m, "VmFlags: ");
686 for (i = 0; i < BITS_PER_LONG; i++) {
Dave Hansenc1192f82016-02-12 13:02:27 -0800687 if (!mnemonics[i][0])
688 continue;
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800689 if (vma->vm_flags & (1UL << i)) {
Andrei Vaginf6640662018-04-10 16:31:19 -0700690 seq_putc(m, mnemonics[i][0]);
691 seq_putc(m, mnemonics[i][1]);
692 seq_putc(m, ' ');
Cyrill Gorcunov834f82e2012-12-17 16:03:13 -0800693 }
694 }
695 seq_putc(m, '\n');
696}
697
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800698#ifdef CONFIG_HUGETLB_PAGE
699static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
700 unsigned long addr, unsigned long end,
701 struct mm_walk *walk)
702{
703 struct mem_size_stats *mss = walk->private;
704 struct vm_area_struct *vma = walk->vma;
705 struct page *page = NULL;
706
707 if (pte_present(*pte)) {
708 page = vm_normal_page(vma, addr, *pte);
709 } else if (is_swap_pte(*pte)) {
710 swp_entry_t swpent = pte_to_swp_entry(*pte);
711
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -0700712 if (is_pfn_swap_entry(swpent))
713 page = pfn_swap_entry_to_page(swpent);
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800714 }
715 if (page) {
716 int mapcount = page_mapcount(page);
717
718 if (mapcount >= 2)
719 mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
720 else
721 mss->private_hugetlb += huge_page_size(hstate_vma(vma));
722 }
723 return 0;
724}
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200725#else
726#define smaps_hugetlb_range NULL
Naoya Horiguchi25ee01a2015-11-05 18:47:11 -0800727#endif /* HUGETLB_PAGE */
728
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200729static const struct mm_walk_ops smaps_walk_ops = {
730 .pmd_entry = smaps_pte_range,
731 .hugetlb_entry = smaps_hugetlb_range,
732};
733
734static const struct mm_walk_ops smaps_shmem_walk_ops = {
735 .pmd_entry = smaps_pte_range,
736 .hugetlb_entry = smaps_hugetlb_range,
737 .pte_hole = smaps_pte_hole,
738};
739
Chinwen Chang03b4b112020-10-13 16:53:43 -0700740/*
741 * Gather mem stats from @vma with the indicated beginning
742 * address @start, and keep them in @mss.
743 *
744 * Use vm_start of @vma as the beginning address if @start is 0.
745 */
Vlastimil Babka8e68d682018-08-21 21:52:52 -0700746static void smap_gather_stats(struct vm_area_struct *vma,
Chinwen Chang03b4b112020-10-13 16:53:43 -0700747 struct mem_size_stats *mss, unsigned long start)
Mauricio Line070ad42005-09-03 15:55:10 -0700748{
Chinwen Chang03b4b112020-10-13 16:53:43 -0700749 const struct mm_walk_ops *ops = &smaps_walk_ops;
750
751 /* Invalid start */
752 if (start >= vma->vm_end)
753 return;
754
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800755#ifdef CONFIG_SHMEM
756 if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) {
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800757 /*
758 * For shared or readonly shmem mappings we know that all
759 * swapped out pages belong to the shmem object, and we can
760 * obtain the swap value much more efficiently. For private
761 * writable mappings, we might have COW pages that are
762 * not affected by the parent swapped out pages of the shmem
763 * object, so we have to distinguish them during the page walk.
764 * Unless we know that the shmem object (or the part mapped by
765 * our VMA) has no swapped out pages at all.
766 */
767 unsigned long shmem_swapped = shmem_swap_usage(vma);
768
Chinwen Chang03b4b112020-10-13 16:53:43 -0700769 if (!start && (!shmem_swapped || (vma->vm_flags & VM_SHARED) ||
770 !(vma->vm_flags & VM_WRITE))) {
Vlastimil Babkafa76da42018-10-26 15:02:16 -0700771 mss->swap += shmem_swapped;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800772 } else {
Chinwen Chang03b4b112020-10-13 16:53:43 -0700773 ops = &smaps_shmem_walk_ops;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800774 }
Vlastimil Babkac261e7d92016-01-14 15:19:17 -0800775 }
776#endif
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700777 /* mmap_lock is held in m_start */
Chinwen Chang03b4b112020-10-13 16:53:43 -0700778 if (!start)
779 walk_page_vma(vma, ops, mss);
780 else
781 walk_page_range(vma->vm_mm, start, vma->vm_end, ops, mss);
Vlastimil Babka8e68d682018-08-21 21:52:52 -0700782}
783
784#define SEQ_PUT_DEC(str, val) \
785 seq_put_decimal_ull_width(m, str, (val) >> 10, 8)
Vlastimil Babkaf1547952018-08-21 21:52:56 -0700786
787/* Show the contents common for smaps and smaps_rollup */
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700788static void __show_smap(struct seq_file *m, const struct mem_size_stats *mss,
789 bool rollup_mode)
Vlastimil Babkaf1547952018-08-21 21:52:56 -0700790{
791 SEQ_PUT_DEC("Rss: ", mss->resident);
792 SEQ_PUT_DEC(" kB\nPss: ", mss->pss >> PSS_SHIFT);
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700793 if (rollup_mode) {
794 /*
795 * These are meaningful only for smaps_rollup, otherwise two of
796 * them are zero, and the other one is the same as Pss.
797 */
798 SEQ_PUT_DEC(" kB\nPss_Anon: ",
799 mss->pss_anon >> PSS_SHIFT);
800 SEQ_PUT_DEC(" kB\nPss_File: ",
801 mss->pss_file >> PSS_SHIFT);
802 SEQ_PUT_DEC(" kB\nPss_Shmem: ",
803 mss->pss_shmem >> PSS_SHIFT);
804 }
Vlastimil Babkaf1547952018-08-21 21:52:56 -0700805 SEQ_PUT_DEC(" kB\nShared_Clean: ", mss->shared_clean);
806 SEQ_PUT_DEC(" kB\nShared_Dirty: ", mss->shared_dirty);
807 SEQ_PUT_DEC(" kB\nPrivate_Clean: ", mss->private_clean);
808 SEQ_PUT_DEC(" kB\nPrivate_Dirty: ", mss->private_dirty);
809 SEQ_PUT_DEC(" kB\nReferenced: ", mss->referenced);
810 SEQ_PUT_DEC(" kB\nAnonymous: ", mss->anonymous);
811 SEQ_PUT_DEC(" kB\nLazyFree: ", mss->lazyfree);
812 SEQ_PUT_DEC(" kB\nAnonHugePages: ", mss->anonymous_thp);
813 SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp);
Michal Koutný471e78c2020-08-11 18:30:57 -0700814 SEQ_PUT_DEC(" kB\nFilePmdMapped: ", mss->file_thp);
Vlastimil Babkaf1547952018-08-21 21:52:56 -0700815 SEQ_PUT_DEC(" kB\nShared_Hugetlb: ", mss->shared_hugetlb);
816 seq_put_decimal_ull_width(m, " kB\nPrivate_Hugetlb: ",
817 mss->private_hugetlb >> 10, 7);
818 SEQ_PUT_DEC(" kB\nSwap: ", mss->swap);
819 SEQ_PUT_DEC(" kB\nSwapPss: ",
820 mss->swap_pss >> PSS_SHIFT);
821 SEQ_PUT_DEC(" kB\nLocked: ",
822 mss->pss_locked >> PSS_SHIFT);
823 seq_puts(m, " kB\n");
824}
825
Vlastimil Babka8e68d682018-08-21 21:52:52 -0700826static int show_smap(struct seq_file *m, void *v)
827{
Vlastimil Babka8e68d682018-08-21 21:52:52 -0700828 struct vm_area_struct *vma = v;
Vlastimil Babka258f6692018-08-21 21:52:59 -0700829 struct mem_size_stats mss;
Vlastimil Babka8e68d682018-08-21 21:52:52 -0700830
Vlastimil Babka258f6692018-08-21 21:52:59 -0700831 memset(&mss, 0, sizeof(mss));
Vlastimil Babka8e68d682018-08-21 21:52:52 -0700832
Chinwen Chang03b4b112020-10-13 16:53:43 -0700833 smap_gather_stats(vma, &mss, 0);
Matt Mackall4752c362008-02-04 22:29:02 -0800834
Vlastimil Babka258f6692018-08-21 21:52:59 -0700835 show_map_vma(m, vma);
Matt Mackall4752c362008-02-04 22:29:02 -0800836
Vlastimil Babka258f6692018-08-21 21:52:59 -0700837 SEQ_PUT_DEC("Size: ", vma->vm_end - vma->vm_start);
838 SEQ_PUT_DEC(" kB\nKernelPageSize: ", vma_kernel_pagesize(vma));
839 SEQ_PUT_DEC(" kB\nMMUPageSize: ", vma_mmu_pagesize(vma));
840 seq_puts(m, " kB\n");
Matt Mackall4752c362008-02-04 22:29:02 -0800841
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700842 __show_smap(m, &mss, false);
Vlastimil Babkaf1547952018-08-21 21:52:56 -0700843
Michal Koutný471e78c2020-08-11 18:30:57 -0700844 seq_printf(m, "THPeligible: %d\n",
Miaohe Line6be37b2021-06-30 18:47:50 -0700845 transparent_hugepage_active(vma));
Michal Hocko7635d9c2018-12-28 00:38:21 -0800846
Vlastimil Babka258f6692018-08-21 21:52:59 -0700847 if (arch_pkeys_enabled())
848 seq_printf(m, "ProtectionKey: %8u\n", vma_pkey(vma));
849 show_smap_vma_flags(m, vma);
850
Vlastimil Babka258f6692018-08-21 21:52:59 -0700851 return 0;
852}
853
854static int show_smaps_rollup(struct seq_file *m, void *v)
855{
856 struct proc_maps_private *priv = m->private;
857 struct mem_size_stats mss;
858 struct mm_struct *mm;
859 struct vm_area_struct *vma;
860 unsigned long last_vma_end = 0;
861 int ret = 0;
862
863 priv->task = get_proc_task(priv->inode);
864 if (!priv->task)
865 return -ESRCH;
866
867 mm = priv->mm;
868 if (!mm || !mmget_not_zero(mm)) {
869 ret = -ESRCH;
870 goto out_put_task;
871 }
872
873 memset(&mss, 0, sizeof(mss));
874
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700875 ret = mmap_read_lock_killable(mm);
Konstantin Khlebnikova26a9782019-07-11 20:59:53 -0700876 if (ret)
877 goto out_put_mm;
878
Vlastimil Babka258f6692018-08-21 21:52:59 -0700879 hold_task_mempolicy(priv);
880
Chinwen Changff9f47f2020-10-13 16:53:47 -0700881 for (vma = priv->mm->mmap; vma;) {
Chinwen Chang03b4b112020-10-13 16:53:43 -0700882 smap_gather_stats(vma, &mss, 0);
Vlastimil Babka258f6692018-08-21 21:52:59 -0700883 last_vma_end = vma->vm_end;
Chinwen Changff9f47f2020-10-13 16:53:47 -0700884
885 /*
886 * Release mmap_lock temporarily if someone wants to
887 * access it for write request.
888 */
889 if (mmap_lock_is_contended(mm)) {
890 mmap_read_unlock(mm);
891 ret = mmap_read_lock_killable(mm);
892 if (ret) {
893 release_task_mempolicy(priv);
894 goto out_put_mm;
895 }
896
897 /*
898 * After dropping the lock, there are four cases to
899 * consider. See the following example for explanation.
900 *
901 * +------+------+-----------+
902 * | VMA1 | VMA2 | VMA3 |
903 * +------+------+-----------+
904 * | | | |
905 * 4k 8k 16k 400k
906 *
907 * Suppose we drop the lock after reading VMA2 due to
908 * contention, then we get:
909 *
910 * last_vma_end = 16k
911 *
912 * 1) VMA2 is freed, but VMA3 exists:
913 *
914 * find_vma(mm, 16k - 1) will return VMA3.
915 * In this case, just continue from VMA3.
916 *
917 * 2) VMA2 still exists:
918 *
919 * find_vma(mm, 16k - 1) will return VMA2.
920 * Iterate the loop like the original one.
921 *
922 * 3) No more VMAs can be found:
923 *
924 * find_vma(mm, 16k - 1) will return NULL.
925 * No more things to do, just break.
926 *
927 * 4) (last_vma_end - 1) is the middle of a vma (VMA'):
928 *
929 * find_vma(mm, 16k - 1) will return VMA' whose range
930 * contains last_vma_end.
931 * Iterate VMA' from last_vma_end.
932 */
933 vma = find_vma(mm, last_vma_end - 1);
934 /* Case 3 above */
935 if (!vma)
936 break;
937
938 /* Case 1 above */
939 if (vma->vm_start >= last_vma_end)
940 continue;
941
942 /* Case 4 above */
943 if (vma->vm_end > last_vma_end)
944 smap_gather_stats(vma, &mss, last_vma_end);
945 }
946 /* Case 2 above */
947 vma = vma->vm_next;
Vlastimil Babka258f6692018-08-21 21:52:59 -0700948 }
949
950 show_vma_header_prefix(m, priv->mm->mmap->vm_start,
951 last_vma_end, 0, 0, 0, 0);
952 seq_pad(m, ' ');
953 seq_puts(m, "[rollup]\n");
954
Luigi Semenzatoee2ad712019-07-11 21:00:10 -0700955 __show_smap(m, &mss, true);
Vlastimil Babka258f6692018-08-21 21:52:59 -0700956
957 release_task_mempolicy(priv);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700958 mmap_read_unlock(mm);
Vlastimil Babka258f6692018-08-21 21:52:59 -0700959
Konstantin Khlebnikova26a9782019-07-11 20:59:53 -0700960out_put_mm:
961 mmput(mm);
Vlastimil Babka258f6692018-08-21 21:52:59 -0700962out_put_task:
963 put_task_struct(priv->task);
964 priv->task = NULL;
965
Daniel Colascione493b0e92017-09-06 16:25:08 -0700966 return ret;
Mauricio Line070ad42005-09-03 15:55:10 -0700967}
Andrei Vagind1be35c2018-04-10 16:31:16 -0700968#undef SEQ_PUT_DEC
Mauricio Line070ad42005-09-03 15:55:10 -0700969
Jan Engelhardt03a44822008-02-08 04:21:19 -0800970static const struct seq_operations proc_pid_smaps_op = {
Matt Mackalla6198792008-02-04 22:29:03 -0800971 .start = m_start,
972 .next = m_next,
973 .stop = m_stop,
Vlastimil Babka871305b2018-08-21 21:52:48 -0700974 .show = show_smap
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700975};
976
977static int pid_smaps_open(struct inode *inode, struct file *file)
Matt Mackalla6198792008-02-04 22:29:03 -0800978{
979 return do_maps_open(inode, file, &proc_pid_smaps_op);
980}
981
Vlastimil Babka258f6692018-08-21 21:52:59 -0700982static int smaps_rollup_open(struct inode *inode, struct file *file)
Daniel Colascione493b0e92017-09-06 16:25:08 -0700983{
Vlastimil Babka258f6692018-08-21 21:52:59 -0700984 int ret;
Daniel Colascione493b0e92017-09-06 16:25:08 -0700985 struct proc_maps_private *priv;
Daniel Colascione493b0e92017-09-06 16:25:08 -0700986
Vlastimil Babka258f6692018-08-21 21:52:59 -0700987 priv = kzalloc(sizeof(*priv), GFP_KERNEL_ACCOUNT);
988 if (!priv)
Daniel Colascione493b0e92017-09-06 16:25:08 -0700989 return -ENOMEM;
Vlastimil Babka258f6692018-08-21 21:52:59 -0700990
991 ret = single_open(file, show_smaps_rollup, priv);
992 if (ret)
993 goto out_free;
994
995 priv->inode = inode;
996 priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
997 if (IS_ERR(priv->mm)) {
998 ret = PTR_ERR(priv->mm);
999
1000 single_release(inode, file);
1001 goto out_free;
Daniel Colascione493b0e92017-09-06 16:25:08 -07001002 }
Vlastimil Babka258f6692018-08-21 21:52:59 -07001003
Daniel Colascione493b0e92017-09-06 16:25:08 -07001004 return 0;
Vlastimil Babka258f6692018-08-21 21:52:59 -07001005
1006out_free:
1007 kfree(priv);
1008 return ret;
1009}
1010
1011static int smaps_rollup_release(struct inode *inode, struct file *file)
1012{
1013 struct seq_file *seq = file->private_data;
1014 struct proc_maps_private *priv = seq->private;
1015
1016 if (priv->mm)
1017 mmdrop(priv->mm);
1018
1019 kfree(priv);
1020 return single_release(inode, file);
Daniel Colascione493b0e92017-09-06 16:25:08 -07001021}
1022
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001023const struct file_operations proc_pid_smaps_operations = {
1024 .open = pid_smaps_open,
1025 .read = seq_read,
1026 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -07001027 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001028};
1029
Daniel Colascione493b0e92017-09-06 16:25:08 -07001030const struct file_operations proc_pid_smaps_rollup_operations = {
Vlastimil Babka258f6692018-08-21 21:52:59 -07001031 .open = smaps_rollup_open,
Daniel Colascione493b0e92017-09-06 16:25:08 -07001032 .read = seq_read,
1033 .llseek = seq_lseek,
Vlastimil Babka258f6692018-08-21 21:52:59 -07001034 .release = smaps_rollup_release,
Daniel Colascione493b0e92017-09-06 16:25:08 -07001035};
1036
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001037enum clear_refs_types {
1038 CLEAR_REFS_ALL = 1,
1039 CLEAR_REFS_ANON,
1040 CLEAR_REFS_MAPPED,
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001041 CLEAR_REFS_SOFT_DIRTY,
Petr Cermak695f0552015-02-12 15:01:00 -08001042 CLEAR_REFS_MM_HIWATER_RSS,
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001043 CLEAR_REFS_LAST,
1044};
1045
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001046struct clear_refs_private {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001047 enum clear_refs_types type;
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001048};
1049
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001050#ifdef CONFIG_MEM_SOFT_DIRTY
Linus Torvalds9348b732021-01-09 17:09:10 -08001051
Linus Torvalds9348b732021-01-09 17:09:10 -08001052static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
1053{
1054 struct page *page;
1055
1056 if (!pte_write(pte))
1057 return false;
1058 if (!is_cow_mapping(vma->vm_flags))
1059 return false;
Andrea Arcangelia458b762021-06-28 19:36:40 -07001060 if (likely(!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags)))
Linus Torvalds9348b732021-01-09 17:09:10 -08001061 return false;
1062 page = vm_normal_page(vma, addr, pte);
1063 if (!page)
1064 return false;
1065 return page_maybe_dma_pinned(page);
1066}
1067
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001068static inline void clear_soft_dirty(struct vm_area_struct *vma,
1069 unsigned long addr, pte_t *pte)
1070{
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001071 /*
1072 * The soft-dirty tracker uses #PF-s to catch writes
1073 * to pages, so write-protect the pte as well. See the
Mike Rapoport1ad13352018-04-18 11:07:49 +03001074 * Documentation/admin-guide/mm/soft-dirty.rst for full description
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001075 * of how soft-dirty works.
1076 */
1077 pte_t ptent = *pte;
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001078
1079 if (pte_present(ptent)) {
Aneesh Kumar K.V04a86452019-03-05 15:46:29 -08001080 pte_t old_pte;
1081
Linus Torvalds9348b732021-01-09 17:09:10 -08001082 if (pte_is_pinned(vma, addr, ptent))
1083 return;
Aneesh Kumar K.V04a86452019-03-05 15:46:29 -08001084 old_pte = ptep_modify_prot_start(vma, addr, pte);
1085 ptent = pte_wrprotect(old_pte);
Martin Schwidefskya7b76172015-04-22 14:20:47 +02001086 ptent = pte_clear_soft_dirty(ptent);
Aneesh Kumar K.V04a86452019-03-05 15:46:29 -08001087 ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001088 } else if (is_swap_pte(ptent)) {
1089 ptent = pte_swp_clear_soft_dirty(ptent);
Laurent Dufour326c2592015-11-05 18:49:21 -08001090 set_pte_at(vma->vm_mm, addr, pte, ptent);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001091 }
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001092}
Laurent Dufour5d3875a2015-11-05 18:49:24 -08001093#else
1094static inline void clear_soft_dirty(struct vm_area_struct *vma,
1095 unsigned long addr, pte_t *pte)
1096{
1097}
1098#endif
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001099
Laurent Dufour5d3875a2015-11-05 18:49:24 -08001100#if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001101static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1102 unsigned long addr, pmd_t *pmdp)
1103{
Kirill A. Shutemova3cf9882018-01-31 16:18:20 -08001104 pmd_t old, pmd = *pmdp;
Kirill A. Shutemov5b7abea2017-04-13 14:56:28 -07001105
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001106 if (pmd_present(pmd)) {
1107 /* See comment in change_huge_pmd() */
Kirill A. Shutemova3cf9882018-01-31 16:18:20 -08001108 old = pmdp_invalidate(vma, addr, pmdp);
1109 if (pmd_dirty(old))
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001110 pmd = pmd_mkdirty(pmd);
Kirill A. Shutemova3cf9882018-01-31 16:18:20 -08001111 if (pmd_young(old))
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001112 pmd = pmd_mkyoung(pmd);
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001113
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001114 pmd = pmd_wrprotect(pmd);
1115 pmd = pmd_clear_soft_dirty(pmd);
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001116
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001117 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1118 } else if (is_migration_entry(pmd_to_swp_entry(pmd))) {
1119 pmd = pmd_swp_clear_soft_dirty(pmd);
1120 set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
1121 }
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001122}
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001123#else
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001124static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
1125 unsigned long addr, pmd_t *pmdp)
1126{
1127}
1128#endif
1129
Matt Mackalla6198792008-02-04 22:29:03 -08001130static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
Dave Hansen21650092008-06-12 15:21:47 -07001131 unsigned long end, struct mm_walk *walk)
Matt Mackalla6198792008-02-04 22:29:03 -08001132{
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001133 struct clear_refs_private *cp = walk->private;
Naoya Horiguchi5c64f522015-02-11 15:27:46 -08001134 struct vm_area_struct *vma = walk->vma;
Matt Mackalla6198792008-02-04 22:29:03 -08001135 pte_t *pte, ptent;
1136 spinlock_t *ptl;
1137 struct page *page;
1138
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001139 ptl = pmd_trans_huge_lock(pmd, vma);
1140 if (ptl) {
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001141 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1142 clear_soft_dirty_pmd(vma, addr, pmd);
1143 goto out;
1144 }
1145
Zi Yan84c3fc42017-09-08 16:11:01 -07001146 if (!pmd_present(*pmd))
1147 goto out;
1148
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001149 page = pmd_page(*pmd);
1150
1151 /* Clear accessed and referenced bits. */
1152 pmdp_test_and_clear_young(vma, addr, pmd);
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07001153 test_and_clear_page_young(page);
Kirill A. Shutemov7d5b3bf2015-02-11 15:28:08 -08001154 ClearPageReferenced(page);
1155out:
1156 spin_unlock(ptl);
1157 return 0;
1158 }
1159
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001160 if (pmd_trans_unstable(pmd))
1161 return 0;
Dave Hansen03319322011-03-22 16:32:56 -07001162
Matt Mackalla6198792008-02-04 22:29:03 -08001163 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
1164 for (; addr != end; pte++, addr += PAGE_SIZE) {
1165 ptent = *pte;
Matt Mackalla6198792008-02-04 22:29:03 -08001166
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001167 if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
1168 clear_soft_dirty(vma, addr, pte);
1169 continue;
1170 }
1171
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001172 if (!pte_present(ptent))
1173 continue;
1174
Matt Mackalla6198792008-02-04 22:29:03 -08001175 page = vm_normal_page(vma, addr, ptent);
1176 if (!page)
1177 continue;
1178
1179 /* Clear accessed and referenced bits. */
1180 ptep_test_and_clear_young(vma, addr, pte);
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07001181 test_and_clear_page_young(page);
Matt Mackalla6198792008-02-04 22:29:03 -08001182 ClearPageReferenced(page);
1183 }
1184 pte_unmap_unlock(pte - 1, ptl);
1185 cond_resched();
1186 return 0;
1187}
1188
Naoya Horiguchi5c64f522015-02-11 15:27:46 -08001189static int clear_refs_test_walk(unsigned long start, unsigned long end,
1190 struct mm_walk *walk)
1191{
1192 struct clear_refs_private *cp = walk->private;
1193 struct vm_area_struct *vma = walk->vma;
1194
Naoya Horiguchi48684a62015-02-11 15:28:06 -08001195 if (vma->vm_flags & VM_PFNMAP)
1196 return 1;
1197
Naoya Horiguchi5c64f522015-02-11 15:27:46 -08001198 /*
1199 * Writing 1 to /proc/pid/clear_refs affects all pages.
1200 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
1201 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
1202 * Writing 4 to /proc/pid/clear_refs affects all pages.
1203 */
1204 if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
1205 return 1;
1206 if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
1207 return 1;
1208 return 0;
1209}
1210
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001211static const struct mm_walk_ops clear_refs_walk_ops = {
1212 .pmd_entry = clear_refs_pte_range,
1213 .test_walk = clear_refs_test_walk,
1214};
1215
Matt Mackallf248dcb2008-02-04 22:29:03 -08001216static ssize_t clear_refs_write(struct file *file, const char __user *buf,
1217 size_t count, loff_t *ppos)
David Rientjesb813e932007-05-06 14:49:24 -07001218{
Matt Mackallf248dcb2008-02-04 22:29:03 -08001219 struct task_struct *task;
Vincent Lifb92a4b2009-09-22 16:45:36 -07001220 char buffer[PROC_NUMBUF];
Matt Mackallf248dcb2008-02-04 22:29:03 -08001221 struct mm_struct *mm;
David Rientjesb813e932007-05-06 14:49:24 -07001222 struct vm_area_struct *vma;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001223 enum clear_refs_types type;
1224 int itype;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001225 int rv;
David Rientjesb813e932007-05-06 14:49:24 -07001226
Matt Mackallf248dcb2008-02-04 22:29:03 -08001227 memset(buffer, 0, sizeof(buffer));
1228 if (count > sizeof(buffer) - 1)
1229 count = sizeof(buffer) - 1;
1230 if (copy_from_user(buffer, buf, count))
1231 return -EFAULT;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001232 rv = kstrtoint(strstrip(buffer), 10, &itype);
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001233 if (rv < 0)
1234 return rv;
Pavel Emelyanov040fa022013-07-03 15:01:16 -07001235 type = (enum clear_refs_types)itype;
1236 if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
Matt Mackallf248dcb2008-02-04 22:29:03 -08001237 return -EINVAL;
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001238
Al Viro496ad9a2013-01-23 17:07:38 -05001239 task = get_proc_task(file_inode(file));
Matt Mackallf248dcb2008-02-04 22:29:03 -08001240 if (!task)
1241 return -ESRCH;
1242 mm = get_task_mm(task);
1243 if (mm) {
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001244 struct mmu_notifier_range range;
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001245 struct clear_refs_private cp = {
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001246 .type = type,
Pavel Emelyanovaf9de7e2013-07-03 15:01:18 -07001247 };
Petr Cermak695f0552015-02-12 15:01:00 -08001248
Linus Torvalds29a951d2021-01-08 13:13:41 -08001249 if (mmap_write_lock_killable(mm)) {
1250 count = -EINTR;
1251 goto out_mm;
1252 }
Petr Cermak695f0552015-02-12 15:01:00 -08001253 if (type == CLEAR_REFS_MM_HIWATER_RSS) {
1254 /*
1255 * Writing 5 to /proc/pid/clear_refs resets the peak
1256 * resident set size to this mm's current rss value.
1257 */
Petr Cermak695f0552015-02-12 15:01:00 -08001258 reset_mm_hiwater_rss(mm);
Linus Torvalds29a951d2021-01-08 13:13:41 -08001259 goto out_unlock;
Petr Cermak695f0552015-02-12 15:01:00 -08001260 }
1261
Peter Feiner64e455072014-10-13 15:55:46 -07001262 if (type == CLEAR_REFS_SOFT_DIRTY) {
1263 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1264 if (!(vma->vm_flags & VM_SOFTDIRTY))
1265 continue;
Linus Torvalds29a951d2021-01-08 13:13:41 -08001266 vma->vm_flags &= ~VM_SOFTDIRTY;
1267 vma_set_page_prot(vma);
Peter Feiner64e455072014-10-13 15:55:46 -07001268 }
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001269
Will Deacon912efa12021-01-27 23:53:42 +00001270 inc_tlb_flush_pending(mm);
Jérôme Glisse7269f992019-05-13 17:20:53 -07001271 mmu_notifier_range_init(&range, MMU_NOTIFY_SOFT_DIRTY,
1272 0, NULL, mm, 0, -1UL);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001273 mmu_notifier_invalidate_range_start(&range);
Peter Feiner64e455072014-10-13 15:55:46 -07001274 }
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001275 walk_page_range(mm, 0, mm->highest_vm_end, &clear_refs_walk_ops,
1276 &cp);
Will Deacon912efa12021-01-27 23:53:42 +00001277 if (type == CLEAR_REFS_SOFT_DIRTY) {
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001278 mmu_notifier_invalidate_range_end(&range);
Will Deacon912efa12021-01-27 23:53:42 +00001279 flush_tlb_mm(mm);
1280 dec_tlb_flush_pending(mm);
1281 }
Linus Torvalds29a951d2021-01-08 13:13:41 -08001282out_unlock:
1283 mmap_write_unlock(mm);
Petr Cermak695f0552015-02-12 15:01:00 -08001284out_mm:
Matt Mackallf248dcb2008-02-04 22:29:03 -08001285 mmput(mm);
1286 }
1287 put_task_struct(task);
Vincent Lifb92a4b2009-09-22 16:45:36 -07001288
1289 return count;
David Rientjesb813e932007-05-06 14:49:24 -07001290}
1291
Matt Mackallf248dcb2008-02-04 22:29:03 -08001292const struct file_operations proc_clear_refs_operations = {
1293 .write = clear_refs_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001294 .llseek = noop_llseek,
Matt Mackallf248dcb2008-02-04 22:29:03 -08001295};
1296
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001297typedef struct {
1298 u64 pme;
1299} pagemap_entry_t;
1300
Matt Mackall85863e42008-02-04 22:29:04 -08001301struct pagemapread {
yonghua zheng8c829622013-08-13 16:01:03 -07001302 int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001303 pagemap_entry_t *buffer;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001304 bool show_pfn;
Matt Mackall85863e42008-02-04 22:29:04 -08001305};
1306
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001307#define PAGEMAP_WALK_SIZE (PMD_SIZE)
1308#define PAGEMAP_WALK_MASK (PMD_MASK)
1309
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001310#define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
1311#define PM_PFRAME_BITS 55
1312#define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
1313#define PM_SOFT_DIRTY BIT_ULL(55)
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001314#define PM_MMAP_EXCLUSIVE BIT_ULL(56)
Peter Xufb8e37f2021-06-30 18:49:09 -07001315#define PM_UFFD_WP BIT_ULL(57)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001316#define PM_FILE BIT_ULL(61)
1317#define PM_SWAP BIT_ULL(62)
1318#define PM_PRESENT BIT_ULL(63)
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001319
Matt Mackall85863e42008-02-04 22:29:04 -08001320#define PM_END_OF_BUFFER 1
1321
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001322static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001323{
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001324 return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001325}
1326
1327static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
Matt Mackall85863e42008-02-04 22:29:04 -08001328 struct pagemapread *pm)
1329{
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001330 pm->buffer[pm->pos++] = *pme;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001331 if (pm->pos >= pm->len)
Thomas Tuttleaae86792008-06-05 22:46:31 -07001332 return PM_END_OF_BUFFER;
Matt Mackall85863e42008-02-04 22:29:04 -08001333 return 0;
1334}
1335
1336static int pagemap_pte_hole(unsigned long start, unsigned long end,
Steven Priceb7a16c72020-02-03 17:36:03 -08001337 __always_unused int depth, struct mm_walk *walk)
Matt Mackall85863e42008-02-04 22:29:04 -08001338{
Dave Hansen21650092008-06-12 15:21:47 -07001339 struct pagemapread *pm = walk->private;
Peter Feiner68b5a652014-08-06 16:08:09 -07001340 unsigned long addr = start;
Matt Mackall85863e42008-02-04 22:29:04 -08001341 int err = 0;
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001342
Peter Feiner68b5a652014-08-06 16:08:09 -07001343 while (addr < end) {
1344 struct vm_area_struct *vma = find_vma(walk->mm, addr);
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001345 pagemap_entry_t pme = make_pme(0, 0);
Peter Feiner87e6d492014-09-25 16:05:18 -07001346 /* End of address space hole, which we mark as non-present. */
1347 unsigned long hole_end;
Peter Feiner68b5a652014-08-06 16:08:09 -07001348
Peter Feiner87e6d492014-09-25 16:05:18 -07001349 if (vma)
1350 hole_end = min(end, vma->vm_start);
1351 else
1352 hole_end = end;
1353
1354 for (; addr < hole_end; addr += PAGE_SIZE) {
1355 err = add_to_pagemap(addr, &pme, pm);
1356 if (err)
1357 goto out;
Peter Feiner68b5a652014-08-06 16:08:09 -07001358 }
1359
Peter Feiner87e6d492014-09-25 16:05:18 -07001360 if (!vma)
1361 break;
1362
1363 /* Addresses in the VMA. */
1364 if (vma->vm_flags & VM_SOFTDIRTY)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001365 pme = make_pme(0, PM_SOFT_DIRTY);
Peter Feiner87e6d492014-09-25 16:05:18 -07001366 for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
Peter Feiner68b5a652014-08-06 16:08:09 -07001367 err = add_to_pagemap(addr, &pme, pm);
1368 if (err)
1369 goto out;
1370 }
Matt Mackall85863e42008-02-04 22:29:04 -08001371 }
Peter Feiner68b5a652014-08-06 16:08:09 -07001372out:
Matt Mackall85863e42008-02-04 22:29:04 -08001373 return err;
1374}
1375
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001376static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001377 struct vm_area_struct *vma, unsigned long addr, pte_t pte)
Matt Mackall85863e42008-02-04 22:29:04 -08001378{
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001379 u64 frame = 0, flags = 0;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001380 struct page *page = NULL;
Matt Mackall85863e42008-02-04 22:29:04 -08001381
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001382 if (pte_present(pte)) {
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001383 if (pm->show_pfn)
1384 frame = pte_pfn(pte);
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001385 flags |= PM_PRESENT;
Christoph Hellwig25b29952019-06-13 22:50:49 +02001386 page = vm_normal_page(vma, addr, pte);
Cyrill Gorcunove9cdd6e2013-10-16 13:46:53 -07001387 if (pte_soft_dirty(pte))
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001388 flags |= PM_SOFT_DIRTY;
Peter Xufb8e37f2021-06-30 18:49:09 -07001389 if (pte_uffd_wp(pte))
1390 flags |= PM_UFFD_WP;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001391 } else if (is_swap_pte(pte)) {
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001392 swp_entry_t entry;
1393 if (pte_swp_soft_dirty(pte))
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001394 flags |= PM_SOFT_DIRTY;
Peter Xufb8e37f2021-06-30 18:49:09 -07001395 if (pte_swp_uffd_wp(pte))
1396 flags |= PM_UFFD_WP;
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001397 entry = pte_to_swp_entry(pte);
Huang Yingab6ecf22018-06-07 17:07:39 -07001398 if (pm->show_pfn)
1399 frame = swp_type(entry) |
1400 (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001401 flags |= PM_SWAP;
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -07001402 if (is_pfn_swap_entry(entry))
1403 page = pfn_swap_entry_to_page(entry);
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001404 }
1405
1406 if (page && !PageAnon(page))
1407 flags |= PM_FILE;
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001408 if (page && page_mapcount(page) == 1)
1409 flags |= PM_MMAP_EXCLUSIVE;
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001410 if (vma->vm_flags & VM_SOFTDIRTY)
1411 flags |= PM_SOFT_DIRTY;
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001412
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001413 return make_pme(frame, flags);
Dave Hansenbcf80392008-06-12 15:21:48 -07001414}
1415
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001416static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
Dave Hansen21650092008-06-12 15:21:47 -07001417 struct mm_walk *walk)
Matt Mackall85863e42008-02-04 22:29:04 -08001418{
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001419 struct vm_area_struct *vma = walk->vma;
Dave Hansen21650092008-06-12 15:21:47 -07001420 struct pagemapread *pm = walk->private;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001421 spinlock_t *ptl;
Konstantin Khlebnikov05fbf352015-02-11 15:27:31 -08001422 pte_t *pte, *orig_pte;
Matt Mackall85863e42008-02-04 22:29:04 -08001423 int err = 0;
1424
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001425#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001426 ptl = pmd_trans_huge_lock(pmdp, vma);
1427 if (ptl) {
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001428 u64 flags = 0, frame = 0;
1429 pmd_t pmd = *pmdp;
Zi Yan84c3fc42017-09-08 16:11:01 -07001430 struct page *page = NULL;
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001431
Huang Yingb83d7e42017-11-02 15:59:34 -07001432 if (vma->vm_flags & VM_SOFTDIRTY)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001433 flags |= PM_SOFT_DIRTY;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001434
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001435 if (pmd_present(pmd)) {
Zi Yan84c3fc42017-09-08 16:11:01 -07001436 page = pmd_page(pmd);
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001437
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001438 flags |= PM_PRESENT;
Huang Yingb83d7e42017-11-02 15:59:34 -07001439 if (pmd_soft_dirty(pmd))
1440 flags |= PM_SOFT_DIRTY;
Peter Xufb8e37f2021-06-30 18:49:09 -07001441 if (pmd_uffd_wp(pmd))
1442 flags |= PM_UFFD_WP;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001443 if (pm->show_pfn)
1444 frame = pmd_pfn(pmd) +
1445 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001446 }
Zi Yan84c3fc42017-09-08 16:11:01 -07001447#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1448 else if (is_swap_pmd(pmd)) {
1449 swp_entry_t entry = pmd_to_swp_entry(pmd);
Huang Yingab6ecf22018-06-07 17:07:39 -07001450 unsigned long offset;
Zi Yan84c3fc42017-09-08 16:11:01 -07001451
Huang Yingab6ecf22018-06-07 17:07:39 -07001452 if (pm->show_pfn) {
1453 offset = swp_offset(entry) +
1454 ((addr & ~PMD_MASK) >> PAGE_SHIFT);
1455 frame = swp_type(entry) |
1456 (offset << MAX_SWAPFILES_SHIFT);
1457 }
Zi Yan84c3fc42017-09-08 16:11:01 -07001458 flags |= PM_SWAP;
Huang Yingb83d7e42017-11-02 15:59:34 -07001459 if (pmd_swp_soft_dirty(pmd))
1460 flags |= PM_SOFT_DIRTY;
Peter Xufb8e37f2021-06-30 18:49:09 -07001461 if (pmd_swp_uffd_wp(pmd))
1462 flags |= PM_UFFD_WP;
Zi Yan84c3fc42017-09-08 16:11:01 -07001463 VM_BUG_ON(!is_pmd_migration_entry(pmd));
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -07001464 page = pfn_swap_entry_to_page(entry);
Zi Yan84c3fc42017-09-08 16:11:01 -07001465 }
1466#endif
1467
1468 if (page && page_mapcount(page) == 1)
1469 flags |= PM_MMAP_EXCLUSIVE;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001470
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001471 for (; addr != end; addr += PAGE_SIZE) {
1472 pagemap_entry_t pme = make_pme(frame, flags);
1473
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001474 err = add_to_pagemap(addr, &pme, pm);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001475 if (err)
1476 break;
Huang Yingab6ecf22018-06-07 17:07:39 -07001477 if (pm->show_pfn) {
1478 if (flags & PM_PRESENT)
1479 frame++;
1480 else if (flags & PM_SWAP)
1481 frame += (1 << MAX_SWAPFILES_SHIFT);
1482 }
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001483 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001484 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001485 return err;
Naoya Horiguchi5aaabe82012-03-21 16:33:57 -07001486 }
1487
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001488 if (pmd_trans_unstable(pmdp))
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07001489 return 0;
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001490#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Dave Hansenbcf80392008-06-12 15:21:48 -07001491
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001492 /*
1493 * We can assume that @vma always points to a valid one and @end never
1494 * goes beyond vma->vm_end.
1495 */
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001496 orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001497 for (; addr < end; pte++, addr += PAGE_SIZE) {
1498 pagemap_entry_t pme;
Peter Feiner81d0fa62014-10-09 15:28:32 -07001499
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001500 pme = pte_to_pagemap_entry(pm, vma, addr, *pte);
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001501 err = add_to_pagemap(addr, &pme, pm);
Konstantin Khlebnikov05fbf352015-02-11 15:27:31 -08001502 if (err)
Peter Feiner81d0fa62014-10-09 15:28:32 -07001503 break;
Matt Mackall85863e42008-02-04 22:29:04 -08001504 }
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001505 pte_unmap_unlock(orig_pte, ptl);
Matt Mackall85863e42008-02-04 22:29:04 -08001506
1507 cond_resched();
1508
1509 return err;
1510}
1511
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001512#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi116354d2010-04-06 14:35:04 -07001513/* This function walks within one hugetlb entry in the single call */
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001514static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
Naoya Horiguchi116354d2010-04-06 14:35:04 -07001515 unsigned long addr, unsigned long end,
1516 struct mm_walk *walk)
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001517{
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001518 struct pagemapread *pm = walk->private;
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001519 struct vm_area_struct *vma = walk->vma;
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001520 u64 flags = 0, frame = 0;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001521 int err = 0;
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001522 pte_t pte;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001523
Naoya Horiguchif995ece2015-02-11 15:27:48 -08001524 if (vma->vm_flags & VM_SOFTDIRTY)
Konstantin Khlebnikovdeb94542015-09-08 15:00:02 -07001525 flags |= PM_SOFT_DIRTY;
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001526
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001527 pte = huge_ptep_get(ptep);
1528 if (pte_present(pte)) {
1529 struct page *page = pte_page(pte);
1530
1531 if (!PageAnon(page))
1532 flags |= PM_FILE;
1533
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001534 if (page_mapcount(page) == 1)
1535 flags |= PM_MMAP_EXCLUSIVE;
1536
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001537 flags |= PM_PRESENT;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001538 if (pm->show_pfn)
1539 frame = pte_pfn(pte) +
1540 ((addr & ~hmask) >> PAGE_SHIFT);
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001541 }
1542
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001543 for (; addr != end; addr += PAGE_SIZE) {
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001544 pagemap_entry_t pme = make_pme(frame, flags);
1545
Naoya Horiguchi092b50b2012-03-21 16:33:59 -07001546 err = add_to_pagemap(addr, &pme, pm);
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001547 if (err)
1548 return err;
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001549 if (pm->show_pfn && (flags & PM_PRESENT))
Konstantin Khlebnikov356515e2015-09-08 15:00:04 -07001550 frame++;
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001551 }
1552
1553 cond_resched();
1554
1555 return err;
1556}
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001557#else
1558#define pagemap_hugetlb_range NULL
Naoya Horiguchi1a5cb812010-05-24 14:32:12 -07001559#endif /* HUGETLB_PAGE */
Naoya Horiguchi5dc37642009-12-14 18:00:01 -08001560
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001561static const struct mm_walk_ops pagemap_ops = {
1562 .pmd_entry = pagemap_pmd_range,
1563 .pte_hole = pagemap_pte_hole,
1564 .hugetlb_entry = pagemap_hugetlb_range,
1565};
1566
Matt Mackall85863e42008-02-04 22:29:04 -08001567/*
1568 * /proc/pid/pagemap - an array mapping virtual pages to pfns
1569 *
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001570 * For each page in the address space, this file contains one 64-bit entry
1571 * consisting of the following:
1572 *
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001573 * Bits 0-54 page frame number (PFN) if present
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001574 * Bits 0-4 swap type if swapped
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001575 * Bits 5-54 swap offset if swapped
Mike Rapoport1ad13352018-04-18 11:07:49 +03001576 * Bit 55 pte is soft-dirty (see Documentation/admin-guide/mm/soft-dirty.rst)
Konstantin Khlebnikov77bb4992015-09-08 15:00:10 -07001577 * Bit 56 page exclusively mapped
1578 * Bits 57-60 zero
Konstantin Khlebnikov052fb0d2012-05-31 16:26:19 -07001579 * Bit 61 page is file-page or shared-anon
Hans Rosenfeldf16278c2008-03-21 18:46:59 -05001580 * Bit 62 page swapped
1581 * Bit 63 page present
1582 *
1583 * If the page is not present but in swap, then the PFN contains an
1584 * encoding of the swap file number and the page's offset into the
1585 * swap. Unmapped pages return a null PFN. This allows determining
Matt Mackall85863e42008-02-04 22:29:04 -08001586 * precisely which pages are mapped (or in swap) and comparing mapped
1587 * pages between processes.
1588 *
1589 * Efficient users of this interface will use /proc/pid/maps to
1590 * determine which areas of memory are actually mapped and llseek to
1591 * skip over unmapped regions.
1592 */
1593static ssize_t pagemap_read(struct file *file, char __user *buf,
1594 size_t count, loff_t *ppos)
1595{
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001596 struct mm_struct *mm = file->private_data;
Matt Mackall85863e42008-02-04 22:29:04 -08001597 struct pagemapread pm;
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001598 unsigned long src;
1599 unsigned long svpfn;
1600 unsigned long start_vaddr;
1601 unsigned long end_vaddr;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001602 int ret = 0, copied = 0;
Matt Mackall85863e42008-02-04 22:29:04 -08001603
Vegard Nossum388f7932017-02-27 14:30:13 -08001604 if (!mm || !mmget_not_zero(mm))
Matt Mackall85863e42008-02-04 22:29:04 -08001605 goto out;
1606
Matt Mackall85863e42008-02-04 22:29:04 -08001607 ret = -EINVAL;
1608 /* file position must be aligned */
Thomas Tuttleaae86792008-06-05 22:46:31 -07001609 if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001610 goto out_mm;
Matt Mackall85863e42008-02-04 22:29:04 -08001611
1612 ret = 0;
Vitaly Mayatskikh08161782009-04-30 15:08:18 -07001613 if (!count)
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001614 goto out_mm;
Vitaly Mayatskikh08161782009-04-30 15:08:18 -07001615
Konstantin Khlebnikov1c903082015-09-08 15:00:07 -07001616 /* do not disclose physical addresses: attack vector */
1617 pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
1618
yonghua zheng8c829622013-08-13 16:01:03 -07001619 pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
Kees Cook6da2ec52018-06-12 13:55:00 -07001620 pm.buffer = kmalloc_array(pm.len, PM_ENTRY_BYTES, GFP_KERNEL);
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001621 ret = -ENOMEM;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001622 if (!pm.buffer)
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001623 goto out_mm;
Matt Mackall85863e42008-02-04 22:29:04 -08001624
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001625 src = *ppos;
1626 svpfn = src / PM_ENTRY_BYTES;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001627 end_vaddr = mm->task_size;
Matt Mackall85863e42008-02-04 22:29:04 -08001628
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001629 /* watch out for wraparound */
Miles Chen40d63662020-12-11 13:36:31 -08001630 start_vaddr = end_vaddr;
1631 if (svpfn <= (ULONG_MAX >> PAGE_SHIFT))
1632 start_vaddr = untagged_addr(svpfn << PAGE_SHIFT);
1633
1634 /* Ensure the address is inside the task */
1635 if (start_vaddr > mm->task_size)
Andrew Morton5d7e0d22008-07-05 01:02:01 -07001636 start_vaddr = end_vaddr;
1637
1638 /*
1639 * The odds are that this will stop walking way
1640 * before end_vaddr, because the length of the
1641 * user buffer is tracked in "pm", and the walk
1642 * will stop when we hit the end of the buffer.
1643 */
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001644 ret = 0;
1645 while (count && (start_vaddr < end_vaddr)) {
1646 int len;
1647 unsigned long end;
Matt Mackall85863e42008-02-04 22:29:04 -08001648
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001649 pm.pos = 0;
Naoya Horiguchiea251c12010-11-24 12:57:13 -08001650 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001651 /* overflow ? */
1652 if (end < start_vaddr || end > end_vaddr)
1653 end = end_vaddr;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001654 ret = mmap_read_lock_killable(mm);
Konstantin Khlebnikovad80b9322019-07-11 20:59:56 -07001655 if (ret)
1656 goto out_free;
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001657 ret = walk_page_range(mm, start_vaddr, end, &pagemap_ops, &pm);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001658 mmap_read_unlock(mm);
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001659 start_vaddr = end;
1660
1661 len = min(count, PM_ENTRY_BYTES * pm.pos);
Dan Carpenter309361e02010-04-06 13:45:39 +03001662 if (copy_to_user(buf, pm.buffer, len)) {
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001663 ret = -EFAULT;
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001664 goto out_free;
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001665 }
1666 copied += len;
1667 buf += len;
1668 count -= len;
Matt Mackall85863e42008-02-04 22:29:04 -08001669 }
KAMEZAWA Hiroyukid82ef022010-04-02 09:11:29 +09001670 *ppos += copied;
1671 if (!ret || ret == PM_END_OF_BUFFER)
1672 ret = copied;
1673
KOSAKI Motohiro98bc93e2011-05-26 16:25:53 -07001674out_free:
1675 kfree(pm.buffer);
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001676out_mm:
1677 mmput(mm);
Matt Mackall85863e42008-02-04 22:29:04 -08001678out:
1679 return ret;
1680}
1681
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001682static int pagemap_open(struct inode *inode, struct file *file)
1683{
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001684 struct mm_struct *mm;
1685
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001686 mm = proc_mem_open(inode, PTRACE_MODE_READ);
1687 if (IS_ERR(mm))
1688 return PTR_ERR(mm);
1689 file->private_data = mm;
1690 return 0;
1691}
1692
1693static int pagemap_release(struct inode *inode, struct file *file)
1694{
1695 struct mm_struct *mm = file->private_data;
1696
1697 if (mm)
1698 mmdrop(mm);
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001699 return 0;
1700}
1701
Matt Mackall85863e42008-02-04 22:29:04 -08001702const struct file_operations proc_pagemap_operations = {
1703 .llseek = mem_lseek, /* borrow this */
1704 .read = pagemap_read,
Pavel Emelyanov541c2372013-07-03 15:01:22 -07001705 .open = pagemap_open,
Konstantin Khlebnikova06db752015-09-08 14:59:59 -07001706 .release = pagemap_release,
Matt Mackall85863e42008-02-04 22:29:04 -08001707};
Matt Mackall1e883282008-02-04 22:29:07 -08001708#endif /* CONFIG_PROC_PAGE_MONITOR */
Matt Mackall85863e42008-02-04 22:29:04 -08001709
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001710#ifdef CONFIG_NUMA
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001711
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001712struct numa_maps {
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001713 unsigned long pages;
1714 unsigned long anon;
1715 unsigned long active;
1716 unsigned long writeback;
1717 unsigned long mapcount_max;
1718 unsigned long dirty;
1719 unsigned long swapcache;
1720 unsigned long node[MAX_NUMNODES];
1721};
1722
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001723struct numa_maps_private {
1724 struct proc_maps_private proc_maps;
1725 struct numa_maps md;
1726};
1727
Dave Hanseneb4866d2011-09-20 15:19:38 -07001728static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
1729 unsigned long nr_pages)
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001730{
1731 int count = page_mapcount(page);
1732
Dave Hanseneb4866d2011-09-20 15:19:38 -07001733 md->pages += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001734 if (pte_dirty || PageDirty(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001735 md->dirty += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001736
1737 if (PageSwapCache(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001738 md->swapcache += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001739
1740 if (PageActive(page) || PageUnevictable(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001741 md->active += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001742
1743 if (PageWriteback(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001744 md->writeback += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001745
1746 if (PageAnon(page))
Dave Hanseneb4866d2011-09-20 15:19:38 -07001747 md->anon += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001748
1749 if (count > md->mapcount_max)
1750 md->mapcount_max = count;
1751
Dave Hanseneb4866d2011-09-20 15:19:38 -07001752 md->node[page_to_nid(page)] += nr_pages;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001753}
1754
Dave Hansen3200a8a2011-09-20 15:19:39 -07001755static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
1756 unsigned long addr)
1757{
1758 struct page *page;
1759 int nid;
1760
1761 if (!pte_present(pte))
1762 return NULL;
1763
1764 page = vm_normal_page(vma, addr, pte);
1765 if (!page)
1766 return NULL;
1767
1768 if (PageReserved(page))
1769 return NULL;
1770
1771 nid = page_to_nid(page);
Lai Jiangshan4ff1b2c2012-12-12 13:51:25 -08001772 if (!node_isset(nid, node_states[N_MEMORY]))
Dave Hansen3200a8a2011-09-20 15:19:39 -07001773 return NULL;
1774
1775 return page;
1776}
1777
Gerald Schaefer28093f92016-04-28 16:18:35 -07001778#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1779static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
1780 struct vm_area_struct *vma,
1781 unsigned long addr)
1782{
1783 struct page *page;
1784 int nid;
1785
1786 if (!pmd_present(pmd))
1787 return NULL;
1788
1789 page = vm_normal_page_pmd(vma, addr, pmd);
1790 if (!page)
1791 return NULL;
1792
1793 if (PageReserved(page))
1794 return NULL;
1795
1796 nid = page_to_nid(page);
1797 if (!node_isset(nid, node_states[N_MEMORY]))
1798 return NULL;
1799
1800 return page;
1801}
1802#endif
1803
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001804static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
1805 unsigned long end, struct mm_walk *walk)
1806{
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001807 struct numa_maps *md = walk->private;
1808 struct vm_area_struct *vma = walk->vma;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001809 spinlock_t *ptl;
1810 pte_t *orig_pte;
1811 pte_t *pte;
1812
Gerald Schaefer28093f92016-04-28 16:18:35 -07001813#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001814 ptl = pmd_trans_huge_lock(pmd, vma);
1815 if (ptl) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001816 struct page *page;
1817
Gerald Schaefer28093f92016-04-28 16:18:35 -07001818 page = can_gather_numa_stats_pmd(*pmd, vma, addr);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001819 if (page)
Gerald Schaefer28093f92016-04-28 16:18:35 -07001820 gather_stats(page, md, pmd_dirty(*pmd),
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001821 HPAGE_PMD_SIZE/PAGE_SIZE);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001822 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001823 return 0;
Dave Hansen32ef4382011-09-20 15:19:41 -07001824 }
1825
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001826 if (pmd_trans_unstable(pmd))
1827 return 0;
Gerald Schaefer28093f92016-04-28 16:18:35 -07001828#endif
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001829 orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
1830 do {
Naoya Horiguchid85f4d62015-02-11 15:27:54 -08001831 struct page *page = can_gather_numa_stats(*pte, vma, addr);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001832 if (!page)
1833 continue;
Dave Hanseneb4866d2011-09-20 15:19:38 -07001834 gather_stats(page, md, pte_dirty(*pte), 1);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001835
1836 } while (pte++, addr += PAGE_SIZE, addr != end);
1837 pte_unmap_unlock(orig_pte, ptl);
Hugh Dickinsa66c04102016-12-12 16:44:47 -08001838 cond_resched();
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001839 return 0;
1840}
1841#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi632fd602015-02-11 15:27:51 -08001842static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001843 unsigned long addr, unsigned long end, struct mm_walk *walk)
1844{
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001845 pte_t huge_pte = huge_ptep_get(pte);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001846 struct numa_maps *md;
1847 struct page *page;
1848
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001849 if (!pte_present(huge_pte))
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001850 return 0;
1851
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001852 page = pte_page(huge_pte);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001853 if (!page)
1854 return 0;
1855
1856 md = walk->private;
Michael Holzheu5c2ff952016-02-02 16:57:26 -08001857 gather_stats(page, md, pte_dirty(huge_pte), 1);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001858 return 0;
1859}
1860
1861#else
Naoya Horiguchi632fd602015-02-11 15:27:51 -08001862static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001863 unsigned long addr, unsigned long end, struct mm_walk *walk)
1864{
1865 return 0;
1866}
1867#endif
1868
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001869static const struct mm_walk_ops show_numa_ops = {
1870 .hugetlb_entry = gather_hugetlb_stats,
1871 .pmd_entry = gather_pte_stats,
1872};
1873
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001874/*
1875 * Display pages allocated per node and memory policy via /proc.
1876 */
Vlastimil Babka871305b2018-08-21 21:52:48 -07001877static int show_numa_map(struct seq_file *m, void *v)
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001878{
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001879 struct numa_maps_private *numa_priv = m->private;
1880 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001881 struct vm_area_struct *vma = v;
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001882 struct numa_maps *md = &numa_priv->md;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001883 struct file *file = vma->vm_file;
1884 struct mm_struct *mm = vma->vm_mm;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001885 struct mempolicy *pol;
David Rientjes948927e2013-11-12 15:07:28 -08001886 char buffer[64];
1887 int nid;
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001888
1889 if (!mm)
1890 return 0;
1891
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001892 /* Ensure we start with an empty set of numa_maps statistics. */
1893 memset(md, 0, sizeof(*md));
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001894
Oleg Nesterov498f2372014-10-09 15:27:52 -07001895 pol = __get_vma_policy(vma, vma->vm_start);
1896 if (pol) {
1897 mpol_to_str(buffer, sizeof(buffer), pol);
1898 mpol_cond_put(pol);
1899 } else {
1900 mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
1901 }
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001902
1903 seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1904
1905 if (file) {
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001906 seq_puts(m, " file=");
Miklos Szeredi2726d562015-06-19 10:30:28 +02001907 seq_file_path(m, file, "\n\t= ");
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001908 } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001909 seq_puts(m, " heap");
Michal Hocko1240ea02017-09-08 16:13:35 -07001910 } else if (is_stack(vma)) {
Johannes Weiner65376df2016-02-02 16:57:29 -08001911 seq_puts(m, " stack");
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001912 }
1913
Andrew Mortonfc360bd2011-10-31 17:06:32 -07001914 if (is_vm_hugetlb_page(vma))
Fabian Frederick17c2b4e2014-06-06 14:37:03 -07001915 seq_puts(m, " huge");
Andrew Mortonfc360bd2011-10-31 17:06:32 -07001916
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001917 /* mmap_lock is held by m_start */
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02001918 walk_page_vma(vma, &show_numa_ops, md);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001919
1920 if (!md->pages)
1921 goto out;
1922
1923 if (md->anon)
1924 seq_printf(m, " anon=%lu", md->anon);
1925
1926 if (md->dirty)
1927 seq_printf(m, " dirty=%lu", md->dirty);
1928
1929 if (md->pages != md->anon && md->pages != md->dirty)
1930 seq_printf(m, " mapped=%lu", md->pages);
1931
1932 if (md->mapcount_max > 1)
1933 seq_printf(m, " mapmax=%lu", md->mapcount_max);
1934
1935 if (md->swapcache)
1936 seq_printf(m, " swapcache=%lu", md->swapcache);
1937
1938 if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1939 seq_printf(m, " active=%lu", md->active);
1940
1941 if (md->writeback)
1942 seq_printf(m, " writeback=%lu", md->writeback);
1943
David Rientjes948927e2013-11-12 15:07:28 -08001944 for_each_node_state(nid, N_MEMORY)
1945 if (md->node[nid])
1946 seq_printf(m, " N%d=%lu", nid, md->node[nid]);
Rafael Aquini198d1592015-02-12 15:01:08 -08001947
1948 seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001949out:
1950 seq_putc(m, '\n');
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001951 return 0;
1952}
Stephen Wilson5b52fc82011-05-24 17:12:49 -07001953
Jan Engelhardt03a44822008-02-08 04:21:19 -08001954static const struct seq_operations proc_pid_numa_maps_op = {
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001955 .start = m_start,
1956 .next = m_next,
1957 .stop = m_stop,
Vlastimil Babka871305b2018-08-21 21:52:48 -07001958 .show = show_numa_map,
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001959};
Eric W. Biederman662795d2006-06-26 00:25:48 -07001960
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001961static int pid_numa_maps_open(struct inode *inode, struct file *file)
1962{
Vlastimil Babka871305b2018-08-21 21:52:48 -07001963 return proc_maps_open(inode, file, &proc_pid_numa_maps_op,
1964 sizeof(struct numa_maps_private));
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001965}
1966
1967const struct file_operations proc_pid_numa_maps_operations = {
1968 .open = pid_numa_maps_open,
1969 .read = seq_read,
1970 .llseek = seq_lseek,
Oleg Nesterov29a40ac2014-10-09 15:25:26 -07001971 .release = proc_map_release,
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07001972};
1973
Stephen Wilsonf69ff942011-05-24 17:12:47 -07001974#endif /* CONFIG_NUMA */