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