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