[PATCH 1/3] tools/mm/thpmaps: fix partial THP anon/file accounting

From: Weilin Tong

Date: Tue Oct 27 2026 - 03:49:33 EST


thp_parse() reduces index_end before looking up the type of the
trailing partially mapped THP. The lookup then uses the preceding
page, or anons[-1] if the THP starts at index zero, so its pages can
be charged to the wrong anon/file counter.

Account the partial THP before reducing index_end.

Fixes: 2444172cfde4 ("tools/mm: add thpmaps script to dump THP usage info")
Assisted-by: LLM
Signed-off-by: Weilin Tong <tongweilin@xxxxxxxxxxxxxxxxx>
---
tools/mm/thpmaps | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/mm/thpmaps b/tools/mm/thpmaps
index 803e0318f2fe..d92aace005c1 100644
--- a/tools/mm/thpmaps
+++ b/tools/mm/thpmaps
@@ -263,8 +263,8 @@ def thp_parse(vma, kpageflags, ranges, indexes, vfns, pfns, anons, heads):
if flags & KPF_COMPOUND_TAIL:
nr = index_end - int(folios[-1])
folios = folios[:-1]
- index_end -= nr
stats['anon' if anons[index_end - 1] else 'file']['partial'] += nr
+ index_end -= nr

# Account fully mapped THPs in the middle of the range.
if len(folios):
--
2.43.7