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

tools/mm: add script to display page state for a given PID and VADDR

Introduces a new drgn script, `show_page_info.py`, which allows users
to analyze the state of a page given a process ID (PID) and a virtual
address (VADDR). This can help kernel developers or debuggers easily
inspect page-related information in a live kernel or vmcore.

The script extracts information such as the page flags, mapping, and
other metadata relevant to diagnosing memory issues.

Output example:
sudo ./show_page_info.py 1 0x7fc988181000
PID: 1 Comm: systemd mm: 0xffff8d22c4089700
RAW: 0017ffffc000416c fffff939062ff708 fffff939062ffe08 ffff8d23062a12a8
RAW: 0000000000000000 ffff8d2323438f60 0000002500000007 ffff8d23203ff500
Page Address: 0xfffff93905664e00
Page Flags: PG_referenced|PG_uptodate|PG_lru|PG_head|PG_active|
PG_private|PG_reported|PG_has_hwpoisoned
Page Size: 4096
Page PFN: 0x159938
Page Physical: 0x159938000
Page Virtual: 0xffff8d2319938000
Page Refcount: 37
Page Mapcount: 7
Page Index: 0x0
Page Memcg Data: 0xffff8d23203ff500
Memcg Name: init.scope
Memcg Path: /sys/fs/cgroup/memory/init.scope
Page Mapping: 0xffff8d23062a12a8
Page Anon/File: File
Page VMA: 0xffff8d22e06e0e40
VMA Start: 0x7fc988181000
VMA End: 0x7fc988185000
This page is part of a compound page.
This page is the head page of a compound page.
Head Page: 0xfffff93905664e00
Compound Order: 2
Number of Pages: 4

Link: https://lkml.kernel.org/r/20250530055855.687067-1-ye.liu@linux.dev
Signed-off-by: Ye Liu <liuye@kylinos.cn>
Tested-by: SeongJae Park <sj@kernel.org>
Reviewed-by: Stephen Brennan <stephen.s.brennan@oracle.com>
Cc: Florian Weimer <fweimer@redhat.com>
Cc: Omar Sandoval <osandov@osandov.com>
Cc: "Paul E . McKenney" <paulmck@kernel.org>
Cc: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Ye Liu and committed by
Andrew Morton
fee8870a af827e09

+174
+5
MAINTAINERS
··· 18818 18818 F: include/linux/page_table_check.h 18819 18819 F: mm/page_table_check.c 18820 18820 18821 + PAGE STATE DEBUG SCRIPT 18822 + M: Ye Liu <liuye@kylinos.cn> 18823 + S: Maintained 18824 + F: tools/mm/show_page_info.py 18825 + 18821 18826 PANASONIC LAPTOP ACPI EXTRAS DRIVER 18822 18827 M: Kenneth Chan <kenneth.t.chan@gmail.com> 18823 18828 L: platform-driver-x86@vger.kernel.org
+169
tools/mm/show_page_info.py
··· 1 + #!/usr/bin/env drgn 2 + # SPDX-License-Identifier: GPL-2.0-only 3 + # Copyright (C) 2025 Ye Liu <liuye@kylinos.cn> 4 + 5 + import argparse 6 + import sys 7 + from drgn import Object, FaultError, PlatformFlags, cast 8 + from drgn.helpers.linux import find_task, follow_page, page_size 9 + from drgn.helpers.linux.mm import ( 10 + decode_page_flags, page_to_pfn, page_to_phys, page_to_virt, vma_find, 11 + PageSlab, PageCompound, PageHead, PageTail, compound_head, compound_order, compound_nr 12 + ) 13 + from drgn.helpers.linux.cgroup import cgroup_name, cgroup_path 14 + 15 + DESC = """ 16 + This is a drgn script to show the page state. 17 + For more info on drgn, visit https://github.com/osandov/drgn. 18 + """ 19 + 20 + def format_page_data(page): 21 + """ 22 + Format raw page data into a readable hex dump with "RAW:" prefix. 23 + 24 + :param page: drgn.Object instance representing the page. 25 + :return: Formatted string of memory contents. 26 + """ 27 + try: 28 + address = page.value_() 29 + size = prog.type("struct page").size 30 + 31 + if prog.platform.flags & PlatformFlags.IS_64_BIT: 32 + word_size = 8 33 + else: 34 + word_size = 4 35 + num_words = size // word_size 36 + 37 + values = [] 38 + for i in range(num_words): 39 + word_address = address + i * word_size 40 + word = prog.read_word(word_address) 41 + values.append(f"{word:0{word_size * 2}x}") 42 + 43 + lines = [f"RAW: {' '.join(values[i:i + 4])}" for i in range(0, len(values), 4)] 44 + 45 + return "\n".join(lines) 46 + 47 + except FaultError as e: 48 + return f"Error reading memory: {e}" 49 + except Exception as e: 50 + return f"Unexpected error: {e}" 51 + 52 + def get_memcg_info(page): 53 + """Retrieve memory cgroup information for a page.""" 54 + try: 55 + MEMCG_DATA_OBJEXTS = prog.constant("MEMCG_DATA_OBJEXTS").value_() 56 + MEMCG_DATA_KMEM = prog.constant("MEMCG_DATA_KMEM").value_() 57 + mask = prog.constant('__NR_MEMCG_DATA_FLAGS').value_() - 1 58 + memcg_data = page.memcg_data.read_() 59 + if memcg_data & MEMCG_DATA_OBJEXTS: 60 + slabobj_ext = cast("struct slabobj_ext *", memcg_data & ~mask) 61 + memcg = slabobj_ext.objcg.memcg.value_() 62 + elif memcg_data & MEMCG_DATA_KMEM: 63 + objcg = cast("struct obj_cgroup *", memcg_data & ~mask) 64 + memcg = objcg.memcg.value_() 65 + else: 66 + memcg = cast("struct mem_cgroup *", memcg_data & ~mask) 67 + 68 + if memcg.value_() == 0: 69 + return "none", "/sys/fs/cgroup/memory/" 70 + cgrp = memcg.css.cgroup 71 + return cgroup_name(cgrp).decode(), f"/sys/fs/cgroup/memory{cgroup_path(cgrp).decode()}" 72 + except FaultError as e: 73 + return "unknown", f"Error retrieving memcg info: {e}" 74 + except Exception as e: 75 + return "unknown", f"Unexpected error: {e}" 76 + 77 + def show_page_state(page, addr, mm, pid, task): 78 + """Display detailed information about a page.""" 79 + try: 80 + print(f'PID: {pid} Comm: {task.comm.string_().decode()} mm: {hex(mm)}') 81 + try: 82 + print(format_page_data(page)) 83 + except FaultError as e: 84 + print(f"Error reading page data: {e}") 85 + fields = { 86 + "Page Address": hex(page.value_()), 87 + "Page Flags": decode_page_flags(page), 88 + "Page Size": prog["PAGE_SIZE"].value_(), 89 + "Page PFN": hex(page_to_pfn(page).value_()), 90 + "Page Physical": hex(page_to_phys(page).value_()), 91 + "Page Virtual": hex(page_to_virt(page).value_()), 92 + "Page Refcount": page._refcount.counter.value_(), 93 + "Page Mapcount": page._mapcount.counter.value_(), 94 + "Page Index": hex(page.__folio_index.value_()), 95 + "Page Memcg Data": hex(page.memcg_data.value_()), 96 + } 97 + 98 + memcg_name, memcg_path = get_memcg_info(page) 99 + fields["Memcg Name"] = memcg_name 100 + fields["Memcg Path"] = memcg_path 101 + fields["Page Mapping"] = hex(page.mapping.value_()) 102 + fields["Page Anon/File"] = "Anon" if page.mapping.value_() & 0x1 else "File" 103 + 104 + try: 105 + vma = vma_find(mm, addr) 106 + fields["Page VMA"] = hex(vma.value_()) 107 + fields["VMA Start"] = hex(vma.vm_start.value_()) 108 + fields["VMA End"] = hex(vma.vm_end.value_()) 109 + except FaultError as e: 110 + fields["Page VMA"] = "Unavailable" 111 + fields["VMA Start"] = "Unavailable" 112 + fields["VMA End"] = "Unavailable" 113 + print(f"Error retrieving VMA information: {e}") 114 + 115 + # Calculate the maximum field name length for alignment 116 + max_field_len = max(len(field) for field in fields) 117 + 118 + # Print aligned fields 119 + for field, value in fields.items(): 120 + print(f"{field}:".ljust(max_field_len + 2) + f"{value}") 121 + 122 + # Additional information about the page 123 + if PageSlab(page): 124 + print("This page belongs to the slab allocator.") 125 + 126 + if PageCompound(page): 127 + print("This page is part of a compound page.") 128 + if PageHead(page): 129 + print("This page is the head page of a compound page.") 130 + if PageTail(page): 131 + print("This page is the tail page of a compound page.") 132 + print(f"{'Head Page:'.ljust(max_field_len + 2)}{hex(compound_head(page).value_())}") 133 + print(f"{'Compound Order:'.ljust(max_field_len + 2)}{compound_order(page).value_()}") 134 + print(f"{'Number of Pages:'.ljust(max_field_len + 2)}{compound_nr(page).value_()}") 135 + else: 136 + print("This page is not part of a compound page.") 137 + except FaultError as e: 138 + print(f"Error accessing page state: {e}") 139 + except Exception as e: 140 + print(f"Unexpected error: {e}") 141 + 142 + def main(): 143 + """Main function to parse arguments and display page state.""" 144 + parser = argparse.ArgumentParser(description=DESC, formatter_class=argparse.RawTextHelpFormatter) 145 + parser.add_argument('pid', metavar='PID', type=int, help='Target process ID (PID)') 146 + parser.add_argument('vaddr', metavar='VADDR', type=str, help='Target virtual address in hexadecimal format (e.g., 0x7fff1234abcd)') 147 + args = parser.parse_args() 148 + 149 + try: 150 + vaddr = int(args.vaddr, 16) 151 + except ValueError: 152 + sys.exit(f"Error: Invalid virtual address format: {args.vaddr}") 153 + 154 + try: 155 + task = find_task(args.pid) 156 + mm = task.mm 157 + page = follow_page(mm, vaddr) 158 + 159 + if page: 160 + show_page_state(page, vaddr, mm, args.pid, task) 161 + else: 162 + sys.exit(f"Address {hex(vaddr)} is not mapped.") 163 + except FaultError as e: 164 + sys.exit(f"Error accessing task or memory: {e}") 165 + except Exception as e: 166 + sys.exit(f"Unexpected error: {e}") 167 + 168 + if __name__ == "__main__": 169 + main()