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