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