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