Skip to contentSkip to conversation

tools/mm/thpmaps: handle carriage returns and non-UTF-8 filenames

PATCH In review Weilin Tong 2 participants 2 messages Nov 4 – Nov 10 Memory management

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tools/mm/thpmaps: handle carriage returns and non-UTF-8 filenames

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. Escape filename characters that stdout cannot encode to avoid UnicodeEncodeError when printing mapping names.

Fixes 2444172cfde4 ("tools/mm: add thpmaps script to dump THP usage info")

---
Changes in v2:
- Escape unencodable mapping names in vma_print() (Sashiko).
- Rebase onto mm-new.

Link to v1: https://lore.kernel.org/linux-mm/[email protected]/

 tools/mm/thpmaps | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/mm/thpmaps +5 -2
index a1a9bc30514f..0e17723515cd 100644
@@ -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]:
@@ -363,12 +364,14 @@ def cont_parse(vma, order, ranges, anons, heads):
 def vma_print(vma, pid):
     # Prints a VMA instance in a format similar to smaps. The main difference is
     # that the pid is included as the first value.
+    name = vma.name.encode(sys.stdout.encoding, errors='backslashreplace')
+    name = name.decode(sys.stdout.encoding)
     print("{:010d}: {:016x}-{:016x} {}{}{}{} {:08x} {:02x}:{:02x} {:08x} {}"
         .format(
             pid, vma.start, vma.end,
             'r' if vma.read else '-', 'w' if vma.write else '-',
             'x' if vma.execute else '-', 'p' if vma.private else 's',
-            vma.pgoff, vma.major, vma.minor, vma.inode, vma.name
+            vma.pgoff, vma.major, vma.minor, vma.inode, name
         ))
 
 

base-commit: 74d73ffcffe03804f97e9fb82301dadf75671282

Reviewed-by on v2 1/1
#1 lore raw
Quoted 4 lines · Weilin Tong

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.

Quoted 8 lines

Read LF-terminated records and use surrogateescape to preserve filename bytes. Escape filename characters that stdout cannot encode to avoid UnicodeEncodeError when printing mapping names.

Fixes 2444172cfde4 ("tools/mm: add thpmaps script to dump THP usage info")
Quoted 42 lines

---
Changes in v2:
- Escape unencodable mapping names in vma_print() (Sashiko).
- Rebase onto mm-new.

Link to v1: https://lore.kernel.org/linux-mm/[email protected]/

 tools/mm/thpmaps | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/mm/thpmaps +5 -2
index a1a9bc30514f..0e17723515cd 100644
@@ -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]:
@@ -363,12 +364,14 @@ def cont_parse(vma, order, ranges, anons, heads):
 def vma_print(vma, pid):
     # Prints a VMA instance in a format similar to smaps. The main difference is
     # that the pid is included as the first value.
+    name = vma.name.encode(sys.stdout.encoding, errors='backslashreplace')
+    name = name.decode(sys.stdout.encoding)
     print("{:010d}: {:016x}-{:016x} {}{}{}{} {:08x} {:02x}:{:02x} {:08x} {}"
         .format(
             pid, vma.start, vma.end,
             'r' if vma.read else '-', 'w' if vma.write else '-',
             'x' if vma.execute else '-', 'p' if vma.private else 's',
-            vma.pgoff, vma.major, vma.minor, vma.inode, vma.name
+            vma.pgoff, vma.major, vma.minor, vma.inode, name
         ))
 
 

base-commit: 74d73ffcffe03804f97e9fb82301dadf75671282