Skip to contentSkip to conversation

fs/proc/task_mmu: fix lost maps entries above 2^63

PATCH Superseded Stian Halseth 2 participants 2 messages Oct 13

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 fs/proc/task_mmu: fix lost maps entries above 2^63

When a read of /proc/pid/{maps,smaps,numa_maps} resumes, m_start() rewinds *ppos to priv->last_pos so that the vma which did not fit into the previous buffer is shown again. It only does so for last_addr > 0, meaning to exclude the start of the file and SENTINEL_VMA_GATE, but last_addr is a signed loff_t holding a vm_end. sparc64 maps shared libraries, anonymous memory and the stack above 0x8000000000000000, so the rewind does not happen there and one entry is lost at every read() boundary.

Reading the maps file of a process with read sizes from 64 to 8192 bytes in steps of 64 loses lines at 126 of the 128 sizes, smaps at 123 and numa_maps at 97, and the proc-maps-race selftest fails 4 of its 6 tests. Python's sys.remote_exec() reads maps with fgets() and fails intermittently when it misses the libpython data mapping.

Skip the rewind only for 0 and the two sentinels instead, with an addr_is_sentinel() helper.

Fixes 5631da56c9a87 ("fs/proc/task_mmu: read proc/pid/maps under per-vma lock")

---

Notes:
v2: addr_is_sentinel() covering both sentinels, as the comment says (David Hildenbrand). No functional change: m_start() returns early on SENTINEL_VMA_END. Re-tested on the T4 (7.2.10): maps and smaps read at 128 sizes, 200 and 1000 mappings, no lost lines.
v1: https://lore.kernel.org/all/[email protected]/

Tested on a SPARC T7-1 (sun4v, 7.3-rc5), a T4-1 (7.2.9) and a Sun Fire V240 (sun4u, 7.3-rc5): no lost lines at any read size afterwards, and Python's sys.remote_exec() test passes 20/20. On an amd64 KVM guest maps, smaps, numa_maps and the proc selftests are unchanged.

 fs/proc/task_mmu.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/proc/task_mmu.c +6 -1
index 5c54aebe21182..9fd6a360919b9 100644
@@ -32,6 +32,11 @@
 #define SENTINEL_VMA_END	-1
 #define SENTINEL_VMA_GATE	-2
 
+static inline bool addr_is_sentinel(loff_t addr)
+{
+	return addr == SENTINEL_VMA_END || addr == SENTINEL_VMA_GATE;
+}
+
 #define SEQ_PUT_DEC(str, val) \
 		seq_put_decimal_ull_width(m, str, (val) << (PAGE_SHIFT-10), 8)
 void task_mem(struct seq_file *m, struct mm_struct *mm)
@@ -339,7 +344,7 @@ static void *m_start(struct seq_file *m, loff_t *ppos)
 	 * Reset current position if last_addr was set before
 	 * and it's not a sentinel.
 	 */
-	if (last_addr > 0)
+	if (last_addr && !addr_is_sentinel(last_addr))
 		*ppos = last_addr = priv->last_pos;
 	vma_iter_init(&priv->iter, mm, (unsigned long)last_addr);
 	hold_task_mempolicy(priv);
Signature · 1 line
2.43.0
Acked-by on v2 1/1
#1 lore raw
Quoted 44 lines · Stian Halseth

When a read of /proc/pid/{maps,smaps,numa_maps} resumes, m_start() rewinds *ppos to priv->last_pos so that the vma which did not fit into the previous buffer is shown again. It only does so for last_addr > 0, meaning to exclude the start of the file and SENTINEL_VMA_GATE, but last_addr is a signed loff_t holding a vm_end. sparc64 maps shared libraries, anonymous memory and the stack above 0x8000000000000000, so the rewind does not happen there and one entry is lost at every read() boundary.

Reading the maps file of a process with read sizes from 64 to 8192 bytes in steps of 64 loses lines at 126 of the 128 sizes, smaps at 123 and numa_maps at 97, and the proc-maps-race selftest fails 4 of its 6 tests. Python's sys.remote_exec() reads maps with fgets() and fails intermittently when it misses the libpython data mapping.

Skip the rewind only for 0 and the two sentinels instead, with an addr_is_sentinel() helper.

Fixes 5631da56c9a87 ("fs/proc/task_mmu: read proc/pid/maps under per-vma lock")

---

Notes:
v2: addr_is_sentinel() covering both sentinels, as the comment says (David Hildenbrand). No functional change: m_start() returns early on SENTINEL_VMA_END. Re-tested on the T4 (7.2.10): maps and smaps read at 128 sizes, 200 and 1000 mappings, no lost lines.
v1: https://lore.kernel.org/all/[email protected]/

Tested on a SPARC T7-1 (sun4v, 7.3-rc5), a T4-1 (7.2.9) and a Sun Fire V240 (sun4u, 7.3-rc5): no lost lines at any read size afterwards, and Python's sys.remote_exec() test passes 20/20. On an amd64 KVM guest maps, smaps, numa_maps and the proc selftests are unchanged.

 fs/proc/task_mmu.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/proc/task_mmu.c +0 -0
index 5c54aebe21182..9fd6a360919b9 100644

fs/proc/task_mmu.c line 35

Quoted 4 lines
fs/proc/task_mmu.c +1 -0
@@ -32,6 +32,11 @@
 #define SENTINEL_VMA_END	-1
 #define SENTINEL_VMA_GATE	-2
 
+static inline bool addr_is_sentinel(loff_t addr)

Nit: no need for the "inline". The compiler is smart enough :)

Signature · 3 lines
Cheers,

David