Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | #include <linux/mm.h> |
615d6e87 | 3 | #include <linux/vmacache.h> |
1da177e4 | 4 | #include <linux/hugetlb.h> |
22e057c5 | 5 | #include <linux/huge_mm.h> |
1da177e4 LT |
6 | #include <linux/mount.h> |
7 | #include <linux/seq_file.h> | |
e070ad49 | 8 | #include <linux/highmem.h> |
5096add8 | 9 | #include <linux/ptrace.h> |
5a0e3ad6 | 10 | #include <linux/slab.h> |
6e21c8f1 CL |
11 | #include <linux/pagemap.h> |
12 | #include <linux/mempolicy.h> | |
22e057c5 | 13 | #include <linux/rmap.h> |
85863e47 | 14 | #include <linux/swap.h> |
6e84f315 | 15 | #include <linux/sched/mm.h> |
85863e47 | 16 | #include <linux/swapops.h> |
0f8975ec | 17 | #include <linux/mmu_notifier.h> |
33c3fc71 | 18 | #include <linux/page_idle.h> |
6a15a370 | 19 | #include <linux/shmem_fs.h> |
b3a81d08 | 20 | #include <linux/uaccess.h> |
27cca866 | 21 | #include <linux/pkeys.h> |
e070ad49 | 22 | |
1da177e4 | 23 | #include <asm/elf.h> |
b3a81d08 | 24 | #include <asm/tlb.h> |
e070ad49 | 25 | #include <asm/tlbflush.h> |
1da177e4 LT |
26 | #include "internal.h" |
27 | ||
d1be35cb AV |
28 | #define SEQ_PUT_DEC(str, val) \ |
29 | seq_put_decimal_ull_width(m, str, (val) << (PAGE_SHIFT-10), 8) | |
df5f8314 | 30 | void task_mem(struct seq_file *m, struct mm_struct *mm) |
1da177e4 | 31 | { |
af5b0f6a | 32 | unsigned long text, lib, swap, anon, file, shmem; |
365e9c87 HD |
33 | unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss; |
34 | ||
8cee852e JM |
35 | anon = get_mm_counter(mm, MM_ANONPAGES); |
36 | file = get_mm_counter(mm, MM_FILEPAGES); | |
37 | shmem = get_mm_counter(mm, MM_SHMEMPAGES); | |
38 | ||
365e9c87 HD |
39 | /* |
40 | * Note: to minimize their overhead, mm maintains hiwater_vm and | |
41 | * hiwater_rss only when about to *lower* total_vm or rss. Any | |
42 | * collector of these hiwater stats must therefore get total_vm | |
43 | * and rss too, which will usually be the higher. Barriers? not | |
44 | * worth the effort, such snapshots can always be inconsistent. | |
45 | */ | |
46 | hiwater_vm = total_vm = mm->total_vm; | |
47 | if (hiwater_vm < mm->hiwater_vm) | |
48 | hiwater_vm = mm->hiwater_vm; | |
8cee852e | 49 | hiwater_rss = total_rss = anon + file + shmem; |
365e9c87 HD |
50 | if (hiwater_rss < mm->hiwater_rss) |
51 | hiwater_rss = mm->hiwater_rss; | |
1da177e4 | 52 | |
8526d84f KK |
53 | /* split executable areas between text and lib */ |
54 | text = PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK); | |
55 | text = min(text, mm->exec_vm << PAGE_SHIFT); | |
56 | lib = (mm->exec_vm << PAGE_SHIFT) - text; | |
57 | ||
b084d435 | 58 | swap = get_mm_counter(mm, MM_SWAPENTS); |
d1be35cb AV |
59 | SEQ_PUT_DEC("VmPeak:\t", hiwater_vm); |
60 | SEQ_PUT_DEC(" kB\nVmSize:\t", total_vm); | |
61 | SEQ_PUT_DEC(" kB\nVmLck:\t", mm->locked_vm); | |
62 | SEQ_PUT_DEC(" kB\nVmPin:\t", mm->pinned_vm); | |
63 | SEQ_PUT_DEC(" kB\nVmHWM:\t", hiwater_rss); | |
64 | SEQ_PUT_DEC(" kB\nVmRSS:\t", total_rss); | |
65 | SEQ_PUT_DEC(" kB\nRssAnon:\t", anon); | |
66 | SEQ_PUT_DEC(" kB\nRssFile:\t", file); | |
67 | SEQ_PUT_DEC(" kB\nRssShmem:\t", shmem); | |
68 | SEQ_PUT_DEC(" kB\nVmData:\t", mm->data_vm); | |
69 | SEQ_PUT_DEC(" kB\nVmStk:\t", mm->stack_vm); | |
70 | seq_put_decimal_ull_width(m, | |
71 | " kB\nVmExe:\t", text >> 10, 8); | |
72 | seq_put_decimal_ull_width(m, | |
73 | " kB\nVmLib:\t", lib >> 10, 8); | |
74 | seq_put_decimal_ull_width(m, | |
75 | " kB\nVmPTE:\t", mm_pgtables_bytes(mm) >> 10, 8); | |
76 | SEQ_PUT_DEC(" kB\nVmSwap:\t", swap); | |
77 | seq_puts(m, " kB\n"); | |
5d317b2b | 78 | hugetlb_report_usage(m, mm); |
1da177e4 | 79 | } |
d1be35cb | 80 | #undef SEQ_PUT_DEC |
1da177e4 LT |
81 | |
82 | unsigned long task_vsize(struct mm_struct *mm) | |
83 | { | |
84 | return PAGE_SIZE * mm->total_vm; | |
85 | } | |
86 | ||
a2ade7b6 AD |
87 | unsigned long task_statm(struct mm_struct *mm, |
88 | unsigned long *shared, unsigned long *text, | |
89 | unsigned long *data, unsigned long *resident) | |
1da177e4 | 90 | { |
eca56ff9 JM |
91 | *shared = get_mm_counter(mm, MM_FILEPAGES) + |
92 | get_mm_counter(mm, MM_SHMEMPAGES); | |
1da177e4 LT |
93 | *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) |
94 | >> PAGE_SHIFT; | |
84638335 | 95 | *data = mm->data_vm + mm->stack_vm; |
d559db08 | 96 | *resident = *shared + get_mm_counter(mm, MM_ANONPAGES); |
1da177e4 LT |
97 | return mm->total_vm; |
98 | } | |
99 | ||
9e781440 KH |
100 | #ifdef CONFIG_NUMA |
101 | /* | |
498f2371 | 102 | * Save get_task_policy() for show_numa_map(). |
9e781440 KH |
103 | */ |
104 | static void hold_task_mempolicy(struct proc_maps_private *priv) | |
105 | { | |
106 | struct task_struct *task = priv->task; | |
107 | ||
108 | task_lock(task); | |
498f2371 | 109 | priv->task_mempolicy = get_task_policy(task); |
9e781440 KH |
110 | mpol_get(priv->task_mempolicy); |
111 | task_unlock(task); | |
112 | } | |
113 | static void release_task_mempolicy(struct proc_maps_private *priv) | |
114 | { | |
115 | mpol_put(priv->task_mempolicy); | |
116 | } | |
117 | #else | |
118 | static void hold_task_mempolicy(struct proc_maps_private *priv) | |
119 | { | |
120 | } | |
121 | static void release_task_mempolicy(struct proc_maps_private *priv) | |
122 | { | |
123 | } | |
124 | #endif | |
125 | ||
59b4bf12 | 126 | static void vma_stop(struct proc_maps_private *priv) |
a6198797 | 127 | { |
59b4bf12 ON |
128 | struct mm_struct *mm = priv->mm; |
129 | ||
130 | release_task_mempolicy(priv); | |
131 | up_read(&mm->mmap_sem); | |
132 | mmput(mm); | |
a6198797 | 133 | } |
ec4dd3eb | 134 | |
ad2a00e4 ON |
135 | static struct vm_area_struct * |
136 | m_next_vma(struct proc_maps_private *priv, struct vm_area_struct *vma) | |
137 | { | |
138 | if (vma == priv->tail_vma) | |
139 | return NULL; | |
140 | return vma->vm_next ?: priv->tail_vma; | |
141 | } | |
142 | ||
b8c20a9b ON |
143 | static void m_cache_vma(struct seq_file *m, struct vm_area_struct *vma) |
144 | { | |
145 | if (m->count < m->size) /* vma is copied successfully */ | |
855af072 | 146 | m->version = m_next_vma(m->private, vma) ? vma->vm_end : -1UL; |
b8c20a9b ON |
147 | } |
148 | ||
0c255321 | 149 | static void *m_start(struct seq_file *m, loff_t *ppos) |
e070ad49 | 150 | { |
a6198797 | 151 | struct proc_maps_private *priv = m->private; |
b8c20a9b | 152 | unsigned long last_addr = m->version; |
a6198797 | 153 | struct mm_struct *mm; |
0c255321 ON |
154 | struct vm_area_struct *vma; |
155 | unsigned int pos = *ppos; | |
a6198797 | 156 | |
b8c20a9b ON |
157 | /* See m_cache_vma(). Zero at the start or after lseek. */ |
158 | if (last_addr == -1UL) | |
159 | return NULL; | |
160 | ||
2c03376d | 161 | priv->task = get_proc_task(priv->inode); |
a6198797 | 162 | if (!priv->task) |
ec6fd8a4 | 163 | return ERR_PTR(-ESRCH); |
a6198797 | 164 | |
29a40ace | 165 | mm = priv->mm; |
388f7934 | 166 | if (!mm || !mmget_not_zero(mm)) |
29a40ace | 167 | return NULL; |
a6198797 | 168 | |
0c255321 | 169 | down_read(&mm->mmap_sem); |
9e781440 | 170 | hold_task_mempolicy(priv); |
0c255321 | 171 | priv->tail_vma = get_gate_vma(mm); |
a6198797 | 172 | |
b8c20a9b | 173 | if (last_addr) { |
855af072 RH |
174 | vma = find_vma(mm, last_addr - 1); |
175 | if (vma && vma->vm_start <= last_addr) | |
176 | vma = m_next_vma(priv, vma); | |
177 | if (vma) | |
b8c20a9b ON |
178 | return vma; |
179 | } | |
180 | ||
181 | m->version = 0; | |
0c255321 | 182 | if (pos < mm->map_count) { |
557c2d8a ON |
183 | for (vma = mm->mmap; pos; pos--) { |
184 | m->version = vma->vm_start; | |
a6198797 | 185 | vma = vma->vm_next; |
557c2d8a | 186 | } |
a6198797 | 187 | return vma; |
0c255321 | 188 | } |
a6198797 | 189 | |
557c2d8a | 190 | /* we do not bother to update m->version in this case */ |
0c255321 ON |
191 | if (pos == mm->map_count && priv->tail_vma) |
192 | return priv->tail_vma; | |
59b4bf12 ON |
193 | |
194 | vma_stop(priv); | |
195 | return NULL; | |
a6198797 MM |
196 | } |
197 | ||
198 | static void *m_next(struct seq_file *m, void *v, loff_t *pos) | |
199 | { | |
200 | struct proc_maps_private *priv = m->private; | |
ad2a00e4 | 201 | struct vm_area_struct *next; |
a6198797 MM |
202 | |
203 | (*pos)++; | |
ad2a00e4 | 204 | next = m_next_vma(priv, v); |
59b4bf12 ON |
205 | if (!next) |
206 | vma_stop(priv); | |
207 | return next; | |
a6198797 MM |
208 | } |
209 | ||
210 | static void m_stop(struct seq_file *m, void *v) | |
211 | { | |
212 | struct proc_maps_private *priv = m->private; | |
a6198797 | 213 | |
59b4bf12 ON |
214 | if (!IS_ERR_OR_NULL(v)) |
215 | vma_stop(priv); | |
0d5f5f45 | 216 | if (priv->task) { |
a6198797 | 217 | put_task_struct(priv->task); |
0d5f5f45 ON |
218 | priv->task = NULL; |
219 | } | |
a6198797 MM |
220 | } |
221 | ||
4db7d0ee ON |
222 | static int proc_maps_open(struct inode *inode, struct file *file, |
223 | const struct seq_operations *ops, int psize) | |
224 | { | |
225 | struct proc_maps_private *priv = __seq_open_private(file, ops, psize); | |
226 | ||
227 | if (!priv) | |
228 | return -ENOMEM; | |
229 | ||
2c03376d | 230 | priv->inode = inode; |
29a40ace ON |
231 | priv->mm = proc_mem_open(inode, PTRACE_MODE_READ); |
232 | if (IS_ERR(priv->mm)) { | |
233 | int err = PTR_ERR(priv->mm); | |
234 | ||
235 | seq_release_private(inode, file); | |
236 | return err; | |
237 | } | |
238 | ||
4db7d0ee ON |
239 | return 0; |
240 | } | |
241 | ||
29a40ace ON |
242 | static int proc_map_release(struct inode *inode, struct file *file) |
243 | { | |
244 | struct seq_file *seq = file->private_data; | |
245 | struct proc_maps_private *priv = seq->private; | |
246 | ||
247 | if (priv->mm) | |
248 | mmdrop(priv->mm); | |
249 | ||
493b0e9d | 250 | kfree(priv->rollup); |
29a40ace ON |
251 | return seq_release_private(inode, file); |
252 | } | |
253 | ||
a6198797 | 254 | static int do_maps_open(struct inode *inode, struct file *file, |
03a44825 | 255 | const struct seq_operations *ops) |
a6198797 | 256 | { |
4db7d0ee ON |
257 | return proc_maps_open(inode, file, ops, |
258 | sizeof(struct proc_maps_private)); | |
a6198797 | 259 | } |
e070ad49 | 260 | |
65376df5 JW |
261 | /* |
262 | * Indicate if the VMA is a stack for the given task; for | |
263 | * /proc/PID/maps that is the stack of the main task. | |
264 | */ | |
1240ea0d | 265 | static int is_stack(struct vm_area_struct *vma) |
58cb6548 | 266 | { |
b18cb64e AL |
267 | /* |
268 | * We make no effort to guess what a given thread considers to be | |
269 | * its "stack". It's not even well-defined for programs written | |
270 | * languages like Go. | |
271 | */ | |
272 | return vma->vm_start <= vma->vm_mm->start_stack && | |
273 | vma->vm_end >= vma->vm_mm->start_stack; | |
58cb6548 ON |
274 | } |
275 | ||
493b0e9d DC |
276 | static void show_vma_header_prefix(struct seq_file *m, |
277 | unsigned long start, unsigned long end, | |
278 | vm_flags_t flags, unsigned long long pgoff, | |
279 | dev_t dev, unsigned long ino) | |
280 | { | |
281 | seq_setwidth(m, 25 + sizeof(void *) * 6 - 1); | |
0e3dc019 AV |
282 | seq_put_hex_ll(m, NULL, start, 8); |
283 | seq_put_hex_ll(m, "-", end, 8); | |
284 | seq_putc(m, ' '); | |
285 | seq_putc(m, flags & VM_READ ? 'r' : '-'); | |
286 | seq_putc(m, flags & VM_WRITE ? 'w' : '-'); | |
287 | seq_putc(m, flags & VM_EXEC ? 'x' : '-'); | |
288 | seq_putc(m, flags & VM_MAYSHARE ? 's' : 'p'); | |
289 | seq_put_hex_ll(m, " ", pgoff, 8); | |
290 | seq_put_hex_ll(m, " ", MAJOR(dev), 2); | |
291 | seq_put_hex_ll(m, ":", MINOR(dev), 2); | |
292 | seq_put_decimal_ull(m, " ", ino); | |
293 | seq_putc(m, ' '); | |
493b0e9d DC |
294 | } |
295 | ||
b7643757 SP |
296 | static void |
297 | show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid) | |
1da177e4 | 298 | { |
e070ad49 ML |
299 | struct mm_struct *mm = vma->vm_mm; |
300 | struct file *file = vma->vm_file; | |
ca16d140 | 301 | vm_flags_t flags = vma->vm_flags; |
1da177e4 | 302 | unsigned long ino = 0; |
6260a4b0 | 303 | unsigned long long pgoff = 0; |
a09a79f6 | 304 | unsigned long start, end; |
1da177e4 | 305 | dev_t dev = 0; |
b7643757 | 306 | const char *name = NULL; |
1da177e4 LT |
307 | |
308 | if (file) { | |
496ad9aa | 309 | struct inode *inode = file_inode(vma->vm_file); |
1da177e4 LT |
310 | dev = inode->i_sb->s_dev; |
311 | ino = inode->i_ino; | |
6260a4b0 | 312 | pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT; |
1da177e4 LT |
313 | } |
314 | ||
d7824370 | 315 | start = vma->vm_start; |
a09a79f6 | 316 | end = vma->vm_end; |
493b0e9d | 317 | show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino); |
1da177e4 LT |
318 | |
319 | /* | |
320 | * Print the dentry name for named mappings, and a | |
321 | * special [heap] marker for the heap: | |
322 | */ | |
e070ad49 | 323 | if (file) { |
652586df | 324 | seq_pad(m, ' '); |
2726d566 | 325 | seq_file_path(m, file, "\n"); |
b7643757 SP |
326 | goto done; |
327 | } | |
328 | ||
78d683e8 AL |
329 | if (vma->vm_ops && vma->vm_ops->name) { |
330 | name = vma->vm_ops->name(vma); | |
331 | if (name) | |
332 | goto done; | |
333 | } | |
334 | ||
b7643757 SP |
335 | name = arch_vma_name(vma); |
336 | if (!name) { | |
b7643757 SP |
337 | if (!mm) { |
338 | name = "[vdso]"; | |
339 | goto done; | |
340 | } | |
341 | ||
342 | if (vma->vm_start <= mm->brk && | |
343 | vma->vm_end >= mm->start_brk) { | |
344 | name = "[heap]"; | |
345 | goto done; | |
346 | } | |
347 | ||
1240ea0d | 348 | if (is_stack(vma)) |
65376df5 | 349 | name = "[stack]"; |
b7643757 SP |
350 | } |
351 | ||
352 | done: | |
353 | if (name) { | |
652586df | 354 | seq_pad(m, ' '); |
b7643757 | 355 | seq_puts(m, name); |
1da177e4 LT |
356 | } |
357 | seq_putc(m, '\n'); | |
7c88db0c JK |
358 | } |
359 | ||
b7643757 | 360 | static int show_map(struct seq_file *m, void *v, int is_pid) |
7c88db0c | 361 | { |
ebb6cdde | 362 | show_map_vma(m, v, is_pid); |
b8c20a9b | 363 | m_cache_vma(m, v); |
1da177e4 LT |
364 | return 0; |
365 | } | |
366 | ||
b7643757 SP |
367 | static int show_pid_map(struct seq_file *m, void *v) |
368 | { | |
369 | return show_map(m, v, 1); | |
370 | } | |
371 | ||
372 | static int show_tid_map(struct seq_file *m, void *v) | |
373 | { | |
374 | return show_map(m, v, 0); | |
375 | } | |
376 | ||
03a44825 | 377 | static const struct seq_operations proc_pid_maps_op = { |
a6198797 MM |
378 | .start = m_start, |
379 | .next = m_next, | |
380 | .stop = m_stop, | |
b7643757 SP |
381 | .show = show_pid_map |
382 | }; | |
383 | ||
384 | static const struct seq_operations proc_tid_maps_op = { | |
385 | .start = m_start, | |
386 | .next = m_next, | |
387 | .stop = m_stop, | |
388 | .show = show_tid_map | |
a6198797 MM |
389 | }; |
390 | ||
b7643757 | 391 | static int pid_maps_open(struct inode *inode, struct file *file) |
a6198797 MM |
392 | { |
393 | return do_maps_open(inode, file, &proc_pid_maps_op); | |
394 | } | |
395 | ||
b7643757 SP |
396 | static int tid_maps_open(struct inode *inode, struct file *file) |
397 | { | |
398 | return do_maps_open(inode, file, &proc_tid_maps_op); | |
399 | } | |
400 | ||
401 | const struct file_operations proc_pid_maps_operations = { | |
402 | .open = pid_maps_open, | |
403 | .read = seq_read, | |
404 | .llseek = seq_lseek, | |
29a40ace | 405 | .release = proc_map_release, |
b7643757 SP |
406 | }; |
407 | ||
408 | const struct file_operations proc_tid_maps_operations = { | |
409 | .open = tid_maps_open, | |
a6198797 MM |
410 | .read = seq_read, |
411 | .llseek = seq_lseek, | |
29a40ace | 412 | .release = proc_map_release, |
a6198797 MM |
413 | }; |
414 | ||
415 | /* | |
416 | * Proportional Set Size(PSS): my share of RSS. | |
417 | * | |
418 | * PSS of a process is the count of pages it has in memory, where each | |
419 | * page is divided by the number of processes sharing it. So if a | |
420 | * process has 1000 pages all to itself, and 1000 shared with one other | |
421 | * process, its PSS will be 1500. | |
422 | * | |
423 | * To keep (accumulated) division errors low, we adopt a 64bit | |
424 | * fixed-point pss counter to minimize division errors. So (pss >> | |
425 | * PSS_SHIFT) would be the real byte count. | |
426 | * | |
427 | * A shift of 12 before division means (assuming 4K page size): | |
428 | * - 1M 3-user-pages add up to 8KB errors; | |
429 | * - supports mapcount up to 2^24, or 16M; | |
430 | * - supports PSS up to 2^52 bytes, or 4PB. | |
431 | */ | |
432 | #define PSS_SHIFT 12 | |
433 | ||
1e883281 | 434 | #ifdef CONFIG_PROC_PAGE_MONITOR |
214e471f | 435 | struct mem_size_stats { |
493b0e9d | 436 | bool first; |
a6198797 MM |
437 | unsigned long resident; |
438 | unsigned long shared_clean; | |
439 | unsigned long shared_dirty; | |
440 | unsigned long private_clean; | |
441 | unsigned long private_dirty; | |
442 | unsigned long referenced; | |
b40d4f84 | 443 | unsigned long anonymous; |
cf8496ea | 444 | unsigned long lazyfree; |
4031a219 | 445 | unsigned long anonymous_thp; |
65c45377 | 446 | unsigned long shmem_thp; |
214e471f | 447 | unsigned long swap; |
25ee01a2 NH |
448 | unsigned long shared_hugetlb; |
449 | unsigned long private_hugetlb; | |
493b0e9d | 450 | unsigned long first_vma_start; |
a6198797 | 451 | u64 pss; |
493b0e9d | 452 | u64 pss_locked; |
8334b962 | 453 | u64 swap_pss; |
c261e7d9 | 454 | bool check_shmem_swap; |
a6198797 MM |
455 | }; |
456 | ||
c164e038 | 457 | static void smaps_account(struct mem_size_stats *mss, struct page *page, |
afd9883f | 458 | bool compound, bool young, bool dirty) |
c164e038 | 459 | { |
f4be6153 | 460 | int i, nr = compound ? 1 << compound_order(page) : 1; |
afd9883f | 461 | unsigned long size = nr * PAGE_SIZE; |
c164e038 | 462 | |
cf8496ea | 463 | if (PageAnon(page)) { |
c164e038 | 464 | mss->anonymous += size; |
cf8496ea SL |
465 | if (!PageSwapBacked(page) && !dirty && !PageDirty(page)) |
466 | mss->lazyfree += size; | |
467 | } | |
c164e038 KS |
468 | |
469 | mss->resident += size; | |
470 | /* Accumulate the size in pages that have been accessed. */ | |
33c3fc71 | 471 | if (young || page_is_young(page) || PageReferenced(page)) |
c164e038 | 472 | mss->referenced += size; |
c164e038 | 473 | |
afd9883f KS |
474 | /* |
475 | * page_count(page) == 1 guarantees the page is mapped exactly once. | |
476 | * If any subpage of the compound page mapped with PTE it would elevate | |
477 | * page_count(). | |
478 | */ | |
479 | if (page_count(page) == 1) { | |
c164e038 KS |
480 | if (dirty || PageDirty(page)) |
481 | mss->private_dirty += size; | |
482 | else | |
483 | mss->private_clean += size; | |
484 | mss->pss += (u64)size << PSS_SHIFT; | |
afd9883f KS |
485 | return; |
486 | } | |
487 | ||
488 | for (i = 0; i < nr; i++, page++) { | |
489 | int mapcount = page_mapcount(page); | |
490 | ||
491 | if (mapcount >= 2) { | |
492 | if (dirty || PageDirty(page)) | |
493 | mss->shared_dirty += PAGE_SIZE; | |
494 | else | |
495 | mss->shared_clean += PAGE_SIZE; | |
496 | mss->pss += (PAGE_SIZE << PSS_SHIFT) / mapcount; | |
497 | } else { | |
498 | if (dirty || PageDirty(page)) | |
499 | mss->private_dirty += PAGE_SIZE; | |
500 | else | |
501 | mss->private_clean += PAGE_SIZE; | |
502 | mss->pss += PAGE_SIZE << PSS_SHIFT; | |
503 | } | |
c164e038 KS |
504 | } |
505 | } | |
ae11c4d9 | 506 | |
c261e7d9 | 507 | #ifdef CONFIG_SHMEM |
c261e7d9 VB |
508 | static int smaps_pte_hole(unsigned long addr, unsigned long end, |
509 | struct mm_walk *walk) | |
510 | { | |
511 | struct mem_size_stats *mss = walk->private; | |
512 | ||
48131e03 VB |
513 | mss->swap += shmem_partial_swap_usage( |
514 | walk->vma->vm_file->f_mapping, addr, end); | |
c261e7d9 VB |
515 | |
516 | return 0; | |
517 | } | |
c261e7d9 VB |
518 | #endif |
519 | ||
c164e038 KS |
520 | static void smaps_pte_entry(pte_t *pte, unsigned long addr, |
521 | struct mm_walk *walk) | |
ae11c4d9 DH |
522 | { |
523 | struct mem_size_stats *mss = walk->private; | |
14eb6fdd | 524 | struct vm_area_struct *vma = walk->vma; |
b1d4d9e0 | 525 | struct page *page = NULL; |
ae11c4d9 | 526 | |
c164e038 KS |
527 | if (pte_present(*pte)) { |
528 | page = vm_normal_page(vma, addr, *pte); | |
529 | } else if (is_swap_pte(*pte)) { | |
530 | swp_entry_t swpent = pte_to_swp_entry(*pte); | |
ae11c4d9 | 531 | |
8334b962 MK |
532 | if (!non_swap_entry(swpent)) { |
533 | int mapcount; | |
534 | ||
c164e038 | 535 | mss->swap += PAGE_SIZE; |
8334b962 MK |
536 | mapcount = swp_swapcount(swpent); |
537 | if (mapcount >= 2) { | |
538 | u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT; | |
539 | ||
540 | do_div(pss_delta, mapcount); | |
541 | mss->swap_pss += pss_delta; | |
542 | } else { | |
543 | mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT; | |
544 | } | |
545 | } else if (is_migration_entry(swpent)) | |
b1d4d9e0 | 546 | page = migration_entry_to_page(swpent); |
5042db43 JG |
547 | else if (is_device_private_entry(swpent)) |
548 | page = device_private_entry_to_page(swpent); | |
c261e7d9 VB |
549 | } else if (unlikely(IS_ENABLED(CONFIG_SHMEM) && mss->check_shmem_swap |
550 | && pte_none(*pte))) { | |
48131e03 VB |
551 | page = find_get_entry(vma->vm_file->f_mapping, |
552 | linear_page_index(vma, addr)); | |
553 | if (!page) | |
554 | return; | |
555 | ||
556 | if (radix_tree_exceptional_entry(page)) | |
557 | mss->swap += PAGE_SIZE; | |
558 | else | |
09cbfeaf | 559 | put_page(page); |
48131e03 VB |
560 | |
561 | return; | |
b1d4d9e0 | 562 | } |
ae11c4d9 | 563 | |
ae11c4d9 DH |
564 | if (!page) |
565 | return; | |
afd9883f KS |
566 | |
567 | smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte)); | |
ae11c4d9 DH |
568 | } |
569 | ||
c164e038 KS |
570 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
571 | static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr, | |
572 | struct mm_walk *walk) | |
573 | { | |
574 | struct mem_size_stats *mss = walk->private; | |
14eb6fdd | 575 | struct vm_area_struct *vma = walk->vma; |
c164e038 KS |
576 | struct page *page; |
577 | ||
578 | /* FOLL_DUMP will return -EFAULT on huge zero page */ | |
579 | page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP); | |
580 | if (IS_ERR_OR_NULL(page)) | |
581 | return; | |
65c45377 KS |
582 | if (PageAnon(page)) |
583 | mss->anonymous_thp += HPAGE_PMD_SIZE; | |
584 | else if (PageSwapBacked(page)) | |
585 | mss->shmem_thp += HPAGE_PMD_SIZE; | |
ca120cf6 DW |
586 | else if (is_zone_device_page(page)) |
587 | /* pass */; | |
65c45377 KS |
588 | else |
589 | VM_BUG_ON_PAGE(1, page); | |
afd9883f | 590 | smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd)); |
c164e038 KS |
591 | } |
592 | #else | |
593 | static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr, | |
594 | struct mm_walk *walk) | |
595 | { | |
596 | } | |
597 | #endif | |
598 | ||
b3ae5acb | 599 | static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, |
2165009b | 600 | struct mm_walk *walk) |
e070ad49 | 601 | { |
14eb6fdd | 602 | struct vm_area_struct *vma = walk->vma; |
ae11c4d9 | 603 | pte_t *pte; |
705e87c0 | 604 | spinlock_t *ptl; |
e070ad49 | 605 | |
b6ec57f4 KS |
606 | ptl = pmd_trans_huge_lock(pmd, vma); |
607 | if (ptl) { | |
84c3fc4e ZY |
608 | if (pmd_present(*pmd)) |
609 | smaps_pmd_entry(pmd, addr, walk); | |
bf929152 | 610 | spin_unlock(ptl); |
14038302 | 611 | goto out; |
22e057c5 | 612 | } |
1a5a9906 AA |
613 | |
614 | if (pmd_trans_unstable(pmd)) | |
14038302 | 615 | goto out; |
22e057c5 DH |
616 | /* |
617 | * The mmap_sem held all the way back in m_start() is what | |
618 | * keeps khugepaged out of here and from collapsing things | |
619 | * in here. | |
620 | */ | |
705e87c0 | 621 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
ae11c4d9 | 622 | for (; addr != end; pte++, addr += PAGE_SIZE) |
c164e038 | 623 | smaps_pte_entry(pte, addr, walk); |
705e87c0 | 624 | pte_unmap_unlock(pte - 1, ptl); |
14038302 | 625 | out: |
705e87c0 | 626 | cond_resched(); |
b3ae5acb | 627 | return 0; |
e070ad49 ML |
628 | } |
629 | ||
834f82e2 CG |
630 | static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma) |
631 | { | |
632 | /* | |
633 | * Don't forget to update Documentation/ on changes. | |
634 | */ | |
635 | static const char mnemonics[BITS_PER_LONG][2] = { | |
636 | /* | |
637 | * In case if we meet a flag we don't know about. | |
638 | */ | |
639 | [0 ... (BITS_PER_LONG-1)] = "??", | |
640 | ||
641 | [ilog2(VM_READ)] = "rd", | |
642 | [ilog2(VM_WRITE)] = "wr", | |
643 | [ilog2(VM_EXEC)] = "ex", | |
644 | [ilog2(VM_SHARED)] = "sh", | |
645 | [ilog2(VM_MAYREAD)] = "mr", | |
646 | [ilog2(VM_MAYWRITE)] = "mw", | |
647 | [ilog2(VM_MAYEXEC)] = "me", | |
648 | [ilog2(VM_MAYSHARE)] = "ms", | |
649 | [ilog2(VM_GROWSDOWN)] = "gd", | |
650 | [ilog2(VM_PFNMAP)] = "pf", | |
651 | [ilog2(VM_DENYWRITE)] = "dw", | |
4aae7e43 QR |
652 | #ifdef CONFIG_X86_INTEL_MPX |
653 | [ilog2(VM_MPX)] = "mp", | |
654 | #endif | |
834f82e2 CG |
655 | [ilog2(VM_LOCKED)] = "lo", |
656 | [ilog2(VM_IO)] = "io", | |
657 | [ilog2(VM_SEQ_READ)] = "sr", | |
658 | [ilog2(VM_RAND_READ)] = "rr", | |
659 | [ilog2(VM_DONTCOPY)] = "dc", | |
660 | [ilog2(VM_DONTEXPAND)] = "de", | |
661 | [ilog2(VM_ACCOUNT)] = "ac", | |
662 | [ilog2(VM_NORESERVE)] = "nr", | |
663 | [ilog2(VM_HUGETLB)] = "ht", | |
b6fb293f | 664 | [ilog2(VM_SYNC)] = "sf", |
834f82e2 | 665 | [ilog2(VM_ARCH_1)] = "ar", |
d2cd9ede | 666 | [ilog2(VM_WIPEONFORK)] = "wf", |
834f82e2 | 667 | [ilog2(VM_DONTDUMP)] = "dd", |
ec8e41ae NH |
668 | #ifdef CONFIG_MEM_SOFT_DIRTY |
669 | [ilog2(VM_SOFTDIRTY)] = "sd", | |
670 | #endif | |
834f82e2 CG |
671 | [ilog2(VM_MIXEDMAP)] = "mm", |
672 | [ilog2(VM_HUGEPAGE)] = "hg", | |
673 | [ilog2(VM_NOHUGEPAGE)] = "nh", | |
674 | [ilog2(VM_MERGEABLE)] = "mg", | |
16ba6f81 AA |
675 | [ilog2(VM_UFFD_MISSING)]= "um", |
676 | [ilog2(VM_UFFD_WP)] = "uw", | |
5212213a | 677 | #ifdef CONFIG_ARCH_HAS_PKEYS |
c1192f84 DH |
678 | /* These come out via ProtectionKey: */ |
679 | [ilog2(VM_PKEY_BIT0)] = "", | |
680 | [ilog2(VM_PKEY_BIT1)] = "", | |
681 | [ilog2(VM_PKEY_BIT2)] = "", | |
682 | [ilog2(VM_PKEY_BIT3)] = "", | |
2c9e0a6f RP |
683 | #if VM_PKEY_BIT4 |
684 | [ilog2(VM_PKEY_BIT4)] = "", | |
c1192f84 | 685 | #endif |
5212213a | 686 | #endif /* CONFIG_ARCH_HAS_PKEYS */ |
834f82e2 CG |
687 | }; |
688 | size_t i; | |
689 | ||
690 | seq_puts(m, "VmFlags: "); | |
691 | for (i = 0; i < BITS_PER_LONG; i++) { | |
c1192f84 DH |
692 | if (!mnemonics[i][0]) |
693 | continue; | |
834f82e2 | 694 | if (vma->vm_flags & (1UL << i)) { |
f6640663 AV |
695 | seq_putc(m, mnemonics[i][0]); |
696 | seq_putc(m, mnemonics[i][1]); | |
697 | seq_putc(m, ' '); | |
834f82e2 CG |
698 | } |
699 | } | |
700 | seq_putc(m, '\n'); | |
701 | } | |
702 | ||
25ee01a2 NH |
703 | #ifdef CONFIG_HUGETLB_PAGE |
704 | static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask, | |
705 | unsigned long addr, unsigned long end, | |
706 | struct mm_walk *walk) | |
707 | { | |
708 | struct mem_size_stats *mss = walk->private; | |
709 | struct vm_area_struct *vma = walk->vma; | |
710 | struct page *page = NULL; | |
711 | ||
712 | if (pte_present(*pte)) { | |
713 | page = vm_normal_page(vma, addr, *pte); | |
714 | } else if (is_swap_pte(*pte)) { | |
715 | swp_entry_t swpent = pte_to_swp_entry(*pte); | |
716 | ||
717 | if (is_migration_entry(swpent)) | |
718 | page = migration_entry_to_page(swpent); | |
5042db43 JG |
719 | else if (is_device_private_entry(swpent)) |
720 | page = device_private_entry_to_page(swpent); | |
25ee01a2 NH |
721 | } |
722 | if (page) { | |
723 | int mapcount = page_mapcount(page); | |
724 | ||
725 | if (mapcount >= 2) | |
726 | mss->shared_hugetlb += huge_page_size(hstate_vma(vma)); | |
727 | else | |
728 | mss->private_hugetlb += huge_page_size(hstate_vma(vma)); | |
729 | } | |
730 | return 0; | |
731 | } | |
732 | #endif /* HUGETLB_PAGE */ | |
733 | ||
d1be35cb AV |
734 | #define SEQ_PUT_DEC(str, val) \ |
735 | seq_put_decimal_ull_width(m, str, (val) >> 10, 8) | |
b7643757 | 736 | static int show_smap(struct seq_file *m, void *v, int is_pid) |
e070ad49 | 737 | { |
493b0e9d | 738 | struct proc_maps_private *priv = m->private; |
e070ad49 | 739 | struct vm_area_struct *vma = v; |
493b0e9d DC |
740 | struct mem_size_stats mss_stack; |
741 | struct mem_size_stats *mss; | |
2165009b DH |
742 | struct mm_walk smaps_walk = { |
743 | .pmd_entry = smaps_pte_range, | |
25ee01a2 NH |
744 | #ifdef CONFIG_HUGETLB_PAGE |
745 | .hugetlb_entry = smaps_hugetlb_range, | |
746 | #endif | |
2165009b | 747 | .mm = vma->vm_mm, |
2165009b | 748 | }; |
493b0e9d DC |
749 | int ret = 0; |
750 | bool rollup_mode; | |
751 | bool last_vma; | |
752 | ||
753 | if (priv->rollup) { | |
754 | rollup_mode = true; | |
755 | mss = priv->rollup; | |
756 | if (mss->first) { | |
757 | mss->first_vma_start = vma->vm_start; | |
758 | mss->first = false; | |
759 | } | |
760 | last_vma = !m_next_vma(priv, vma); | |
761 | } else { | |
762 | rollup_mode = false; | |
763 | memset(&mss_stack, 0, sizeof(mss_stack)); | |
764 | mss = &mss_stack; | |
765 | } | |
e070ad49 | 766 | |
493b0e9d | 767 | smaps_walk.private = mss; |
c261e7d9 VB |
768 | |
769 | #ifdef CONFIG_SHMEM | |
770 | if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) { | |
6a15a370 VB |
771 | /* |
772 | * For shared or readonly shmem mappings we know that all | |
773 | * swapped out pages belong to the shmem object, and we can | |
774 | * obtain the swap value much more efficiently. For private | |
775 | * writable mappings, we might have COW pages that are | |
776 | * not affected by the parent swapped out pages of the shmem | |
777 | * object, so we have to distinguish them during the page walk. | |
778 | * Unless we know that the shmem object (or the part mapped by | |
779 | * our VMA) has no swapped out pages at all. | |
780 | */ | |
781 | unsigned long shmem_swapped = shmem_swap_usage(vma); | |
782 | ||
783 | if (!shmem_swapped || (vma->vm_flags & VM_SHARED) || | |
784 | !(vma->vm_flags & VM_WRITE)) { | |
493b0e9d | 785 | mss->swap = shmem_swapped; |
6a15a370 | 786 | } else { |
493b0e9d | 787 | mss->check_shmem_swap = true; |
6a15a370 VB |
788 | smaps_walk.pte_hole = smaps_pte_hole; |
789 | } | |
c261e7d9 VB |
790 | } |
791 | #endif | |
792 | ||
d82ef020 | 793 | /* mmap_sem is held in m_start */ |
14eb6fdd | 794 | walk_page_vma(vma, &smaps_walk); |
493b0e9d DC |
795 | if (vma->vm_flags & VM_LOCKED) |
796 | mss->pss_locked += mss->pss; | |
797 | ||
798 | if (!rollup_mode) { | |
799 | show_map_vma(m, vma, is_pid); | |
800 | } else if (last_vma) { | |
801 | show_vma_header_prefix( | |
802 | m, mss->first_vma_start, vma->vm_end, 0, 0, 0, 0); | |
803 | seq_pad(m, ' '); | |
804 | seq_puts(m, "[rollup]\n"); | |
805 | } else { | |
806 | ret = SEQ_SKIP; | |
807 | } | |
4752c369 | 808 | |
d1be35cb AV |
809 | if (!rollup_mode) { |
810 | SEQ_PUT_DEC("Size: ", vma->vm_end - vma->vm_start); | |
811 | SEQ_PUT_DEC(" kB\nKernelPageSize: ", vma_kernel_pagesize(vma)); | |
812 | SEQ_PUT_DEC(" kB\nMMUPageSize: ", vma_mmu_pagesize(vma)); | |
813 | seq_puts(m, " kB\n"); | |
814 | } | |
493b0e9d | 815 | |
d1be35cb AV |
816 | if (!rollup_mode || last_vma) { |
817 | SEQ_PUT_DEC("Rss: ", mss->resident); | |
818 | SEQ_PUT_DEC(" kB\nPss: ", mss->pss >> PSS_SHIFT); | |
819 | SEQ_PUT_DEC(" kB\nShared_Clean: ", mss->shared_clean); | |
820 | SEQ_PUT_DEC(" kB\nShared_Dirty: ", mss->shared_dirty); | |
821 | SEQ_PUT_DEC(" kB\nPrivate_Clean: ", mss->private_clean); | |
822 | SEQ_PUT_DEC(" kB\nPrivate_Dirty: ", mss->private_dirty); | |
823 | SEQ_PUT_DEC(" kB\nReferenced: ", mss->referenced); | |
824 | SEQ_PUT_DEC(" kB\nAnonymous: ", mss->anonymous); | |
825 | SEQ_PUT_DEC(" kB\nLazyFree: ", mss->lazyfree); | |
826 | SEQ_PUT_DEC(" kB\nAnonHugePages: ", mss->anonymous_thp); | |
827 | SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp); | |
828 | SEQ_PUT_DEC(" kB\nShared_Hugetlb: ", mss->shared_hugetlb); | |
829 | seq_put_decimal_ull_width(m, " kB\nPrivate_Hugetlb: ", | |
830 | mss->private_hugetlb >> 10, 7); | |
831 | SEQ_PUT_DEC(" kB\nSwap: ", mss->swap); | |
832 | SEQ_PUT_DEC(" kB\nSwapPss: ", | |
833 | mss->swap_pss >> PSS_SHIFT); | |
834 | SEQ_PUT_DEC(" kB\nLocked: ", mss->pss >> PSS_SHIFT); | |
835 | seq_puts(m, " kB\n"); | |
836 | } | |
493b0e9d | 837 | if (!rollup_mode) { |
27cca866 RP |
838 | if (arch_pkeys_enabled()) |
839 | seq_printf(m, "ProtectionKey: %8u\n", vma_pkey(vma)); | |
493b0e9d DC |
840 | show_smap_vma_flags(m, vma); |
841 | } | |
b8c20a9b | 842 | m_cache_vma(m, vma); |
493b0e9d | 843 | return ret; |
e070ad49 | 844 | } |
d1be35cb | 845 | #undef SEQ_PUT_DEC |
e070ad49 | 846 | |
b7643757 SP |
847 | static int show_pid_smap(struct seq_file *m, void *v) |
848 | { | |
849 | return show_smap(m, v, 1); | |
850 | } | |
851 | ||
852 | static int show_tid_smap(struct seq_file *m, void *v) | |
853 | { | |
854 | return show_smap(m, v, 0); | |
855 | } | |
856 | ||
03a44825 | 857 | static const struct seq_operations proc_pid_smaps_op = { |
a6198797 MM |
858 | .start = m_start, |
859 | .next = m_next, | |
860 | .stop = m_stop, | |
b7643757 SP |
861 | .show = show_pid_smap |
862 | }; | |
863 | ||
864 | static const struct seq_operations proc_tid_smaps_op = { | |
865 | .start = m_start, | |
866 | .next = m_next, | |
867 | .stop = m_stop, | |
868 | .show = show_tid_smap | |
a6198797 MM |
869 | }; |
870 | ||
b7643757 | 871 | static int pid_smaps_open(struct inode *inode, struct file *file) |
a6198797 MM |
872 | { |
873 | return do_maps_open(inode, file, &proc_pid_smaps_op); | |
874 | } | |
875 | ||
493b0e9d DC |
876 | static int pid_smaps_rollup_open(struct inode *inode, struct file *file) |
877 | { | |
878 | struct seq_file *seq; | |
879 | struct proc_maps_private *priv; | |
880 | int ret = do_maps_open(inode, file, &proc_pid_smaps_op); | |
881 | ||
882 | if (ret < 0) | |
883 | return ret; | |
884 | seq = file->private_data; | |
885 | priv = seq->private; | |
886 | priv->rollup = kzalloc(sizeof(*priv->rollup), GFP_KERNEL); | |
887 | if (!priv->rollup) { | |
888 | proc_map_release(inode, file); | |
889 | return -ENOMEM; | |
890 | } | |
891 | priv->rollup->first = true; | |
892 | return 0; | |
893 | } | |
894 | ||
b7643757 SP |
895 | static int tid_smaps_open(struct inode *inode, struct file *file) |
896 | { | |
897 | return do_maps_open(inode, file, &proc_tid_smaps_op); | |
898 | } | |
899 | ||
900 | const struct file_operations proc_pid_smaps_operations = { | |
901 | .open = pid_smaps_open, | |
902 | .read = seq_read, | |
903 | .llseek = seq_lseek, | |
29a40ace | 904 | .release = proc_map_release, |
b7643757 SP |
905 | }; |
906 | ||
493b0e9d DC |
907 | const struct file_operations proc_pid_smaps_rollup_operations = { |
908 | .open = pid_smaps_rollup_open, | |
909 | .read = seq_read, | |
910 | .llseek = seq_lseek, | |
911 | .release = proc_map_release, | |
912 | }; | |
913 | ||
b7643757 SP |
914 | const struct file_operations proc_tid_smaps_operations = { |
915 | .open = tid_smaps_open, | |
a6198797 MM |
916 | .read = seq_read, |
917 | .llseek = seq_lseek, | |
29a40ace | 918 | .release = proc_map_release, |
a6198797 MM |
919 | }; |
920 | ||
040fa020 PE |
921 | enum clear_refs_types { |
922 | CLEAR_REFS_ALL = 1, | |
923 | CLEAR_REFS_ANON, | |
924 | CLEAR_REFS_MAPPED, | |
0f8975ec | 925 | CLEAR_REFS_SOFT_DIRTY, |
695f0559 | 926 | CLEAR_REFS_MM_HIWATER_RSS, |
040fa020 PE |
927 | CLEAR_REFS_LAST, |
928 | }; | |
929 | ||
af9de7eb | 930 | struct clear_refs_private { |
0f8975ec | 931 | enum clear_refs_types type; |
af9de7eb PE |
932 | }; |
933 | ||
7d5b3bfa | 934 | #ifdef CONFIG_MEM_SOFT_DIRTY |
0f8975ec PE |
935 | static inline void clear_soft_dirty(struct vm_area_struct *vma, |
936 | unsigned long addr, pte_t *pte) | |
937 | { | |
0f8975ec PE |
938 | /* |
939 | * The soft-dirty tracker uses #PF-s to catch writes | |
940 | * to pages, so write-protect the pte as well. See the | |
1ad1335d | 941 | * Documentation/admin-guide/mm/soft-dirty.rst for full description |
0f8975ec PE |
942 | * of how soft-dirty works. |
943 | */ | |
944 | pte_t ptent = *pte; | |
179ef71c CG |
945 | |
946 | if (pte_present(ptent)) { | |
326c2597 | 947 | ptent = ptep_modify_prot_start(vma->vm_mm, addr, pte); |
179ef71c | 948 | ptent = pte_wrprotect(ptent); |
a7b76174 | 949 | ptent = pte_clear_soft_dirty(ptent); |
326c2597 | 950 | ptep_modify_prot_commit(vma->vm_mm, addr, pte, ptent); |
179ef71c CG |
951 | } else if (is_swap_pte(ptent)) { |
952 | ptent = pte_swp_clear_soft_dirty(ptent); | |
326c2597 | 953 | set_pte_at(vma->vm_mm, addr, pte, ptent); |
179ef71c | 954 | } |
0f8975ec | 955 | } |
5d3875a0 LD |
956 | #else |
957 | static inline void clear_soft_dirty(struct vm_area_struct *vma, | |
958 | unsigned long addr, pte_t *pte) | |
959 | { | |
960 | } | |
961 | #endif | |
0f8975ec | 962 | |
5d3875a0 | 963 | #if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE) |
7d5b3bfa KS |
964 | static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma, |
965 | unsigned long addr, pmd_t *pmdp) | |
966 | { | |
a3cf988f | 967 | pmd_t old, pmd = *pmdp; |
5b7abeae | 968 | |
ab6e3d09 NH |
969 | if (pmd_present(pmd)) { |
970 | /* See comment in change_huge_pmd() */ | |
a3cf988f KS |
971 | old = pmdp_invalidate(vma, addr, pmdp); |
972 | if (pmd_dirty(old)) | |
ab6e3d09 | 973 | pmd = pmd_mkdirty(pmd); |
a3cf988f | 974 | if (pmd_young(old)) |
ab6e3d09 NH |
975 | pmd = pmd_mkyoung(pmd); |
976 | ||
977 | pmd = pmd_wrprotect(pmd); | |
978 | pmd = pmd_clear_soft_dirty(pmd); | |
979 | ||
980 | set_pmd_at(vma->vm_mm, addr, pmdp, pmd); | |
981 | } else if (is_migration_entry(pmd_to_swp_entry(pmd))) { | |
982 | pmd = pmd_swp_clear_soft_dirty(pmd); | |
983 | set_pmd_at(vma->vm_mm, addr, pmdp, pmd); | |
984 | } | |
7d5b3bfa | 985 | } |
7d5b3bfa | 986 | #else |
7d5b3bfa KS |
987 | static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma, |
988 | unsigned long addr, pmd_t *pmdp) | |
989 | { | |
990 | } | |
991 | #endif | |
992 | ||
a6198797 | 993 | static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr, |
2165009b | 994 | unsigned long end, struct mm_walk *walk) |
a6198797 | 995 | { |
af9de7eb | 996 | struct clear_refs_private *cp = walk->private; |
5c64f52a | 997 | struct vm_area_struct *vma = walk->vma; |
a6198797 MM |
998 | pte_t *pte, ptent; |
999 | spinlock_t *ptl; | |
1000 | struct page *page; | |
1001 | ||
b6ec57f4 KS |
1002 | ptl = pmd_trans_huge_lock(pmd, vma); |
1003 | if (ptl) { | |
7d5b3bfa KS |
1004 | if (cp->type == CLEAR_REFS_SOFT_DIRTY) { |
1005 | clear_soft_dirty_pmd(vma, addr, pmd); | |
1006 | goto out; | |
1007 | } | |
1008 | ||
84c3fc4e ZY |
1009 | if (!pmd_present(*pmd)) |
1010 | goto out; | |
1011 | ||
7d5b3bfa KS |
1012 | page = pmd_page(*pmd); |
1013 | ||
1014 | /* Clear accessed and referenced bits. */ | |
1015 | pmdp_test_and_clear_young(vma, addr, pmd); | |
33c3fc71 | 1016 | test_and_clear_page_young(page); |
7d5b3bfa KS |
1017 | ClearPageReferenced(page); |
1018 | out: | |
1019 | spin_unlock(ptl); | |
1020 | return 0; | |
1021 | } | |
1022 | ||
1a5a9906 AA |
1023 | if (pmd_trans_unstable(pmd)) |
1024 | return 0; | |
03319327 | 1025 | |
a6198797 MM |
1026 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
1027 | for (; addr != end; pte++, addr += PAGE_SIZE) { | |
1028 | ptent = *pte; | |
a6198797 | 1029 | |
0f8975ec PE |
1030 | if (cp->type == CLEAR_REFS_SOFT_DIRTY) { |
1031 | clear_soft_dirty(vma, addr, pte); | |
1032 | continue; | |
1033 | } | |
1034 | ||
179ef71c CG |
1035 | if (!pte_present(ptent)) |
1036 | continue; | |
1037 | ||
a6198797 MM |
1038 | page = vm_normal_page(vma, addr, ptent); |
1039 | if (!page) | |
1040 | continue; | |
1041 | ||
1042 | /* Clear accessed and referenced bits. */ | |
1043 | ptep_test_and_clear_young(vma, addr, pte); | |
33c3fc71 | 1044 | test_and_clear_page_young(page); |
a6198797 MM |
1045 | ClearPageReferenced(page); |
1046 | } | |
1047 | pte_unmap_unlock(pte - 1, ptl); | |
1048 | cond_resched(); | |
1049 | return 0; | |
1050 | } | |
1051 | ||
5c64f52a NH |
1052 | static int clear_refs_test_walk(unsigned long start, unsigned long end, |
1053 | struct mm_walk *walk) | |
1054 | { | |
1055 | struct clear_refs_private *cp = walk->private; | |
1056 | struct vm_area_struct *vma = walk->vma; | |
1057 | ||
48684a65 NH |
1058 | if (vma->vm_flags & VM_PFNMAP) |
1059 | return 1; | |
1060 | ||
5c64f52a NH |
1061 | /* |
1062 | * Writing 1 to /proc/pid/clear_refs affects all pages. | |
1063 | * Writing 2 to /proc/pid/clear_refs only affects anonymous pages. | |
1064 | * Writing 3 to /proc/pid/clear_refs only affects file mapped pages. | |
1065 | * Writing 4 to /proc/pid/clear_refs affects all pages. | |
1066 | */ | |
1067 | if (cp->type == CLEAR_REFS_ANON && vma->vm_file) | |
1068 | return 1; | |
1069 | if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file) | |
1070 | return 1; | |
1071 | return 0; | |
1072 | } | |
1073 | ||
f248dcb3 MM |
1074 | static ssize_t clear_refs_write(struct file *file, const char __user *buf, |
1075 | size_t count, loff_t *ppos) | |
b813e931 | 1076 | { |
f248dcb3 | 1077 | struct task_struct *task; |
fb92a4b0 | 1078 | char buffer[PROC_NUMBUF]; |
f248dcb3 | 1079 | struct mm_struct *mm; |
b813e931 | 1080 | struct vm_area_struct *vma; |
040fa020 | 1081 | enum clear_refs_types type; |
b3a81d08 | 1082 | struct mmu_gather tlb; |
040fa020 | 1083 | int itype; |
0a8cb8e3 | 1084 | int rv; |
b813e931 | 1085 | |
f248dcb3 MM |
1086 | memset(buffer, 0, sizeof(buffer)); |
1087 | if (count > sizeof(buffer) - 1) | |
1088 | count = sizeof(buffer) - 1; | |
1089 | if (copy_from_user(buffer, buf, count)) | |
1090 | return -EFAULT; | |
040fa020 | 1091 | rv = kstrtoint(strstrip(buffer), 10, &itype); |
0a8cb8e3 AD |
1092 | if (rv < 0) |
1093 | return rv; | |
040fa020 PE |
1094 | type = (enum clear_refs_types)itype; |
1095 | if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST) | |
f248dcb3 | 1096 | return -EINVAL; |
541c237c | 1097 | |
496ad9aa | 1098 | task = get_proc_task(file_inode(file)); |
f248dcb3 MM |
1099 | if (!task) |
1100 | return -ESRCH; | |
1101 | mm = get_task_mm(task); | |
1102 | if (mm) { | |
af9de7eb | 1103 | struct clear_refs_private cp = { |
0f8975ec | 1104 | .type = type, |
af9de7eb | 1105 | }; |
20cbc972 AM |
1106 | struct mm_walk clear_refs_walk = { |
1107 | .pmd_entry = clear_refs_pte_range, | |
5c64f52a | 1108 | .test_walk = clear_refs_test_walk, |
20cbc972 | 1109 | .mm = mm, |
af9de7eb | 1110 | .private = &cp, |
20cbc972 | 1111 | }; |
695f0559 PC |
1112 | |
1113 | if (type == CLEAR_REFS_MM_HIWATER_RSS) { | |
4e80153a MH |
1114 | if (down_write_killable(&mm->mmap_sem)) { |
1115 | count = -EINTR; | |
1116 | goto out_mm; | |
1117 | } | |
1118 | ||
695f0559 PC |
1119 | /* |
1120 | * Writing 5 to /proc/pid/clear_refs resets the peak | |
1121 | * resident set size to this mm's current rss value. | |
1122 | */ | |
695f0559 PC |
1123 | reset_mm_hiwater_rss(mm); |
1124 | up_write(&mm->mmap_sem); | |
1125 | goto out_mm; | |
1126 | } | |
1127 | ||
f248dcb3 | 1128 | down_read(&mm->mmap_sem); |
b3a81d08 | 1129 | tlb_gather_mmu(&tlb, mm, 0, -1); |
64e45507 PF |
1130 | if (type == CLEAR_REFS_SOFT_DIRTY) { |
1131 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |
1132 | if (!(vma->vm_flags & VM_SOFTDIRTY)) | |
1133 | continue; | |
1134 | up_read(&mm->mmap_sem); | |
4e80153a MH |
1135 | if (down_write_killable(&mm->mmap_sem)) { |
1136 | count = -EINTR; | |
1137 | goto out_mm; | |
1138 | } | |
64e45507 PF |
1139 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
1140 | vma->vm_flags &= ~VM_SOFTDIRTY; | |
1141 | vma_set_page_prot(vma); | |
1142 | } | |
1143 | downgrade_write(&mm->mmap_sem); | |
1144 | break; | |
1145 | } | |
0f8975ec | 1146 | mmu_notifier_invalidate_range_start(mm, 0, -1); |
64e45507 | 1147 | } |
0f30206b | 1148 | walk_page_range(0, mm->highest_vm_end, &clear_refs_walk); |
0f8975ec PE |
1149 | if (type == CLEAR_REFS_SOFT_DIRTY) |
1150 | mmu_notifier_invalidate_range_end(mm, 0, -1); | |
b3a81d08 | 1151 | tlb_finish_mmu(&tlb, 0, -1); |
f248dcb3 | 1152 | up_read(&mm->mmap_sem); |
695f0559 | 1153 | out_mm: |
f248dcb3 MM |
1154 | mmput(mm); |
1155 | } | |
1156 | put_task_struct(task); | |
fb92a4b0 VL |
1157 | |
1158 | return count; | |
b813e931 DR |
1159 | } |
1160 | ||
f248dcb3 MM |
1161 | const struct file_operations proc_clear_refs_operations = { |
1162 | .write = clear_refs_write, | |
6038f373 | 1163 | .llseek = noop_llseek, |
f248dcb3 MM |
1164 | }; |
1165 | ||
092b50ba NH |
1166 | typedef struct { |
1167 | u64 pme; | |
1168 | } pagemap_entry_t; | |
1169 | ||
85863e47 | 1170 | struct pagemapread { |
8c829622 | 1171 | int pos, len; /* units: PM_ENTRY_BYTES, not bytes */ |
092b50ba | 1172 | pagemap_entry_t *buffer; |
1c90308e | 1173 | bool show_pfn; |
85863e47 MM |
1174 | }; |
1175 | ||
5aaabe83 NH |
1176 | #define PAGEMAP_WALK_SIZE (PMD_SIZE) |
1177 | #define PAGEMAP_WALK_MASK (PMD_MASK) | |
1178 | ||
deb94544 KK |
1179 | #define PM_ENTRY_BYTES sizeof(pagemap_entry_t) |
1180 | #define PM_PFRAME_BITS 55 | |
1181 | #define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0) | |
1182 | #define PM_SOFT_DIRTY BIT_ULL(55) | |
77bb499b | 1183 | #define PM_MMAP_EXCLUSIVE BIT_ULL(56) |
deb94544 KK |
1184 | #define PM_FILE BIT_ULL(61) |
1185 | #define PM_SWAP BIT_ULL(62) | |
1186 | #define PM_PRESENT BIT_ULL(63) | |
1187 | ||
85863e47 MM |
1188 | #define PM_END_OF_BUFFER 1 |
1189 | ||
deb94544 | 1190 | static inline pagemap_entry_t make_pme(u64 frame, u64 flags) |
092b50ba | 1191 | { |
deb94544 | 1192 | return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags }; |
092b50ba NH |
1193 | } |
1194 | ||
1195 | static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme, | |
85863e47 MM |
1196 | struct pagemapread *pm) |
1197 | { | |
092b50ba | 1198 | pm->buffer[pm->pos++] = *pme; |
d82ef020 | 1199 | if (pm->pos >= pm->len) |
aae8679b | 1200 | return PM_END_OF_BUFFER; |
85863e47 MM |
1201 | return 0; |
1202 | } | |
1203 | ||
1204 | static int pagemap_pte_hole(unsigned long start, unsigned long end, | |
2165009b | 1205 | struct mm_walk *walk) |
85863e47 | 1206 | { |
2165009b | 1207 | struct pagemapread *pm = walk->private; |
68b5a652 | 1208 | unsigned long addr = start; |
85863e47 | 1209 | int err = 0; |
092b50ba | 1210 | |
68b5a652 PF |
1211 | while (addr < end) { |
1212 | struct vm_area_struct *vma = find_vma(walk->mm, addr); | |
deb94544 | 1213 | pagemap_entry_t pme = make_pme(0, 0); |
87e6d49a PF |
1214 | /* End of address space hole, which we mark as non-present. */ |
1215 | unsigned long hole_end; | |
68b5a652 | 1216 | |
87e6d49a PF |
1217 | if (vma) |
1218 | hole_end = min(end, vma->vm_start); | |
1219 | else | |
1220 | hole_end = end; | |
1221 | ||
1222 | for (; addr < hole_end; addr += PAGE_SIZE) { | |
1223 | err = add_to_pagemap(addr, &pme, pm); | |
1224 | if (err) | |
1225 | goto out; | |
68b5a652 PF |
1226 | } |
1227 | ||
87e6d49a PF |
1228 | if (!vma) |
1229 | break; | |
1230 | ||
1231 | /* Addresses in the VMA. */ | |
1232 | if (vma->vm_flags & VM_SOFTDIRTY) | |
deb94544 | 1233 | pme = make_pme(0, PM_SOFT_DIRTY); |
87e6d49a | 1234 | for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) { |
68b5a652 PF |
1235 | err = add_to_pagemap(addr, &pme, pm); |
1236 | if (err) | |
1237 | goto out; | |
1238 | } | |
85863e47 | 1239 | } |
68b5a652 | 1240 | out: |
85863e47 MM |
1241 | return err; |
1242 | } | |
1243 | ||
deb94544 | 1244 | static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm, |
052fb0d6 | 1245 | struct vm_area_struct *vma, unsigned long addr, pte_t pte) |
85863e47 | 1246 | { |
deb94544 | 1247 | u64 frame = 0, flags = 0; |
052fb0d6 | 1248 | struct page *page = NULL; |
85863e47 | 1249 | |
052fb0d6 | 1250 | if (pte_present(pte)) { |
1c90308e KK |
1251 | if (pm->show_pfn) |
1252 | frame = pte_pfn(pte); | |
deb94544 | 1253 | flags |= PM_PRESENT; |
df6ad698 | 1254 | page = _vm_normal_page(vma, addr, pte, true); |
e9cdd6e7 | 1255 | if (pte_soft_dirty(pte)) |
deb94544 | 1256 | flags |= PM_SOFT_DIRTY; |
052fb0d6 | 1257 | } else if (is_swap_pte(pte)) { |
179ef71c CG |
1258 | swp_entry_t entry; |
1259 | if (pte_swp_soft_dirty(pte)) | |
deb94544 | 1260 | flags |= PM_SOFT_DIRTY; |
179ef71c | 1261 | entry = pte_to_swp_entry(pte); |
ab6ecf24 HY |
1262 | if (pm->show_pfn) |
1263 | frame = swp_type(entry) | | |
1264 | (swp_offset(entry) << MAX_SWAPFILES_SHIFT); | |
deb94544 | 1265 | flags |= PM_SWAP; |
052fb0d6 KK |
1266 | if (is_migration_entry(entry)) |
1267 | page = migration_entry_to_page(entry); | |
5042db43 JG |
1268 | |
1269 | if (is_device_private_entry(entry)) | |
1270 | page = device_private_entry_to_page(entry); | |
052fb0d6 KK |
1271 | } |
1272 | ||
1273 | if (page && !PageAnon(page)) | |
1274 | flags |= PM_FILE; | |
77bb499b KK |
1275 | if (page && page_mapcount(page) == 1) |
1276 | flags |= PM_MMAP_EXCLUSIVE; | |
deb94544 KK |
1277 | if (vma->vm_flags & VM_SOFTDIRTY) |
1278 | flags |= PM_SOFT_DIRTY; | |
052fb0d6 | 1279 | |
deb94544 | 1280 | return make_pme(frame, flags); |
bcf8039e DH |
1281 | } |
1282 | ||
356515e7 | 1283 | static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end, |
2165009b | 1284 | struct mm_walk *walk) |
85863e47 | 1285 | { |
f995ece2 | 1286 | struct vm_area_struct *vma = walk->vma; |
2165009b | 1287 | struct pagemapread *pm = walk->private; |
bf929152 | 1288 | spinlock_t *ptl; |
05fbf357 | 1289 | pte_t *pte, *orig_pte; |
85863e47 MM |
1290 | int err = 0; |
1291 | ||
356515e7 | 1292 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
b6ec57f4 KS |
1293 | ptl = pmd_trans_huge_lock(pmdp, vma); |
1294 | if (ptl) { | |
356515e7 KK |
1295 | u64 flags = 0, frame = 0; |
1296 | pmd_t pmd = *pmdp; | |
84c3fc4e | 1297 | struct page *page = NULL; |
0f8975ec | 1298 | |
b83d7e43 | 1299 | if (vma->vm_flags & VM_SOFTDIRTY) |
deb94544 | 1300 | flags |= PM_SOFT_DIRTY; |
d9104d1c | 1301 | |
356515e7 | 1302 | if (pmd_present(pmd)) { |
84c3fc4e | 1303 | page = pmd_page(pmd); |
77bb499b | 1304 | |
356515e7 | 1305 | flags |= PM_PRESENT; |
b83d7e43 HY |
1306 | if (pmd_soft_dirty(pmd)) |
1307 | flags |= PM_SOFT_DIRTY; | |
1c90308e KK |
1308 | if (pm->show_pfn) |
1309 | frame = pmd_pfn(pmd) + | |
1310 | ((addr & ~PMD_MASK) >> PAGE_SHIFT); | |
356515e7 | 1311 | } |
84c3fc4e ZY |
1312 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION |
1313 | else if (is_swap_pmd(pmd)) { | |
1314 | swp_entry_t entry = pmd_to_swp_entry(pmd); | |
ab6ecf24 | 1315 | unsigned long offset; |
84c3fc4e | 1316 | |
ab6ecf24 HY |
1317 | if (pm->show_pfn) { |
1318 | offset = swp_offset(entry) + | |
1319 | ((addr & ~PMD_MASK) >> PAGE_SHIFT); | |
1320 | frame = swp_type(entry) | | |
1321 | (offset << MAX_SWAPFILES_SHIFT); | |
1322 | } | |
84c3fc4e | 1323 | flags |= PM_SWAP; |
b83d7e43 HY |
1324 | if (pmd_swp_soft_dirty(pmd)) |
1325 | flags |= PM_SOFT_DIRTY; | |
84c3fc4e ZY |
1326 | VM_BUG_ON(!is_pmd_migration_entry(pmd)); |
1327 | page = migration_entry_to_page(entry); | |
1328 | } | |
1329 | #endif | |
1330 | ||
1331 | if (page && page_mapcount(page) == 1) | |
1332 | flags |= PM_MMAP_EXCLUSIVE; | |
356515e7 | 1333 | |
025c5b24 | 1334 | for (; addr != end; addr += PAGE_SIZE) { |
356515e7 | 1335 | pagemap_entry_t pme = make_pme(frame, flags); |
025c5b24 | 1336 | |
092b50ba | 1337 | err = add_to_pagemap(addr, &pme, pm); |
025c5b24 NH |
1338 | if (err) |
1339 | break; | |
ab6ecf24 HY |
1340 | if (pm->show_pfn) { |
1341 | if (flags & PM_PRESENT) | |
1342 | frame++; | |
1343 | else if (flags & PM_SWAP) | |
1344 | frame += (1 << MAX_SWAPFILES_SHIFT); | |
1345 | } | |
5aaabe83 | 1346 | } |
bf929152 | 1347 | spin_unlock(ptl); |
025c5b24 | 1348 | return err; |
5aaabe83 NH |
1349 | } |
1350 | ||
356515e7 | 1351 | if (pmd_trans_unstable(pmdp)) |
45f83cef | 1352 | return 0; |
356515e7 | 1353 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
81d0fa62 | 1354 | |
f995ece2 NH |
1355 | /* |
1356 | * We can assume that @vma always points to a valid one and @end never | |
1357 | * goes beyond vma->vm_end. | |
1358 | */ | |
356515e7 | 1359 | orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl); |
f995ece2 NH |
1360 | for (; addr < end; pte++, addr += PAGE_SIZE) { |
1361 | pagemap_entry_t pme; | |
05fbf357 | 1362 | |
deb94544 | 1363 | pme = pte_to_pagemap_entry(pm, vma, addr, *pte); |
f995ece2 | 1364 | err = add_to_pagemap(addr, &pme, pm); |
05fbf357 | 1365 | if (err) |
81d0fa62 | 1366 | break; |
85863e47 | 1367 | } |
f995ece2 | 1368 | pte_unmap_unlock(orig_pte, ptl); |
85863e47 MM |
1369 | |
1370 | cond_resched(); | |
1371 | ||
1372 | return err; | |
1373 | } | |
1374 | ||
1a5cb814 | 1375 | #ifdef CONFIG_HUGETLB_PAGE |
116354d1 | 1376 | /* This function walks within one hugetlb entry in the single call */ |
356515e7 | 1377 | static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask, |
116354d1 NH |
1378 | unsigned long addr, unsigned long end, |
1379 | struct mm_walk *walk) | |
5dc37642 | 1380 | { |
5dc37642 | 1381 | struct pagemapread *pm = walk->private; |
f995ece2 | 1382 | struct vm_area_struct *vma = walk->vma; |
356515e7 | 1383 | u64 flags = 0, frame = 0; |
5dc37642 | 1384 | int err = 0; |
356515e7 | 1385 | pte_t pte; |
5dc37642 | 1386 | |
f995ece2 | 1387 | if (vma->vm_flags & VM_SOFTDIRTY) |
deb94544 | 1388 | flags |= PM_SOFT_DIRTY; |
d9104d1c | 1389 | |
356515e7 KK |
1390 | pte = huge_ptep_get(ptep); |
1391 | if (pte_present(pte)) { | |
1392 | struct page *page = pte_page(pte); | |
1393 | ||
1394 | if (!PageAnon(page)) | |
1395 | flags |= PM_FILE; | |
1396 | ||
77bb499b KK |
1397 | if (page_mapcount(page) == 1) |
1398 | flags |= PM_MMAP_EXCLUSIVE; | |
1399 | ||
356515e7 | 1400 | flags |= PM_PRESENT; |
1c90308e KK |
1401 | if (pm->show_pfn) |
1402 | frame = pte_pfn(pte) + | |
1403 | ((addr & ~hmask) >> PAGE_SHIFT); | |
356515e7 KK |
1404 | } |
1405 | ||
5dc37642 | 1406 | for (; addr != end; addr += PAGE_SIZE) { |
356515e7 KK |
1407 | pagemap_entry_t pme = make_pme(frame, flags); |
1408 | ||
092b50ba | 1409 | err = add_to_pagemap(addr, &pme, pm); |
5dc37642 NH |
1410 | if (err) |
1411 | return err; | |
1c90308e | 1412 | if (pm->show_pfn && (flags & PM_PRESENT)) |
356515e7 | 1413 | frame++; |
5dc37642 NH |
1414 | } |
1415 | ||
1416 | cond_resched(); | |
1417 | ||
1418 | return err; | |
1419 | } | |
1a5cb814 | 1420 | #endif /* HUGETLB_PAGE */ |
5dc37642 | 1421 | |
85863e47 MM |
1422 | /* |
1423 | * /proc/pid/pagemap - an array mapping virtual pages to pfns | |
1424 | * | |
f16278c6 HR |
1425 | * For each page in the address space, this file contains one 64-bit entry |
1426 | * consisting of the following: | |
1427 | * | |
052fb0d6 | 1428 | * Bits 0-54 page frame number (PFN) if present |
f16278c6 | 1429 | * Bits 0-4 swap type if swapped |
052fb0d6 | 1430 | * Bits 5-54 swap offset if swapped |
1ad1335d | 1431 | * Bit 55 pte is soft-dirty (see Documentation/admin-guide/mm/soft-dirty.rst) |
77bb499b KK |
1432 | * Bit 56 page exclusively mapped |
1433 | * Bits 57-60 zero | |
052fb0d6 | 1434 | * Bit 61 page is file-page or shared-anon |
f16278c6 HR |
1435 | * Bit 62 page swapped |
1436 | * Bit 63 page present | |
1437 | * | |
1438 | * If the page is not present but in swap, then the PFN contains an | |
1439 | * encoding of the swap file number and the page's offset into the | |
1440 | * swap. Unmapped pages return a null PFN. This allows determining | |
85863e47 MM |
1441 | * precisely which pages are mapped (or in swap) and comparing mapped |
1442 | * pages between processes. | |
1443 | * | |
1444 | * Efficient users of this interface will use /proc/pid/maps to | |
1445 | * determine which areas of memory are actually mapped and llseek to | |
1446 | * skip over unmapped regions. | |
1447 | */ | |
1448 | static ssize_t pagemap_read(struct file *file, char __user *buf, | |
1449 | size_t count, loff_t *ppos) | |
1450 | { | |
a06db751 | 1451 | struct mm_struct *mm = file->private_data; |
85863e47 | 1452 | struct pagemapread pm; |
ee1e6ab6 | 1453 | struct mm_walk pagemap_walk = {}; |
5d7e0d2b AM |
1454 | unsigned long src; |
1455 | unsigned long svpfn; | |
1456 | unsigned long start_vaddr; | |
1457 | unsigned long end_vaddr; | |
a06db751 | 1458 | int ret = 0, copied = 0; |
85863e47 | 1459 | |
388f7934 | 1460 | if (!mm || !mmget_not_zero(mm)) |
85863e47 MM |
1461 | goto out; |
1462 | ||
85863e47 MM |
1463 | ret = -EINVAL; |
1464 | /* file position must be aligned */ | |
aae8679b | 1465 | if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES)) |
a06db751 | 1466 | goto out_mm; |
85863e47 MM |
1467 | |
1468 | ret = 0; | |
08161786 | 1469 | if (!count) |
a06db751 | 1470 | goto out_mm; |
08161786 | 1471 | |
1c90308e KK |
1472 | /* do not disclose physical addresses: attack vector */ |
1473 | pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN); | |
1474 | ||
8c829622 | 1475 | pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT); |
6da2ec56 | 1476 | pm.buffer = kmalloc_array(pm.len, PM_ENTRY_BYTES, GFP_KERNEL); |
5d7e0d2b | 1477 | ret = -ENOMEM; |
d82ef020 | 1478 | if (!pm.buffer) |
a06db751 | 1479 | goto out_mm; |
85863e47 | 1480 | |
356515e7 | 1481 | pagemap_walk.pmd_entry = pagemap_pmd_range; |
5d7e0d2b | 1482 | pagemap_walk.pte_hole = pagemap_pte_hole; |
1a5cb814 | 1483 | #ifdef CONFIG_HUGETLB_PAGE |
5dc37642 | 1484 | pagemap_walk.hugetlb_entry = pagemap_hugetlb_range; |
1a5cb814 | 1485 | #endif |
5d7e0d2b AM |
1486 | pagemap_walk.mm = mm; |
1487 | pagemap_walk.private = ± | |
1488 | ||
1489 | src = *ppos; | |
1490 | svpfn = src / PM_ENTRY_BYTES; | |
1491 | start_vaddr = svpfn << PAGE_SHIFT; | |
a06db751 | 1492 | end_vaddr = mm->task_size; |
5d7e0d2b AM |
1493 | |
1494 | /* watch out for wraparound */ | |
a06db751 | 1495 | if (svpfn > mm->task_size >> PAGE_SHIFT) |
5d7e0d2b AM |
1496 | start_vaddr = end_vaddr; |
1497 | ||
1498 | /* | |
1499 | * The odds are that this will stop walking way | |
1500 | * before end_vaddr, because the length of the | |
1501 | * user buffer is tracked in "pm", and the walk | |
1502 | * will stop when we hit the end of the buffer. | |
1503 | */ | |
d82ef020 KH |
1504 | ret = 0; |
1505 | while (count && (start_vaddr < end_vaddr)) { | |
1506 | int len; | |
1507 | unsigned long end; | |
1508 | ||
1509 | pm.pos = 0; | |
ea251c1d | 1510 | end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK; |
d82ef020 KH |
1511 | /* overflow ? */ |
1512 | if (end < start_vaddr || end > end_vaddr) | |
1513 | end = end_vaddr; | |
1514 | down_read(&mm->mmap_sem); | |
1515 | ret = walk_page_range(start_vaddr, end, &pagemap_walk); | |
1516 | up_read(&mm->mmap_sem); | |
1517 | start_vaddr = end; | |
1518 | ||
1519 | len = min(count, PM_ENTRY_BYTES * pm.pos); | |
309361e0 | 1520 | if (copy_to_user(buf, pm.buffer, len)) { |
d82ef020 | 1521 | ret = -EFAULT; |
a06db751 | 1522 | goto out_free; |
d82ef020 KH |
1523 | } |
1524 | copied += len; | |
1525 | buf += len; | |
1526 | count -= len; | |
85863e47 | 1527 | } |
d82ef020 KH |
1528 | *ppos += copied; |
1529 | if (!ret || ret == PM_END_OF_BUFFER) | |
1530 | ret = copied; | |
1531 | ||
98bc93e5 KM |
1532 | out_free: |
1533 | kfree(pm.buffer); | |
a06db751 KK |
1534 | out_mm: |
1535 | mmput(mm); | |
85863e47 MM |
1536 | out: |
1537 | return ret; | |
1538 | } | |
1539 | ||
541c237c PE |
1540 | static int pagemap_open(struct inode *inode, struct file *file) |
1541 | { | |
a06db751 KK |
1542 | struct mm_struct *mm; |
1543 | ||
a06db751 KK |
1544 | mm = proc_mem_open(inode, PTRACE_MODE_READ); |
1545 | if (IS_ERR(mm)) | |
1546 | return PTR_ERR(mm); | |
1547 | file->private_data = mm; | |
1548 | return 0; | |
1549 | } | |
1550 | ||
1551 | static int pagemap_release(struct inode *inode, struct file *file) | |
1552 | { | |
1553 | struct mm_struct *mm = file->private_data; | |
1554 | ||
1555 | if (mm) | |
1556 | mmdrop(mm); | |
541c237c PE |
1557 | return 0; |
1558 | } | |
1559 | ||
85863e47 MM |
1560 | const struct file_operations proc_pagemap_operations = { |
1561 | .llseek = mem_lseek, /* borrow this */ | |
1562 | .read = pagemap_read, | |
541c237c | 1563 | .open = pagemap_open, |
a06db751 | 1564 | .release = pagemap_release, |
85863e47 | 1565 | }; |
1e883281 | 1566 | #endif /* CONFIG_PROC_PAGE_MONITOR */ |
85863e47 | 1567 | |
6e21c8f1 | 1568 | #ifdef CONFIG_NUMA |
6e21c8f1 | 1569 | |
f69ff943 | 1570 | struct numa_maps { |
f69ff943 SW |
1571 | unsigned long pages; |
1572 | unsigned long anon; | |
1573 | unsigned long active; | |
1574 | unsigned long writeback; | |
1575 | unsigned long mapcount_max; | |
1576 | unsigned long dirty; | |
1577 | unsigned long swapcache; | |
1578 | unsigned long node[MAX_NUMNODES]; | |
1579 | }; | |
1580 | ||
5b52fc89 SW |
1581 | struct numa_maps_private { |
1582 | struct proc_maps_private proc_maps; | |
1583 | struct numa_maps md; | |
1584 | }; | |
1585 | ||
eb4866d0 DH |
1586 | static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty, |
1587 | unsigned long nr_pages) | |
f69ff943 SW |
1588 | { |
1589 | int count = page_mapcount(page); | |
1590 | ||
eb4866d0 | 1591 | md->pages += nr_pages; |
f69ff943 | 1592 | if (pte_dirty || PageDirty(page)) |
eb4866d0 | 1593 | md->dirty += nr_pages; |
f69ff943 SW |
1594 | |
1595 | if (PageSwapCache(page)) | |
eb4866d0 | 1596 | md->swapcache += nr_pages; |
f69ff943 SW |
1597 | |
1598 | if (PageActive(page) || PageUnevictable(page)) | |
eb4866d0 | 1599 | md->active += nr_pages; |
f69ff943 SW |
1600 | |
1601 | if (PageWriteback(page)) | |
eb4866d0 | 1602 | md->writeback += nr_pages; |
f69ff943 SW |
1603 | |
1604 | if (PageAnon(page)) | |
eb4866d0 | 1605 | md->anon += nr_pages; |
f69ff943 SW |
1606 | |
1607 | if (count > md->mapcount_max) | |
1608 | md->mapcount_max = count; | |
1609 | ||
eb4866d0 | 1610 | md->node[page_to_nid(page)] += nr_pages; |
f69ff943 SW |
1611 | } |
1612 | ||
3200a8aa DH |
1613 | static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma, |
1614 | unsigned long addr) | |
1615 | { | |
1616 | struct page *page; | |
1617 | int nid; | |
1618 | ||
1619 | if (!pte_present(pte)) | |
1620 | return NULL; | |
1621 | ||
1622 | page = vm_normal_page(vma, addr, pte); | |
1623 | if (!page) | |
1624 | return NULL; | |
1625 | ||
1626 | if (PageReserved(page)) | |
1627 | return NULL; | |
1628 | ||
1629 | nid = page_to_nid(page); | |
4ff1b2c2 | 1630 | if (!node_isset(nid, node_states[N_MEMORY])) |
3200a8aa DH |
1631 | return NULL; |
1632 | ||
1633 | return page; | |
1634 | } | |
1635 | ||
28093f9f GS |
1636 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
1637 | static struct page *can_gather_numa_stats_pmd(pmd_t pmd, | |
1638 | struct vm_area_struct *vma, | |
1639 | unsigned long addr) | |
1640 | { | |
1641 | struct page *page; | |
1642 | int nid; | |
1643 | ||
1644 | if (!pmd_present(pmd)) | |
1645 | return NULL; | |
1646 | ||
1647 | page = vm_normal_page_pmd(vma, addr, pmd); | |
1648 | if (!page) | |
1649 | return NULL; | |
1650 | ||
1651 | if (PageReserved(page)) | |
1652 | return NULL; | |
1653 | ||
1654 | nid = page_to_nid(page); | |
1655 | if (!node_isset(nid, node_states[N_MEMORY])) | |
1656 | return NULL; | |
1657 | ||
1658 | return page; | |
1659 | } | |
1660 | #endif | |
1661 | ||
f69ff943 SW |
1662 | static int gather_pte_stats(pmd_t *pmd, unsigned long addr, |
1663 | unsigned long end, struct mm_walk *walk) | |
1664 | { | |
d85f4d6d NH |
1665 | struct numa_maps *md = walk->private; |
1666 | struct vm_area_struct *vma = walk->vma; | |
f69ff943 SW |
1667 | spinlock_t *ptl; |
1668 | pte_t *orig_pte; | |
1669 | pte_t *pte; | |
1670 | ||
28093f9f | 1671 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
b6ec57f4 KS |
1672 | ptl = pmd_trans_huge_lock(pmd, vma); |
1673 | if (ptl) { | |
025c5b24 NH |
1674 | struct page *page; |
1675 | ||
28093f9f | 1676 | page = can_gather_numa_stats_pmd(*pmd, vma, addr); |
025c5b24 | 1677 | if (page) |
28093f9f | 1678 | gather_stats(page, md, pmd_dirty(*pmd), |
025c5b24 | 1679 | HPAGE_PMD_SIZE/PAGE_SIZE); |
bf929152 | 1680 | spin_unlock(ptl); |
025c5b24 | 1681 | return 0; |
32ef4384 DH |
1682 | } |
1683 | ||
1a5a9906 AA |
1684 | if (pmd_trans_unstable(pmd)) |
1685 | return 0; | |
28093f9f | 1686 | #endif |
f69ff943 SW |
1687 | orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl); |
1688 | do { | |
d85f4d6d | 1689 | struct page *page = can_gather_numa_stats(*pte, vma, addr); |
f69ff943 SW |
1690 | if (!page) |
1691 | continue; | |
eb4866d0 | 1692 | gather_stats(page, md, pte_dirty(*pte), 1); |
f69ff943 SW |
1693 | |
1694 | } while (pte++, addr += PAGE_SIZE, addr != end); | |
1695 | pte_unmap_unlock(orig_pte, ptl); | |
a66c0410 | 1696 | cond_resched(); |
f69ff943 SW |
1697 | return 0; |
1698 | } | |
1699 | #ifdef CONFIG_HUGETLB_PAGE | |
632fd60f | 1700 | static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask, |
f69ff943 SW |
1701 | unsigned long addr, unsigned long end, struct mm_walk *walk) |
1702 | { | |
5c2ff95e | 1703 | pte_t huge_pte = huge_ptep_get(pte); |
f69ff943 SW |
1704 | struct numa_maps *md; |
1705 | struct page *page; | |
1706 | ||
5c2ff95e | 1707 | if (!pte_present(huge_pte)) |
f69ff943 SW |
1708 | return 0; |
1709 | ||
5c2ff95e | 1710 | page = pte_page(huge_pte); |
f69ff943 SW |
1711 | if (!page) |
1712 | return 0; | |
1713 | ||
1714 | md = walk->private; | |
5c2ff95e | 1715 | gather_stats(page, md, pte_dirty(huge_pte), 1); |
f69ff943 SW |
1716 | return 0; |
1717 | } | |
1718 | ||
1719 | #else | |
632fd60f | 1720 | static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask, |
f69ff943 SW |
1721 | unsigned long addr, unsigned long end, struct mm_walk *walk) |
1722 | { | |
1723 | return 0; | |
1724 | } | |
1725 | #endif | |
1726 | ||
1727 | /* | |
1728 | * Display pages allocated per node and memory policy via /proc. | |
1729 | */ | |
b7643757 | 1730 | static int show_numa_map(struct seq_file *m, void *v, int is_pid) |
f69ff943 | 1731 | { |
5b52fc89 SW |
1732 | struct numa_maps_private *numa_priv = m->private; |
1733 | struct proc_maps_private *proc_priv = &numa_priv->proc_maps; | |
f69ff943 | 1734 | struct vm_area_struct *vma = v; |
5b52fc89 | 1735 | struct numa_maps *md = &numa_priv->md; |
f69ff943 SW |
1736 | struct file *file = vma->vm_file; |
1737 | struct mm_struct *mm = vma->vm_mm; | |
d85f4d6d NH |
1738 | struct mm_walk walk = { |
1739 | .hugetlb_entry = gather_hugetlb_stats, | |
1740 | .pmd_entry = gather_pte_stats, | |
1741 | .private = md, | |
1742 | .mm = mm, | |
1743 | }; | |
f69ff943 | 1744 | struct mempolicy *pol; |
948927ee DR |
1745 | char buffer[64]; |
1746 | int nid; | |
f69ff943 SW |
1747 | |
1748 | if (!mm) | |
1749 | return 0; | |
1750 | ||
5b52fc89 SW |
1751 | /* Ensure we start with an empty set of numa_maps statistics. */ |
1752 | memset(md, 0, sizeof(*md)); | |
f69ff943 | 1753 | |
498f2371 ON |
1754 | pol = __get_vma_policy(vma, vma->vm_start); |
1755 | if (pol) { | |
1756 | mpol_to_str(buffer, sizeof(buffer), pol); | |
1757 | mpol_cond_put(pol); | |
1758 | } else { | |
1759 | mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy); | |
1760 | } | |
f69ff943 SW |
1761 | |
1762 | seq_printf(m, "%08lx %s", vma->vm_start, buffer); | |
1763 | ||
1764 | if (file) { | |
17c2b4ee | 1765 | seq_puts(m, " file="); |
2726d566 | 1766 | seq_file_path(m, file, "\n\t= "); |
f69ff943 | 1767 | } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) { |
17c2b4ee | 1768 | seq_puts(m, " heap"); |
1240ea0d | 1769 | } else if (is_stack(vma)) { |
65376df5 | 1770 | seq_puts(m, " stack"); |
f69ff943 SW |
1771 | } |
1772 | ||
fc360bd9 | 1773 | if (is_vm_hugetlb_page(vma)) |
17c2b4ee | 1774 | seq_puts(m, " huge"); |
fc360bd9 | 1775 | |
d85f4d6d NH |
1776 | /* mmap_sem is held by m_start */ |
1777 | walk_page_vma(vma, &walk); | |
f69ff943 SW |
1778 | |
1779 | if (!md->pages) | |
1780 | goto out; | |
1781 | ||
1782 | if (md->anon) | |
1783 | seq_printf(m, " anon=%lu", md->anon); | |
1784 | ||
1785 | if (md->dirty) | |
1786 | seq_printf(m, " dirty=%lu", md->dirty); | |
1787 | ||
1788 | if (md->pages != md->anon && md->pages != md->dirty) | |
1789 | seq_printf(m, " mapped=%lu", md->pages); | |
1790 | ||
1791 | if (md->mapcount_max > 1) | |
1792 | seq_printf(m, " mapmax=%lu", md->mapcount_max); | |
1793 | ||
1794 | if (md->swapcache) | |
1795 | seq_printf(m, " swapcache=%lu", md->swapcache); | |
1796 | ||
1797 | if (md->active < md->pages && !is_vm_hugetlb_page(vma)) | |
1798 | seq_printf(m, " active=%lu", md->active); | |
1799 | ||
1800 | if (md->writeback) | |
1801 | seq_printf(m, " writeback=%lu", md->writeback); | |
1802 | ||
948927ee DR |
1803 | for_each_node_state(nid, N_MEMORY) |
1804 | if (md->node[nid]) | |
1805 | seq_printf(m, " N%d=%lu", nid, md->node[nid]); | |
198d1597 RA |
1806 | |
1807 | seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10); | |
f69ff943 SW |
1808 | out: |
1809 | seq_putc(m, '\n'); | |
b8c20a9b | 1810 | m_cache_vma(m, vma); |
f69ff943 SW |
1811 | return 0; |
1812 | } | |
5b52fc89 | 1813 | |
b7643757 SP |
1814 | static int show_pid_numa_map(struct seq_file *m, void *v) |
1815 | { | |
1816 | return show_numa_map(m, v, 1); | |
1817 | } | |
1818 | ||
1819 | static int show_tid_numa_map(struct seq_file *m, void *v) | |
1820 | { | |
1821 | return show_numa_map(m, v, 0); | |
1822 | } | |
1823 | ||
03a44825 | 1824 | static const struct seq_operations proc_pid_numa_maps_op = { |
b7643757 SP |
1825 | .start = m_start, |
1826 | .next = m_next, | |
1827 | .stop = m_stop, | |
1828 | .show = show_pid_numa_map, | |
6e21c8f1 | 1829 | }; |
662795de | 1830 | |
b7643757 SP |
1831 | static const struct seq_operations proc_tid_numa_maps_op = { |
1832 | .start = m_start, | |
1833 | .next = m_next, | |
1834 | .stop = m_stop, | |
1835 | .show = show_tid_numa_map, | |
1836 | }; | |
1837 | ||
1838 | static int numa_maps_open(struct inode *inode, struct file *file, | |
1839 | const struct seq_operations *ops) | |
662795de | 1840 | { |
4db7d0ee ON |
1841 | return proc_maps_open(inode, file, ops, |
1842 | sizeof(struct numa_maps_private)); | |
662795de EB |
1843 | } |
1844 | ||
b7643757 SP |
1845 | static int pid_numa_maps_open(struct inode *inode, struct file *file) |
1846 | { | |
1847 | return numa_maps_open(inode, file, &proc_pid_numa_maps_op); | |
1848 | } | |
1849 | ||
1850 | static int tid_numa_maps_open(struct inode *inode, struct file *file) | |
1851 | { | |
1852 | return numa_maps_open(inode, file, &proc_tid_numa_maps_op); | |
1853 | } | |
1854 | ||
1855 | const struct file_operations proc_pid_numa_maps_operations = { | |
1856 | .open = pid_numa_maps_open, | |
1857 | .read = seq_read, | |
1858 | .llseek = seq_lseek, | |
29a40ace | 1859 | .release = proc_map_release, |
b7643757 SP |
1860 | }; |
1861 | ||
1862 | const struct file_operations proc_tid_numa_maps_operations = { | |
1863 | .open = tid_numa_maps_open, | |
662795de EB |
1864 | .read = seq_read, |
1865 | .llseek = seq_lseek, | |
29a40ace | 1866 | .release = proc_map_release, |
662795de | 1867 | }; |
f69ff943 | 1868 | #endif /* CONFIG_NUMA */ |