Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

proc: fix vma display mismatch between /proc/pid/{maps,smaps}

Commit 4752c369789250eafcd7813e11c8fb689235b0d2 aka
"maps4: simplify interdependence of maps and smaps" broke /proc/pid/smaps,
causing it to display some vmas twice and other vmas not at all. For example:

grep .- /proc/1/smaps >/tmp/smaps; diff /proc/1/maps /tmp/smaps

1 25d24
2 < 7fd7e23aa000-7fd7e23ac000 rw-p 7fd7e23aa000 00:00 0
3 28a28
4 > ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]

The bug has something to do with setting m->version before all the
seq_printf's have been performed. show_map was doing this correctly,
but show_smap was doing this in the middle of its seq_printf sequence.
This patch arranges things so that the setting of m->version in show_smap
is also done at the end of its seq_printf sequence.

Testing: in addition to the above grep test, for each process I summed
up the 'Rss' fields of /proc/pid/smaps and compared that to the 'VmRSS'
field of /proc/pid/status. All matched except for Xorg (which has a
/dev/mem mapping which Rss accounts for but VmRSS does not). This result
gives us some confidence that neither /proc/pid/maps nor /proc/pid/smaps
are any longer skipping or double-counting vmas.

Signed-off-by: Joe Korty <joe.korty@ccur.com>
Cc: Matt Mackall <mpm@selenic.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>

authored by

Joe Korty and committed by
Alexey Dobriyan
7c88db0c 2515ddc6

+16 -9
+16 -9
fs/proc/task_mmu.c
··· 198 198 return ret; 199 199 } 200 200 201 - static int show_map(struct seq_file *m, void *v) 201 + static void show_map_vma(struct seq_file *m, struct vm_area_struct *vma) 202 202 { 203 - struct proc_maps_private *priv = m->private; 204 - struct task_struct *task = priv->task; 205 - struct vm_area_struct *vma = v; 206 203 struct mm_struct *mm = vma->vm_mm; 207 204 struct file *file = vma->vm_file; 208 205 int flags = vma->vm_flags; ··· 251 254 } 252 255 } 253 256 seq_putc(m, '\n'); 257 + } 258 + 259 + static int show_map(struct seq_file *m, void *v) 260 + { 261 + struct vm_area_struct *vma = v; 262 + struct proc_maps_private *priv = m->private; 263 + struct task_struct *task = priv->task; 264 + 265 + show_map_vma(m, vma); 254 266 255 267 if (m->count < m->size) /* vma is copied successfully */ 256 268 m->version = (vma != get_gate_vma(task))? vma->vm_start: 0; ··· 370 364 371 365 static int show_smap(struct seq_file *m, void *v) 372 366 { 367 + struct proc_maps_private *priv = m->private; 368 + struct task_struct *task = priv->task; 373 369 struct vm_area_struct *vma = v; 374 370 struct mem_size_stats mss; 375 - int ret; 376 371 struct mm_walk smaps_walk = { 377 372 .pmd_entry = smaps_pte_range, 378 373 .mm = vma->vm_mm, ··· 385 378 if (vma->vm_mm && !is_vm_hugetlb_page(vma)) 386 379 walk_page_range(vma->vm_start, vma->vm_end, &smaps_walk); 387 380 388 - ret = show_map(m, v); 389 - if (ret) 390 - return ret; 381 + show_map_vma(m, vma); 391 382 392 383 seq_printf(m, 393 384 "Size: %8lu kB\n" ··· 407 402 mss.referenced >> 10, 408 403 mss.swap >> 10); 409 404 410 - return ret; 405 + if (m->count < m->size) /* vma is copied successfully */ 406 + m->version = (vma != get_gate_vma(task)) ? vma->vm_start : 0; 407 + return 0; 411 408 } 412 409 413 410 static const struct seq_operations proc_pid_smaps_op = {