[PATCH] tools/mm/thpmaps: handle carriage returns and non-UTF-8 filenames
From: Weilin Tong
Date: Mon Nov 02 2026 - 03:56:34 EST
VMAList opens smaps with universal newline handling and strict decoding.
A carriage return in a mapped filename can split a header into multiple
records, while non-UTF-8 bytes cause UnicodeDecodeError.
Read LF-terminated records and use surrogateescape to preserve filename
bytes.
Fixes: 2444172cfde4 ("tools/mm: add thpmaps script to dump THP usage info")
Suggested-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Link: https://lore.kernel.org/linux-mm/20261101211644.c63a2028bbede0f763f41f80@xxxxxxxxxxxxxxxxxxxx/
Signed-off-by: Weilin Tong <tongweilin@xxxxxxxxxxxxxxxxx>
---
This is a follow-up to "tools/mm/thpmaps: fix missing mapping names":
https://lore.kernel.org/linux-mm/20261027074904.3328878-4-tongweilin@xxxxxxxxxxxxxxxxx/
tools/mm/thpmaps | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/tools/mm/thpmaps b/tools/mm/thpmaps
index a1a9bc30514f..f89568bd499e 100644
--- a/tools/mm/thpmaps
+++ b/tools/mm/thpmaps
@@ -196,7 +196,8 @@ class VMAList:
# instance to receive VMAs.
def __init__(self, pid='self', stats=[]):
self.vmas = []
- with open(f'/proc/{pid}/smaps', 'r') as file:
+ with open(f'/proc/{pid}/smaps', 'r', newline='\n',
+ errors='surrogateescape') as file:
for line in file:
elements = line.rstrip('\n').split(maxsplit=5)
if '-' in elements[0]:
--
2.43.7