* [v3 PATCH 1/2] powerpc/mm: Dump linux pagetables @ 2016-05-27 5:48 Rashmica Gupta 2016-05-27 5:49 ` [v3 PATCH 2/2] powerpc/mm: Dump hash table Rashmica Gupta 2016-11-17 12:03 ` [v3,1/2] powerpc/mm: Dump linux pagetables Michael Ellerman 0 siblings, 2 replies; 9+ messages in thread From: Rashmica Gupta @ 2016-05-27 5:48 UTC (permalink / raw) To: linuxppc-dev, mpe Useful to be able to dump the kernels page tables to check permissions and memory types - derived from arm64's implementation. Add a debugfs file to check the page tables. To use this the PPC_PTDUMP config option must be selected. Signed-off-by: Rashmica Gupta <rashmicy@gmail.com> --- v2 -> v3: Changed to be compatible with P9. arch/powerpc/Kconfig.debug | 12 + arch/powerpc/mm/Makefile | 1 + arch/powerpc/mm/dump_linuxpagetables.c | 440 +++++++++++++++++++++++++++++++++ 3 files changed, 453 insertions(+) create mode 100644 arch/powerpc/mm/dump_linuxpagetables.c diff --git a/arch/powerpc/Kconfig.debug b/arch/powerpc/Kconfig.debug index 2be221874de8..d19f496a7e12 100644 --- a/arch/powerpc/Kconfig.debug +++ b/arch/powerpc/Kconfig.debug @@ -350,4 +350,16 @@ config FAIL_IOMMU If you are unsure, say N. +config PPC_PTDUMP + bool "Export kernel pagetable layout to userspace via debugfs" + depends on DEBUG_KERNEL + select DEBUG_FS + help + This option exports the state of the kernel pagetables to a + debugfs file. This is only useful for kernel developers who are + working in architecture specific areas of the kernel - probably + not a good idea to enable this feature in a production kernel. + + If you are unsure, say N. + endmenu diff --git a/arch/powerpc/mm/Makefile b/arch/powerpc/mm/Makefile index f2cea6d5e764..d3dfa02082b1 100644 --- a/arch/powerpc/mm/Makefile +++ b/arch/powerpc/mm/Makefile @@ -43,3 +43,4 @@ obj-$(CONFIG_NOT_COHERENT_CACHE) += dma-noncoherent.o obj-$(CONFIG_HIGHMEM) += highmem.o obj-$(CONFIG_PPC_COPRO_BASE) += copro_fault.o obj-$(CONFIG_SPAPR_TCE_IOMMU) += mmu_context_iommu.o +obj-$(CONFIG_PPC_PTDUMP) += dump_linuxpagetables.o diff --git a/arch/powerpc/mm/dump_linuxpagetables.c b/arch/powerpc/mm/dump_linuxpagetables.c new file mode 100644 index 000000000000..d242bc79ae64 --- /dev/null +++ b/arch/powerpc/mm/dump_linuxpagetables.c @@ -0,0 +1,440 @@ +/* + * Copyright 2016, Rashmica Gupta, IBM Corp. + * + * This traverses the kernel pagetables and dumps the + * information about the used sections of memory to + * /sys/kernel/debug/kernel_pagetables. + * + * Derived from the arm64 implementation: + * Copyright (c) 2014, The Linux Foundation, Laura Abbott. + * (C) Copyright 2008 Intel Corporation, Arjan van de Ven. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; version 2 + * of the License. + */ +#include <linux/debugfs.h> +#include <linux/fs.h> +#include <linux/io.h> +#include <linux/mm.h> +#include <linux/sched.h> +#include <linux/seq_file.h> +#include <asm/fixmap.h> +#include <asm/pgtable.h> +#include <linux/const.h> +#include <asm/page.h> +#include <asm/pgalloc.h> + +/* + * To visualise what is happening, + * + * - PTRS_PER_P** = how many entries there are in the corresponding P** + * - P**_SHIFT = how many bits of the address we use to index into the + * corresponding P** + * - P**_SIZE is how much memory we can access through the table - not the + * size of the table itself. + * P**={PGD, PUD, PMD, PTE} + * + * + * Each entry of the PGD points to a PUD. Each entry of a PUD points to a + * PMD. Each entry of a PMD points to a PTE. And every PTE entry points to + * a page. + * + * In the case where there are only 3 levels, the PUD is folded into the + * PGD: every PUD has only one entry which points to the PMD. + * + * The page dumper groups page table entries of the same type into a single + * description. It uses pg_state to track the range information while + * iterating over the PTE entries. When the continuity is broken it then + * dumps out a description of the range - ie PTEs that are virtually contiguous + * with the same PTE flags are chunked together. This is to make it clear how + * different areas of the kernel virtual memory are used. + * + */ +struct pg_state { + struct seq_file *seq; + const struct addr_marker *marker; + unsigned long start_address; + unsigned int level; + u64 current_flags; +}; + +struct addr_marker { + unsigned long start_address; + const char *name; +}; + +static struct addr_marker address_markers[] = { + { 0, "Start of kernel VM" }, + { 0, "vmalloc() Area" }, + { 0, "vmalloc() End" }, + { 0, "isa I/O start" }, + { 0, "isa I/O end" }, + { 0, "phb I/O start" }, + { 0, "phb I/O end" }, + { 0, "I/O remap start" }, + { 0, "I/O remap end" }, + { 0, "vmemmap start" }, + { -1, NULL }, +}; + +struct flag_info { + u64 mask; + u64 val; + const char *set; + const char *clear; + bool is_val; + int shift; +}; + +static const struct flag_info flag_array[] = { + { +#ifdef CONFIG_PPC_STD_MMU_64 + .mask = _PAGE_PRIVILEGED, + .val = 0, +#else + .mask = _PAGE_USER, + .val = _PAGE_USER, +#endif + .set = "user", + .clear = " ", + }, { + .mask = _PAGE_RW, + .val = _PAGE_RW, + .set = "rw", + .clear = "ro", + }, { + .mask = _PAGE_EXEC, + .val = _PAGE_EXEC, + .set = " X ", + .clear = " ", + }, { + .mask = _PAGE_PTE, + .val = _PAGE_PTE, + .set = "pte", + .clear = " ", + }, { + .mask = _PAGE_PRESENT, + .val = _PAGE_PRESENT, + .set = "present", + .clear = " ", + }, { +#ifdef CONFIG_PPC_STD_MMU_64 + .mask = H_PAGE_HASHPTE, + .val = H_PAGE_HASHPTE, +#else + .mask = _PAGE_HASHPTE, + .val = _PAGE_HASHPTE, +#endif + .set = "hpte", + .clear = " ", + }, { +#ifndef CONFIG_PPC_STD_MMU_64 + .mask = _PAGE_GUARDED, + .val = _PAGE_GUARDED, + .set = "guarded", + .clear = " ", + }, { +#endif + .mask = _PAGE_DIRTY, + .val = _PAGE_DIRTY, + .set = "dirty", + .clear = " ", + }, { + .mask = _PAGE_ACCESSED, + .val = _PAGE_ACCESSED, + .set = "accessed", + .clear = " ", + }, { +#ifndef CONFIG_PPC_STD_MMU_64 + .mask = _PAGE_WRITETHRU, + .val = _PAGE_WRITETHRU, + .set = "write through", + .clear = " ", + }, { +#endif + .mask = _PAGE_NO_CACHE, + .val = _PAGE_NO_CACHE, + .set = "no cache", + .clear = " ", + }, { + .mask = H_PAGE_BUSY, + .val = H_PAGE_BUSY, + .set = "busy", + }, { +#ifdef CONFIG_PPC_64K_PAGES + .mask = H_PAGE_COMBO, + .val = H_PAGE_COMBO, + .set = "combo", + }, { + .mask = H_PAGE_4K_PFN, + .val = H_PAGE_4K_PFN, + .set = "4K_pfn", + }, { +#endif + .mask = H_PAGE_F_GIX, + .val = H_PAGE_F_GIX, + .set = "f_gix", + .is_val = true, + .shift = H_PAGE_F_GIX_SHIFT, + }, { + .mask = H_PAGE_F_SECOND, + .val = H_PAGE_F_SECOND, + .set = "f_second", + }, { + .mask = _PAGE_SPECIAL, + .val = _PAGE_SPECIAL, + .set = "special", + } +}; + +struct pgtable_level { + const struct flag_info *flag; + size_t num; + u64 mask; +}; + +static struct pgtable_level pg_level[] = { + { + }, { /* pgd */ + .flag = flag_array, + .num = ARRAY_SIZE(flag_array), + }, { /* pud */ + .flag = flag_array, + .num = ARRAY_SIZE(flag_array), + }, { /* pmd */ + .flag = flag_array, + .num = ARRAY_SIZE(flag_array), + }, { /* pte */ + .flag = flag_array, + .num = ARRAY_SIZE(flag_array), + }, +}; + +static void dump_flag_info(struct pg_state *st, const struct flag_info + *flag, u64 pte, int num) +{ + unsigned int i; + + for (i = 0; i < num; i++, flag++) { + const char *s = NULL; + u64 val; + + /* flag not defined so don't check it */ + if (flag->mask == 0) + continue; + /* Some 'flags' are actually values */ + if (flag->is_val) { + val = pte & flag->val; + if (flag->shift) + val = val >> flag->shift; + seq_printf(st->seq, " %s:%llx", flag->set, val); + } else { + if ((pte & flag->mask) == flag->val) + s = flag->set; + else + s = flag->clear; + if (s) + seq_printf(st->seq, " %s", s); + } + st->current_flags &= ~flag->mask; + } + if (st->current_flags != 0) + seq_printf(st->seq, " unknown flags:%llx", st->current_flags); +} + +static void dump_addr(struct pg_state *st, unsigned long addr) +{ + static const char units[] = "KMGTPE"; + const char *unit = units; + unsigned long delta; + + seq_printf(st->seq, "0x%016lx-0x%016lx ", st->start_address, addr-1); + delta = (addr - st->start_address) >> 10; + /* Work out what appropriate unit to use */ + while (!(delta & 1023) && unit[1]) { + delta >>= 10; + unit++; + } + seq_printf(st->seq, "%9lu%c", delta, *unit); + +} + +static void note_page(struct pg_state *st, unsigned long addr, + unsigned int level, u64 val) +{ + u64 flag = val & pg_level[level].mask; + /* At first no level is set */ + if (!st->level) { + st->level = level; + st->current_flags = flag; + st->start_address = addr; + seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); + /* + * Dump the section of virtual memory when: + * - the PTE flags from one entry to the next differs. + * - we change levels in the tree. + * - the address is in a different section of memory and is thus + * used for a different purpose, regardless of the flags. + */ + } else if (flag != st->current_flags || level != st->level || + addr >= st->marker[1].start_address) { + + /* Check the PTE flags */ + if (st->current_flags) { + dump_addr(st, addr); + + /* Dump all the flags */ + if (pg_level[st->level].flag) + dump_flag_info(st, pg_level[st->level].flag, + st->current_flags, + pg_level[st->level].num); + + seq_puts(st->seq, "\n"); + } + + /* + * Address indicates we have passed the end of the + * current section of virtual memory + */ + while (addr >= st->marker[1].start_address) { + st->marker++; + seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); + } + st->start_address = addr; + st->current_flags = flag; + st->level = level; + } +} + +static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start) +{ + pte_t *pte = pte_offset_kernel(pmd, 0); + unsigned long addr; + unsigned int i; + + for (i = 0; i < PTRS_PER_PTE; i++, pte++) { + addr = start + i * PAGE_SIZE; + note_page(st, addr, 4, pte_val(*pte)); + + } +} + +static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start) +{ + pmd_t *pmd = pmd_offset(pud, 0); + unsigned long addr; + unsigned int i; + + for (i = 0; i < PTRS_PER_PMD; i++, pmd++) { + addr = start + i * PMD_SIZE; + if (!pmd_none(*pmd)) + /* pmd exists */ + walk_pte(st, pmd, addr); + else + note_page(st, addr, 3, pmd_val(*pmd)); + } +} + +static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start) +{ + pud_t *pud = pud_offset(pgd, 0); + unsigned long addr; + unsigned int i; + + for (i = 0; i < PTRS_PER_PUD; i++, pud++) { + addr = start + i * PUD_SIZE; + if (!pud_none(*pud)) + /* pud exists */ + walk_pmd(st, pud, addr); + else + note_page(st, addr, 2, pud_val(*pud)); + } +} + +static void walk_pagetables(struct pg_state *st) +{ + pgd_t *pgd = pgd_offset_k(0UL); + unsigned int i; + unsigned long addr; + + /* + * Traverse the linux pagetable structure and dump pages that are in + * the hash pagetable. + */ + for (i = 0; i < PTRS_PER_PGD; i++, pgd++) { + addr = KERN_VIRT_START + i * PGDIR_SIZE; + if (!pgd_none(*pgd)) + /* pgd exists */ + walk_pud(st, pgd, addr); + else + note_page(st, addr, 1, pgd_val(*pgd)); + } +} + +static void populate_markers(void) +{ + address_markers[0].start_address = PAGE_OFFSET; + address_markers[1].start_address = VMALLOC_START; + address_markers[2].start_address = VMALLOC_END; + address_markers[3].start_address = ISA_IO_BASE; + address_markers[4].start_address = ISA_IO_END; + address_markers[5].start_address = PHB_IO_BASE; + address_markers[6].start_address = PHB_IO_END; + address_markers[7].start_address = IOREMAP_BASE; + address_markers[8].start_address = IOREMAP_END; +#ifdef CONFIG_PPC_STD_MMU_64 + address_markers[9].start_address = H_VMEMMAP_BASE; +#else + address_markers[9].start_address = VMEMMAP_BASE; +#endif +} + +static int ptdump_show(struct seq_file *m, void *v) +{ + struct pg_state st = { + .seq = m, + .start_address = KERN_VIRT_START, + .marker = address_markers, + }; + /* Traverse kernel page tables */ + walk_pagetables(&st); + note_page(&st, 0, 0, 0); + return 0; +} + + +static int ptdump_open(struct inode *inode, struct file *file) +{ + return single_open(file, ptdump_show, NULL); +} + +static const struct file_operations ptdump_fops = { + .open = ptdump_open, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, +}; + +static void build_pgtable_complete_mask(void) +{ + unsigned int i, j; + + for (i = 0; i < ARRAY_SIZE(pg_level); i++) + if (pg_level[i].flag) + for (j = 0; j < pg_level[i].num; j++) + pg_level[i].mask |= pg_level[i].flag[j].mask; +} + +static int ptdump_init(void) +{ + struct dentry *debugfs_file; + + populate_markers(); + build_pgtable_complete_mask(); + debugfs_file = debugfs_create_file("kernel_pagetables", 0400, NULL, + NULL, &ptdump_fops); + return debugfs_file ? 0 : -ENOMEM; +} +device_initcall(ptdump_init); -- 2.5.0 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* [v3 PATCH 2/2] powerpc/mm: Dump hash table 2016-05-27 5:48 [v3 PATCH 1/2] powerpc/mm: Dump linux pagetables Rashmica Gupta @ 2016-05-27 5:49 ` Rashmica Gupta 2016-11-17 12:04 ` [v3,2/2] " Michael Ellerman 2016-11-18 6:01 ` [v3 PATCH 2/2] " Aneesh Kumar K.V 2016-11-17 12:03 ` [v3,1/2] powerpc/mm: Dump linux pagetables Michael Ellerman 1 sibling, 2 replies; 9+ messages in thread From: Rashmica Gupta @ 2016-05-27 5:49 UTC (permalink / raw) To: linuxppc-dev, mpe Useful to be able to dump the kernel hash page table to check which pages are hashed along with their sizes and other details. Add a debugfs file to check the hash page table. If radix is enabled (and so there is no hash page table) then this file doesn't exist. To use this the PPC_PTDUMP config option must be selected. Signed-off-by: Rashmica Gupta <rashmicy@gmail.com> --- v2 -> v3: Changed to be compatible with P9. arch/powerpc/mm/Makefile | 3 +- arch/powerpc/mm/dump_hashpagetable.c | 543 +++++++++++++++++++++++++++++++++++ 2 files changed, 545 insertions(+), 1 deletion(-) create mode 100644 arch/powerpc/mm/dump_hashpagetable.c diff --git a/arch/powerpc/mm/Makefile b/arch/powerpc/mm/Makefile index d3dfa02082b1..df4fbf56aa7b 100644 --- a/arch/powerpc/mm/Makefile +++ b/arch/powerpc/mm/Makefile @@ -43,4 +43,5 @@ obj-$(CONFIG_NOT_COHERENT_CACHE) += dma-noncoherent.o obj-$(CONFIG_HIGHMEM) += highmem.o obj-$(CONFIG_PPC_COPRO_BASE) += copro_fault.o obj-$(CONFIG_SPAPR_TCE_IOMMU) += mmu_context_iommu.o -obj-$(CONFIG_PPC_PTDUMP) += dump_linuxpagetables.o +obj-$(CONFIG_PPC_PTDUMP) += dump_linuxpagetables.o \ + dump_hashpagetable.o diff --git a/arch/powerpc/mm/dump_hashpagetable.c b/arch/powerpc/mm/dump_hashpagetable.c new file mode 100644 index 000000000000..f8ccc224eb2c --- /dev/null +++ b/arch/powerpc/mm/dump_hashpagetable.c @@ -0,0 +1,543 @@ +/* + * Copyright 2016, Rashmica Gupta, IBM Corp. + * + * This traverses the kernel virtual memory and dumps the pages that are in + * the hash pagetable, along with their flags to + * /sys/kernel/debug/kernel_hash_pagetable. + * + * If radix is enabled then there is no hash page table and so no debugfs file + * is generated. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; version 2 + * of the License. + */ +#include <linux/debugfs.h> +#include <linux/fs.h> +#include <linux/io.h> +#include <linux/mm.h> +#include <linux/sched.h> +#include <linux/seq_file.h> +#include <asm/fixmap.h> +#include <asm/pgtable.h> +#include <linux/const.h> +#include <asm/page.h> +#include <asm/pgalloc.h> +#include <asm/plpar_wrappers.h> +#include <linux/memblock.h> +#include <asm/firmware.h> + +struct pg_state { + struct seq_file *seq; + const struct addr_marker *marker; + unsigned long start_address; + unsigned int level; + u64 current_flags; +}; + +struct addr_marker { + unsigned long start_address; + const char *name; +}; + +static struct addr_marker address_markers[] = { + { 0, "Start of kernel VM" }, + { 0, "vmalloc() Area" }, + { 0, "vmalloc() End" }, + { 0, "isa I/O start" }, + { 0, "isa I/O end" }, + { 0, "phb I/O start" }, + { 0, "phb I/O end" }, + { 0, "I/O remap start" }, + { 0, "I/O remap end" }, + { 0, "vmemmap start" }, + { -1, NULL }, +}; + +struct flag_info { + u64 mask; + u64 val; + const char *set; + const char *clear; + bool is_val; + int shift; +}; + +static const struct flag_info v_flag_array[] = { + { + .mask = SLB_VSID_B, + .val = SLB_VSID_B_256M, + .set = "ssize: 256M", + .clear = "ssize: 1T ", + }, { + .mask = HPTE_V_SECONDARY, + .val = HPTE_V_SECONDARY, + .set = "secondary", + .clear = "primary ", + }, { + .mask = HPTE_V_VALID, + .val = HPTE_V_VALID, + .set = "valid ", + .clear = "invalid", + }, { + .mask = HPTE_V_BOLTED, + .val = HPTE_V_BOLTED, + .set = "bolted", + .clear = "", + } +}; + +static const struct flag_info r_flag_array[] = { + { + .mask = HPTE_R_PP0 | HPTE_R_PP, + .val = PP_RWXX, + .set = "prot:RW--", + }, { + .mask = HPTE_R_PP0 | HPTE_R_PP, + .val = PP_RWRX, + .set = "prot:RWR-", + }, { + .mask = HPTE_R_PP0 | HPTE_R_PP, + .val = PP_RWRW, + .set = "prot:RWRW", + }, { + .mask = HPTE_R_PP0 | HPTE_R_PP, + .val = PP_RXRX, + .set = "prot:R-R-", + }, { + .mask = HPTE_R_PP0 | HPTE_R_PP, + .val = PP_RXXX, + .set = "prot:R---", + }, { + .mask = HPTE_R_KEY_HI | HPTE_R_KEY_LO, + .val = HPTE_R_KEY_HI | HPTE_R_KEY_LO, + .set = "key", + .clear = "", + .is_val = true, + }, { + .mask = HPTE_R_R, + .val = HPTE_R_R, + .set = "ref", + .clear = " ", + }, { + .mask = HPTE_R_C, + .val = HPTE_R_C, + .set = "changed", + .clear = " ", + }, { + .mask = HPTE_R_N, + .val = HPTE_R_N, + .set = "no execute", + }, { + .mask = HPTE_R_WIMG, + .val = HPTE_R_W, + .set = "writethru", + }, { + .mask = HPTE_R_WIMG, + .val = HPTE_R_I, + .set = "no cache", + }, { + .mask = HPTE_R_WIMG, + .val = HPTE_R_G, + .set = "guarded", + } +}; + +static int calculate_pagesize(struct pg_state *st, int ps, char s[]) +{ + static const char units[] = "BKMGTPE"; + const char *unit = units; + + while (ps > 9 && unit[1]) { + ps -= 10; + unit++; + } + seq_printf(st->seq, " %s_ps: %i%c\t", s, 1<<ps, *unit); + return ps; +} + +static void dump_flag_info(struct pg_state *st, const struct flag_info + *flag, u64 pte, int num) +{ + unsigned int i; + + for (i = 0; i < num; i++, flag++) { + const char *s = NULL; + u64 val; + + /* flag not defined so don't check it */ + if (flag->mask == 0) + continue; + /* Some 'flags' are actually values */ + if (flag->is_val) { + val = pte & flag->val; + if (flag->shift) + val = val >> flag->shift; + seq_printf(st->seq, " %s:%llx", flag->set, val); + } else { + if ((pte & flag->mask) == flag->val) + s = flag->set; + else + s = flag->clear; + if (s) + seq_printf(st->seq, " %s", s); + } + } +} + +static void dump_hpte_info(struct pg_state *st, unsigned long ea, u64 v, u64 r, + unsigned long rpn, int bps, int aps, unsigned long lp) +{ + int aps_index; + + while (ea >= st->marker[1].start_address) { + st->marker++; + seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); + } + seq_printf(st->seq, "0x%lx:\t", ea); + seq_printf(st->seq, "AVPN:%llx\t", HPTE_V_AVPN_VAL(v)); + dump_flag_info(st, v_flag_array, v, ARRAY_SIZE(v_flag_array)); + seq_printf(st->seq, " rpn: %lx\t", rpn); + dump_flag_info(st, r_flag_array, r, ARRAY_SIZE(r_flag_array)); + + calculate_pagesize(st, bps, "base"); + aps_index = calculate_pagesize(st, aps, "actual"); + if (aps_index != 2) + seq_printf(st->seq, "LP enc: %lx", lp); + seq_puts(st->seq, "\n"); +} + + +static int native_find(unsigned long ea, int psize, bool primary, u64 *v, u64 + *r) +{ + struct hash_pte *hptep; + unsigned long hash, vsid, vpn, hpte_group, want_v, hpte_v; + int i, ssize = mmu_kernel_ssize; + unsigned long shift = mmu_psize_defs[psize].shift; + + /* calculate hash */ + vsid = get_kernel_vsid(ea, ssize); + vpn = hpt_vpn(ea, vsid, ssize); + hash = hpt_hash(vpn, shift, ssize); + want_v = hpte_encode_avpn(vpn, psize, ssize); + + /* to check in the secondary hash table, we invert the hash */ + if (!primary) + hash = ~hash; + hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP; + for (i = 0; i < HPTES_PER_GROUP; i++) { + hptep = htab_address + hpte_group; + hpte_v = be64_to_cpu(hptep->v); + + if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID)) { + /* HPTE matches */ + *v = be64_to_cpu(hptep->v); + *r = be64_to_cpu(hptep->r); + return 0; + } + ++hpte_group; + } + return -1; +} + +static int pseries_find(unsigned long ea, int psize, bool primary, u64 *v, u64 + *r) +{ + struct hash_pte ptes[4]; + unsigned long vsid, vpn, hash, hpte_group, want_v; + int i, j, ssize = mmu_kernel_ssize; + long lpar_rc = 0; + unsigned long shift = mmu_psize_defs[psize].shift; + + /* calculate hash */ + vsid = get_kernel_vsid(ea, ssize); + vpn = hpt_vpn(ea, vsid, ssize); + hash = hpt_hash(vpn, shift, ssize); + want_v = hpte_encode_avpn(vpn, psize, ssize); + + /* to check in the secondary hash table, we invert the hash */ + if (!primary) + hash = ~hash; + hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; + /* see if we can find an entry in the hpte with this hash */ + for (i = 0; i < HPTES_PER_GROUP; i += 4, hpte_group += 4) { + lpar_rc = plpar_pte_read_4(0, hpte_group, (void *)ptes); + + if (lpar_rc != H_SUCCESS) + continue; + for (j = 0; j < 4; j++) { + if (HPTE_V_COMPARE(ptes[j].v, want_v) && + (ptes[j].v & HPTE_V_VALID)) { + /* HPTE matches */ + *v = ptes[j].v; + *r = ptes[j].r; + return 0; + } + } + } + return -1; +} + +static void decode_r(int bps, unsigned long r, unsigned long *rpn, int *aps, + unsigned long *lp_bits) +{ + struct mmu_psize_def entry; + unsigned long arpn, mask, lp; + int penc = -2, idx = 0, shift; + + /*. + * The LP field has 8 bits. Depending on the actual page size, some of + * these bits are concatenated with the APRN to get the RPN. The rest + * of the bits in the LP field is the LP value and is an encoding for + * the base page size and the actual page size. + * + * - find the mmu entry for our base page size + * - go through all page encodings and use the associated mask to + * find an encoding that matches our encoding in the LP field. + */ + arpn = (r & HPTE_R_RPN) >> HPTE_R_RPN_SHIFT; + lp = arpn & 0xff; + + entry = mmu_psize_defs[bps]; + while (idx < MMU_PAGE_COUNT) { + penc = entry.penc[idx]; + if ((penc != -1) && (mmu_psize_defs[idx].shift)) { + shift = mmu_psize_defs[idx].shift - HPTE_R_RPN_SHIFT; + mask = (0x1 << (shift)) - 1; + if ((lp & mask) == penc) { + *aps = mmu_psize_to_shift(idx); + *lp_bits = lp & mask; + *rpn = arpn >> shift; + return; + } + } + idx++; + } +} + +static unsigned long hpte_find(struct pg_state *st, unsigned long ea, int psize) +{ + unsigned long slot; + u64 v = 0, r = 0; + unsigned long rpn, lp_bits; + int base_psize = 0, actual_psize = 0; + + if (ea <= PAGE_OFFSET) + return -1; + + /* Look in primary table */ + if (firmware_has_feature(FW_FEATURE_LPAR)) + slot = pseries_find(ea, psize, true, &v, &r); + else + slot = native_find(ea, psize, true, &v, &r); + + /* Look in secondary table */ + if (slot == -1) { + if (firmware_has_feature(FW_FEATURE_LPAR)) + slot = pseries_find(ea, psize, false, &v, &r); + else + slot = native_find(ea, psize, false, &v, &r); + } + + /* No entry found */ + if (slot == -1) + return -1; + + /* We found an entry in the hash page table: + * - check that this has the same base page + * - find the actual page size + * - find the RPN + */ + base_psize = mmu_psize_to_shift(psize); + + if ((v & HPTE_V_LARGE) == HPTE_V_LARGE) { + decode_r(psize, r, &rpn, &actual_psize, &lp_bits); + } else { + /* 4K actual page size */ + actual_psize = 12; + rpn = (r & HPTE_R_RPN) >> HPTE_R_RPN_SHIFT; + /* In this case there are no LP bits */ + lp_bits = -1; + } + /* We didn't find a matching encoding, so the PTE we found isn't for + * this address. + */ + if (actual_psize == -1) + return -1; + + dump_hpte_info(st, ea, v, r, rpn, base_psize, actual_psize, lp_bits); + return 0; +} + +static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start) +{ + pte_t *pte = pte_offset_kernel(pmd, 0); + unsigned long addr, pteval, psize; + int i, status; + + for (i = 0; i < PTRS_PER_PTE; i++, pte++) { + addr = start + i * PAGE_SIZE; + pteval = pte_val(*pte); + + if (addr < VMALLOC_END) + psize = mmu_vmalloc_psize; + else + psize = mmu_io_psize; +#ifdef CONFIG_PPC_64K_PAGES + /* check for secret 4K mappings */ + if (((pteval & H_PAGE_COMBO) == H_PAGE_COMBO) || + ((pteval & H_PAGE_4K_PFN) == H_PAGE_4K_PFN)) + psize = mmu_io_psize; +#endif + /* check for hashpte */ + status = hpte_find(st, addr, psize); + + if (((pteval & H_PAGE_HASHPTE) != H_PAGE_HASHPTE) + && (status != -1)) { + /* found a hpte that is not in the linux page tables */ + seq_printf(st->seq, "page probably bolted before linux" + " pagetables were set: addr:%lx, pteval:%lx\n", + addr, pteval); + } + } +} + +static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start) +{ + pmd_t *pmd = pmd_offset(pud, 0); + unsigned long addr; + unsigned int i; + + for (i = 0; i < PTRS_PER_PMD; i++, pmd++) { + addr = start + i * PMD_SIZE; + if (!pmd_none(*pmd)) + /* pmd exists */ + walk_pte(st, pmd, addr); + } +} + +static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start) +{ + pud_t *pud = pud_offset(pgd, 0); + unsigned long addr; + unsigned int i; + + for (i = 0; i < PTRS_PER_PUD; i++, pud++) { + addr = start + i * PUD_SIZE; + if (!pud_none(*pud)) + /* pud exists */ + walk_pmd(st, pud, addr); + } +} + +static void walk_pagetables(struct pg_state *st) +{ + pgd_t *pgd = pgd_offset_k(0UL); + unsigned int i; + unsigned long addr; + + /* + * Traverse the linux pagetable structure and dump pages that are in + * the hash pagetable. + */ + for (i = 0; i < PTRS_PER_PGD; i++, pgd++) { + addr = KERN_VIRT_START + i * PGDIR_SIZE; + if (!pgd_none(*pgd)) + /* pgd exists */ + walk_pud(st, pgd, addr); + } +} + + +static void walk_linearmapping(struct pg_state *st) +{ + unsigned long addr; + + /* + * Traverse the linear mapping section of virtual memory and dump pages + * that are in the hash pagetable. + */ + unsigned long psize = 1 << mmu_psize_defs[mmu_linear_psize].shift; + + for (addr = PAGE_OFFSET; addr < PAGE_OFFSET + + memblock_phys_mem_size(); addr += psize) + hpte_find(st, addr, mmu_linear_psize); +} + +static void walk_vmemmap(struct pg_state *st) +{ + struct vmemmap_backing *ptr = vmemmap_list; + + /* + * Traverse the vmemmaped memory and dump pages that are in the hash + * pagetable. + */ + while (ptr->list) { + hpte_find(st, ptr->virt_addr, mmu_vmemmap_psize); + ptr = ptr->list; + } + seq_puts(st->seq, "---[ vmemmap end ]---\n"); +} + +static void populate_markers(void) +{ + address_markers[0].start_address = PAGE_OFFSET; + address_markers[1].start_address = VMALLOC_START; + address_markers[2].start_address = VMALLOC_END; + address_markers[3].start_address = ISA_IO_BASE; + address_markers[4].start_address = ISA_IO_END; + address_markers[5].start_address = PHB_IO_BASE; + address_markers[6].start_address = PHB_IO_END; + address_markers[7].start_address = IOREMAP_BASE; + address_markers[8].start_address = IOREMAP_END; +#ifdef CONFIG_PPC_STD_MMU_64 + address_markers[9].start_address = H_VMEMMAP_BASE; +#else + address_markers[9].start_address = VMEMMAP_BASE; +#endif +} + +static int ptdump_show(struct seq_file *m, void *v) +{ + struct pg_state st = { + .seq = m, + .start_address = PAGE_OFFSET, + .marker = address_markers, + }; + /* + * Traverse the 0xc, 0xd and 0xf areas of the kernel virtual memory and + * dump pages that are in the hash pagetable. + */ + walk_linearmapping(&st); + walk_pagetables(&st); + walk_vmemmap(&st); + return 0; +} + +static int ptdump_open(struct inode *inode, struct file *file) +{ + return single_open(file, ptdump_show, NULL); +} + +static const struct file_operations ptdump_fops = { + .open = ptdump_open, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, +}; + +static int ptdump_init(void) +{ + struct dentry *debugfs_file; + + if (!radix_enabled()) { + populate_markers(); + debugfs_file = debugfs_create_file("kernel_hash_pagetable", + 0400, NULL, NULL, &ptdump_fops); + return debugfs_file ? 0 : -ENOMEM; + } + return 0; +} +device_initcall(ptdump_init); -- 2.5.0 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [v3,2/2] powerpc/mm: Dump hash table 2016-05-27 5:49 ` [v3 PATCH 2/2] powerpc/mm: Dump hash table Rashmica Gupta @ 2016-11-17 12:04 ` Michael Ellerman 2016-11-18 6:01 ` [v3 PATCH 2/2] " Aneesh Kumar K.V 1 sibling, 0 replies; 9+ messages in thread From: Michael Ellerman @ 2016-11-17 12:04 UTC (permalink / raw) To: Rashmica Gupta, linuxppc-dev On Fri, 2016-27-05 at 05:49:00 UTC, Rashmica Gupta wrote: > Useful to be able to dump the kernel hash page table to check > which pages are hashed along with their sizes and other details. > > Add a debugfs file to check the hash page table. If radix is enabled > (and so there is no hash page table) then this file doesn't exist. To > use this the PPC_PTDUMP config option must be selected. > > Signed-off-by: Rashmica Gupta <rashmicy@gmail.com> Applied to powerpc next, thanks. https://git.kernel.org/powerpc/c/1515ab932156257afd8a5864506dab cheers ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [v3 PATCH 2/2] powerpc/mm: Dump hash table 2016-05-27 5:49 ` [v3 PATCH 2/2] powerpc/mm: Dump hash table Rashmica Gupta 2016-11-17 12:04 ` [v3,2/2] " Michael Ellerman @ 2016-11-18 6:01 ` Aneesh Kumar K.V 1 sibling, 0 replies; 9+ messages in thread From: Aneesh Kumar K.V @ 2016-11-18 6:01 UTC (permalink / raw) To: Rashmica Gupta, linuxppc-dev, mpe Rashmica Gupta <rashmicy@gmail.com> writes: > Useful to be able to dump the kernel hash page table to check > which pages are hashed along with their sizes and other details. > > Add a debugfs file to check the hash page table. If radix is enabled > (and so there is no hash page table) then this file doesn't exist. To > use this the PPC_PTDUMP config option must be selected. > > Signed-off-by: Rashmica Gupta <rashmicy@gmail.com> I completely missed this when it was sent to the list. > --- > v2 -> v3: Changed to be compatible with P9. > > arch/powerpc/mm/Makefile | 3 +- > arch/powerpc/mm/dump_hashpagetable.c | 543 +++++++++++++++++++++++++++++++++++ > 2 files changed, 545 insertions(+), 1 deletion(-) > create mode 100644 arch/powerpc/mm/dump_hashpagetable.c > > diff --git a/arch/powerpc/mm/Makefile b/arch/powerpc/mm/Makefile > index d3dfa02082b1..df4fbf56aa7b 100644 > --- a/arch/powerpc/mm/Makefile > +++ b/arch/powerpc/mm/Makefile > @@ -43,4 +43,5 @@ obj-$(CONFIG_NOT_COHERENT_CACHE) += dma-noncoherent.o > obj-$(CONFIG_HIGHMEM) += highmem.o > obj-$(CONFIG_PPC_COPRO_BASE) += copro_fault.o > obj-$(CONFIG_SPAPR_TCE_IOMMU) += mmu_context_iommu.o > -obj-$(CONFIG_PPC_PTDUMP) += dump_linuxpagetables.o > +obj-$(CONFIG_PPC_PTDUMP) += dump_linuxpagetables.o \ > + dump_hashpagetable.o > diff --git a/arch/powerpc/mm/dump_hashpagetable.c b/arch/powerpc/mm/dump_hashpagetable.c > new file mode 100644 > index 000000000000..f8ccc224eb2c > --- /dev/null > +++ b/arch/powerpc/mm/dump_hashpagetable.c > @@ -0,0 +1,543 @@ > +/* > + * Copyright 2016, Rashmica Gupta, IBM Corp. > + * > + * This traverses the kernel virtual memory and dumps the pages that are in > + * the hash pagetable, along with their flags to > + * /sys/kernel/debug/kernel_hash_pagetable. > + * > + * If radix is enabled then there is no hash page table and so no debugfs file > + * is generated. > + * > + * This program is free software; you can redistribute it and/or > + * modify it under the terms of the GNU General Public License > + * as published by the Free Software Foundation; version 2 > + * of the License. > + */ > +#include <linux/debugfs.h> > +#include <linux/fs.h> > +#include <linux/io.h> > +#include <linux/mm.h> > +#include <linux/sched.h> > +#include <linux/seq_file.h> > +#include <asm/fixmap.h> > +#include <asm/pgtable.h> > +#include <linux/const.h> > +#include <asm/page.h> > +#include <asm/pgalloc.h> > +#include <asm/plpar_wrappers.h> > +#include <linux/memblock.h> > +#include <asm/firmware.h> > + > +struct pg_state { > + struct seq_file *seq; > + const struct addr_marker *marker; > + unsigned long start_address; > + unsigned int level; > + u64 current_flags; > +}; > + > +struct addr_marker { > + unsigned long start_address; > + const char *name; > +}; > + > +static struct addr_marker address_markers[] = { > + { 0, "Start of kernel VM" }, > + { 0, "vmalloc() Area" }, > + { 0, "vmalloc() End" }, > + { 0, "isa I/O start" }, > + { 0, "isa I/O end" }, > + { 0, "phb I/O start" }, > + { 0, "phb I/O end" }, > + { 0, "I/O remap start" }, > + { 0, "I/O remap end" }, > + { 0, "vmemmap start" }, > + { -1, NULL }, > +}; > + > +struct flag_info { > + u64 mask; > + u64 val; > + const char *set; > + const char *clear; > + bool is_val; > + int shift; > +}; > + > +static const struct flag_info v_flag_array[] = { > + { > + .mask = SLB_VSID_B, > + .val = SLB_VSID_B_256M, > + .set = "ssize: 256M", > + .clear = "ssize: 1T ", > + }, { > + .mask = HPTE_V_SECONDARY, > + .val = HPTE_V_SECONDARY, > + .set = "secondary", > + .clear = "primary ", > + }, { > + .mask = HPTE_V_VALID, > + .val = HPTE_V_VALID, > + .set = "valid ", > + .clear = "invalid", > + }, { > + .mask = HPTE_V_BOLTED, > + .val = HPTE_V_BOLTED, > + .set = "bolted", > + .clear = "", > + } > +}; > + > +static const struct flag_info r_flag_array[] = { > + { > + .mask = HPTE_R_PP0 | HPTE_R_PP, > + .val = PP_RWXX, > + .set = "prot:RW--", > + }, { > + .mask = HPTE_R_PP0 | HPTE_R_PP, > + .val = PP_RWRX, > + .set = "prot:RWR-", > + }, { > + .mask = HPTE_R_PP0 | HPTE_R_PP, > + .val = PP_RWRW, > + .set = "prot:RWRW", > + }, { > + .mask = HPTE_R_PP0 | HPTE_R_PP, > + .val = PP_RXRX, > + .set = "prot:R-R-", > + }, { > + .mask = HPTE_R_PP0 | HPTE_R_PP, > + .val = PP_RXXX, > + .set = "prot:R---", > + }, { > + .mask = HPTE_R_KEY_HI | HPTE_R_KEY_LO, > + .val = HPTE_R_KEY_HI | HPTE_R_KEY_LO, > + .set = "key", > + .clear = "", > + .is_val = true, > + }, { > + .mask = HPTE_R_R, > + .val = HPTE_R_R, > + .set = "ref", > + .clear = " ", > + }, { > + .mask = HPTE_R_C, > + .val = HPTE_R_C, > + .set = "changed", > + .clear = " ", > + }, { > + .mask = HPTE_R_N, > + .val = HPTE_R_N, > + .set = "no execute", > + }, { > + .mask = HPTE_R_WIMG, > + .val = HPTE_R_W, > + .set = "writethru", > + }, { > + .mask = HPTE_R_WIMG, > + .val = HPTE_R_I, > + .set = "no cache", > + }, { > + .mask = HPTE_R_WIMG, > + .val = HPTE_R_G, > + .set = "guarded", > + } > +}; > + > +static int calculate_pagesize(struct pg_state *st, int ps, char s[]) > +{ > + static const char units[] = "BKMGTPE"; > + const char *unit = units; > + > + while (ps > 9 && unit[1]) { > + ps -= 10; > + unit++; > + } > + seq_printf(st->seq, " %s_ps: %i%c\t", s, 1<<ps, *unit); > + return ps; > +} > + > +static void dump_flag_info(struct pg_state *st, const struct flag_info > + *flag, u64 pte, int num) > +{ > + unsigned int i; > + > + for (i = 0; i < num; i++, flag++) { > + const char *s = NULL; > + u64 val; > + > + /* flag not defined so don't check it */ > + if (flag->mask == 0) > + continue; > + /* Some 'flags' are actually values */ > + if (flag->is_val) { > + val = pte & flag->val; > + if (flag->shift) > + val = val >> flag->shift; > + seq_printf(st->seq, " %s:%llx", flag->set, val); > + } else { > + if ((pte & flag->mask) == flag->val) > + s = flag->set; > + else > + s = flag->clear; > + if (s) > + seq_printf(st->seq, " %s", s); > + } > + } > +} > + > +static void dump_hpte_info(struct pg_state *st, unsigned long ea, u64 v, u64 r, > + unsigned long rpn, int bps, int aps, unsigned long lp) > +{ > + int aps_index; > + > + while (ea >= st->marker[1].start_address) { > + st->marker++; > + seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); > + } > + seq_printf(st->seq, "0x%lx:\t", ea); > + seq_printf(st->seq, "AVPN:%llx\t", HPTE_V_AVPN_VAL(v)); > + dump_flag_info(st, v_flag_array, v, ARRAY_SIZE(v_flag_array)); > + seq_printf(st->seq, " rpn: %lx\t", rpn); > + dump_flag_info(st, r_flag_array, r, ARRAY_SIZE(r_flag_array)); > + > + calculate_pagesize(st, bps, "base"); > + aps_index = calculate_pagesize(st, aps, "actual"); > + if (aps_index != 2) > + seq_printf(st->seq, "LP enc: %lx", lp); > + seq_puts(st->seq, "\n"); > +} > + > + > +static int native_find(unsigned long ea, int psize, bool primary, u64 *v, u64 > + *r) > +{ > + struct hash_pte *hptep; > + unsigned long hash, vsid, vpn, hpte_group, want_v, hpte_v; > + int i, ssize = mmu_kernel_ssize; > + unsigned long shift = mmu_psize_defs[psize].shift; > + > + /* calculate hash */ > + vsid = get_kernel_vsid(ea, ssize); > + vpn = hpt_vpn(ea, vsid, ssize); > + hash = hpt_hash(vpn, shift, ssize); > + want_v = hpte_encode_avpn(vpn, psize, ssize); > + > + /* to check in the secondary hash table, we invert the hash */ > + if (!primary) > + hash = ~hash; > + hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP; > + for (i = 0; i < HPTES_PER_GROUP; i++) { > + hptep = htab_address + hpte_group; > + hpte_v = be64_to_cpu(hptep->v); > + > + if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID)) { > + /* HPTE matches */ > + *v = be64_to_cpu(hptep->v); > + *r = be64_to_cpu(hptep->r); > + return 0; > + } > + ++hpte_group; > + } > + return -1; > +} > + > +static int pseries_find(unsigned long ea, int psize, bool primary, u64 *v, u64 > + *r) > +{ > + struct hash_pte ptes[4]; > + unsigned long vsid, vpn, hash, hpte_group, want_v; > + int i, j, ssize = mmu_kernel_ssize; > + long lpar_rc = 0; > + unsigned long shift = mmu_psize_defs[psize].shift; > + > + /* calculate hash */ > + vsid = get_kernel_vsid(ea, ssize); > + vpn = hpt_vpn(ea, vsid, ssize); > + hash = hpt_hash(vpn, shift, ssize); > + want_v = hpte_encode_avpn(vpn, psize, ssize); > + > + /* to check in the secondary hash table, we invert the hash */ > + if (!primary) > + hash = ~hash; > + hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; > + /* see if we can find an entry in the hpte with this hash */ > + for (i = 0; i < HPTES_PER_GROUP; i += 4, hpte_group += 4) { > + lpar_rc = plpar_pte_read_4(0, hpte_group, (void *)ptes); > + > + if (lpar_rc != H_SUCCESS) > + continue; > + for (j = 0; j < 4; j++) { > + if (HPTE_V_COMPARE(ptes[j].v, want_v) && > + (ptes[j].v & HPTE_V_VALID)) { > + /* HPTE matches */ > + *v = ptes[j].v; > + *r = ptes[j].r; > + return 0; > + } > + } > + } > + return -1; > +} > + > +static void decode_r(int bps, unsigned long r, unsigned long *rpn, int *aps, > + unsigned long *lp_bits) > +{ > + struct mmu_psize_def entry; > + unsigned long arpn, mask, lp; > + int penc = -2, idx = 0, shift; > + > + /*. > + * The LP field has 8 bits. Depending on the actual page size, some of > + * these bits are concatenated with the APRN to get the RPN. The rest > + * of the bits in the LP field is the LP value and is an encoding for > + * the base page size and the actual page size. > + * > + * - find the mmu entry for our base page size > + * - go through all page encodings and use the associated mask to > + * find an encoding that matches our encoding in the LP field. > + */ > + arpn = (r & HPTE_R_RPN) >> HPTE_R_RPN_SHIFT; > + lp = arpn & 0xff; > + > + entry = mmu_psize_defs[bps]; > + while (idx < MMU_PAGE_COUNT) { > + penc = entry.penc[idx]; > + if ((penc != -1) && (mmu_psize_defs[idx].shift)) { > + shift = mmu_psize_defs[idx].shift - HPTE_R_RPN_SHIFT; > + mask = (0x1 << (shift)) - 1; > + if ((lp & mask) == penc) { > + *aps = mmu_psize_to_shift(idx); > + *lp_bits = lp & mask; > + *rpn = arpn >> shift; > + return; > + } > + } > + idx++; > + } > +} > + > +static unsigned long hpte_find(struct pg_state *st, unsigned long ea, int psize) > +{ > + unsigned long slot; > + u64 v = 0, r = 0; > + unsigned long rpn, lp_bits; > + int base_psize = 0, actual_psize = 0; > + > + if (ea <= PAGE_OFFSET) > + return -1; > + > + /* Look in primary table */ > + if (firmware_has_feature(FW_FEATURE_LPAR)) > + slot = pseries_find(ea, psize, true, &v, &r); > + else > + slot = native_find(ea, psize, true, &v, &r); > + > + /* Look in secondary table */ > + if (slot == -1) { > + if (firmware_has_feature(FW_FEATURE_LPAR)) > + slot = pseries_find(ea, psize, false, &v, &r); > + else > + slot = native_find(ea, psize, false, &v, &r); > + } > + > + /* No entry found */ > + if (slot == -1) > + return -1; > + > + /* We found an entry in the hash page table: > + * - check that this has the same base page > + * - find the actual page size > + * - find the RPN > + */ > + base_psize = mmu_psize_to_shift(psize); > + > + if ((v & HPTE_V_LARGE) == HPTE_V_LARGE) { > + decode_r(psize, r, &rpn, &actual_psize, &lp_bits); > + } else { > + /* 4K actual page size */ > + actual_psize = 12; > + rpn = (r & HPTE_R_RPN) >> HPTE_R_RPN_SHIFT; > + /* In this case there are no LP bits */ > + lp_bits = -1; > + } > + /* We didn't find a matching encoding, so the PTE we found isn't for > + * this address. > + */ > + if (actual_psize == -1) > + return -1; > + > + dump_hpte_info(st, ea, v, r, rpn, base_psize, actual_psize, lp_bits); > + return 0; > +} > + > +static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start) > +{ > + pte_t *pte = pte_offset_kernel(pmd, 0); > + unsigned long addr, pteval, psize; > + int i, status; > + > + for (i = 0; i < PTRS_PER_PTE; i++, pte++) { > + addr = start + i * PAGE_SIZE; > + pteval = pte_val(*pte); > + > + if (addr < VMALLOC_END) > + psize = mmu_vmalloc_psize; > + else > + psize = mmu_io_psize; > +#ifdef CONFIG_PPC_64K_PAGES > + /* check for secret 4K mappings */ > + if (((pteval & H_PAGE_COMBO) == H_PAGE_COMBO) || > + ((pteval & H_PAGE_4K_PFN) == H_PAGE_4K_PFN)) > + psize = mmu_io_psize; > +#endif > + /* check for hashpte */ > + status = hpte_find(st, addr, psize); > + > + if (((pteval & H_PAGE_HASHPTE) != H_PAGE_HASHPTE) > + && (status != -1)) { > + /* found a hpte that is not in the linux page tables */ > + seq_printf(st->seq, "page probably bolted before linux" > + " pagetables were set: addr:%lx, pteval:%lx\n", > + addr, pteval); > + } > + } > +} > + > +static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start) > +{ > + pmd_t *pmd = pmd_offset(pud, 0); > + unsigned long addr; > + unsigned int i; > + > + for (i = 0; i < PTRS_PER_PMD; i++, pmd++) { > + addr = start + i * PMD_SIZE; > + if (!pmd_none(*pmd)) > + /* pmd exists */ > + walk_pte(st, pmd, addr); Is that correct ? we need to handle bad_pmd, hugetlb entries, THP entries etc right ? Also while walking we also need to make sure it is not getting split. > + } > +} > + > +static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start) > +{ > + pud_t *pud = pud_offset(pgd, 0); > + unsigned long addr; > + unsigned int i; > + > + for (i = 0; i < PTRS_PER_PUD; i++, pud++) { > + addr = start + i * PUD_SIZE; > + if (!pud_none(*pud)) > + /* pud exists */ > + walk_pmd(st, pud, addr); > + } > +} > + > +static void walk_pagetables(struct pg_state *st) > +{ > + pgd_t *pgd = pgd_offset_k(0UL); > + unsigned int i; > + unsigned long addr; > + > + /* > + * Traverse the linux pagetable structure and dump pages that are in > + * the hash pagetable. > + */ > + for (i = 0; i < PTRS_PER_PGD; i++, pgd++) { > + addr = KERN_VIRT_START + i * PGDIR_SIZE; > + if (!pgd_none(*pgd)) > + /* pgd exists */ > + walk_pud(st, pgd, addr); > + } > +} > + > + > +static void walk_linearmapping(struct pg_state *st) > +{ > + unsigned long addr; > + > + /* > + * Traverse the linear mapping section of virtual memory and dump pages > + * that are in the hash pagetable. > + */ > + unsigned long psize = 1 << mmu_psize_defs[mmu_linear_psize].shift; > + > + for (addr = PAGE_OFFSET; addr < PAGE_OFFSET + > + memblock_phys_mem_size(); addr += psize) > + hpte_find(st, addr, mmu_linear_psize); > +} > + > +static void walk_vmemmap(struct pg_state *st) > +{ > + struct vmemmap_backing *ptr = vmemmap_list; > + > + /* > + * Traverse the vmemmaped memory and dump pages that are in the hash > + * pagetable. > + */ > + while (ptr->list) { > + hpte_find(st, ptr->virt_addr, mmu_vmemmap_psize); > + ptr = ptr->list; > + } > + seq_puts(st->seq, "---[ vmemmap end ]---\n"); > +} > + > +static void populate_markers(void) > +{ > + address_markers[0].start_address = PAGE_OFFSET; > + address_markers[1].start_address = VMALLOC_START; > + address_markers[2].start_address = VMALLOC_END; > + address_markers[3].start_address = ISA_IO_BASE; > + address_markers[4].start_address = ISA_IO_END; > + address_markers[5].start_address = PHB_IO_BASE; > + address_markers[6].start_address = PHB_IO_END; > + address_markers[7].start_address = IOREMAP_BASE; > + address_markers[8].start_address = IOREMAP_END; > +#ifdef CONFIG_PPC_STD_MMU_64 > + address_markers[9].start_address = H_VMEMMAP_BASE; > +#else > + address_markers[9].start_address = VMEMMAP_BASE; > +#endif > +} > + > +static int ptdump_show(struct seq_file *m, void *v) > +{ > + struct pg_state st = { > + .seq = m, > + .start_address = PAGE_OFFSET, > + .marker = address_markers, > + }; > + /* > + * Traverse the 0xc, 0xd and 0xf areas of the kernel virtual memory and > + * dump pages that are in the hash pagetable. > + */ > + walk_linearmapping(&st); > + walk_pagetables(&st); > + walk_vmemmap(&st); > + return 0; > +} > + > +static int ptdump_open(struct inode *inode, struct file *file) > +{ > + return single_open(file, ptdump_show, NULL); > +} > + > +static const struct file_operations ptdump_fops = { > + .open = ptdump_open, > + .read = seq_read, > + .llseek = seq_lseek, > + .release = single_release, > +}; > + > +static int ptdump_init(void) > +{ > + struct dentry *debugfs_file; > + > + if (!radix_enabled()) { > + populate_markers(); > + debugfs_file = debugfs_create_file("kernel_hash_pagetable", > + 0400, NULL, NULL, &ptdump_fops); > + return debugfs_file ? 0 : -ENOMEM; > + } > + return 0; > +} > +device_initcall(ptdump_init); > -- > 2.5.0 > > _______________________________________________ > Linuxppc-dev mailing list > Linuxppc-dev@lists.ozlabs.org > https://lists.ozlabs.org/listinfo/linuxppc-dev ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [v3,1/2] powerpc/mm: Dump linux pagetables 2016-05-27 5:48 [v3 PATCH 1/2] powerpc/mm: Dump linux pagetables Rashmica Gupta 2016-05-27 5:49 ` [v3 PATCH 2/2] powerpc/mm: Dump hash table Rashmica Gupta @ 2016-11-17 12:03 ` Michael Ellerman 2017-04-12 16:40 ` Christophe LEROY 1 sibling, 1 reply; 9+ messages in thread From: Michael Ellerman @ 2016-11-17 12:03 UTC (permalink / raw) To: Rashmica Gupta, linuxppc-dev On Fri, 2016-27-05 at 05:48:59 UTC, Rashmica Gupta wrote: > Useful to be able to dump the kernels page tables to check permissions > and memory types - derived from arm64's implementation. > > Add a debugfs file to check the page tables. To use this the PPC_PTDUMP > config option must be selected. > > Signed-off-by: Rashmica Gupta <rashmicy@gmail.com> Applied to powerpc next, thanks. https://git.kernel.org/powerpc/c/8eb07b187000d48152c4ef97f075bd cheers ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [v3,1/2] powerpc/mm: Dump linux pagetables 2016-11-17 12:03 ` [v3,1/2] powerpc/mm: Dump linux pagetables Michael Ellerman @ 2017-04-12 16:40 ` Christophe LEROY 2017-04-13 11:31 ` Michael Ellerman 0 siblings, 1 reply; 9+ messages in thread From: Christophe LEROY @ 2017-04-12 16:40 UTC (permalink / raw) To: Michael Ellerman, Rashmica Gupta; +Cc: linuxppc-dev Hi Rashmica, Le 17/11/2016 à 13:03, Michael Ellerman a écrit : > On Fri, 2016-27-05 at 05:48:59 UTC, Rashmica Gupta wrote: >> Useful to be able to dump the kernels page tables to check permissions >> and memory types - derived from arm64's implementation. >> >> Add a debugfs file to check the page tables. To use this the PPC_PTDUMP >> config option must be selected. >> >> Signed-off-by: Rashmica Gupta <rashmicy@gmail.com> > > Applied to powerpc next, thanks. > > https://git.kernel.org/powerpc/c/8eb07b187000d48152c4ef97f075bd For your information, compilation fails on mpc885_ads_defconfig when we activate PPC_PTDUMP: CC arch/powerpc/mm/dump_linuxpagetables.o arch/powerpc/mm/dump_linuxpagetables.c: In function 'walk_pagetables': arch/powerpc/mm/dump_linuxpagetables.c:369:10: error: 'KERN_VIRT_START' undeclared (first use in this function) arch/powerpc/mm/dump_linuxpagetables.c:369:10: note: each undeclared identifier is reported only once for each function it appears in arch/powerpc/mm/dump_linuxpagetables.c: In function 'populate_markers': arch/powerpc/mm/dump_linuxpagetables.c:383:37: error: 'ISA_IO_BASE' undeclared (first use in this function) arch/powerpc/mm/dump_linuxpagetables.c:384:37: error: 'ISA_IO_END' undeclared (first use in this function) arch/powerpc/mm/dump_linuxpagetables.c:385:37: error: 'PHB_IO_BASE' undeclared (first use in this function) arch/powerpc/mm/dump_linuxpagetables.c:386:37: error: 'PHB_IO_END' undeclared (first use in this function) arch/powerpc/mm/dump_linuxpagetables.c:387:37: error: 'IOREMAP_BASE' undeclared (first use in this function) arch/powerpc/mm/dump_linuxpagetables.c:388:37: error: 'IOREMAP_END' undeclared (first use in this function) arch/powerpc/mm/dump_linuxpagetables.c:392:38: error: 'VMEMMAP_BASE' undeclared (first use in this function) arch/powerpc/mm/dump_linuxpagetables.c: In function 'ptdump_show': arch/powerpc/mm/dump_linuxpagetables.c:400:20: error: 'KERN_VIRT_START' undeclared (first use in this function) make[1]: *** [arch/powerpc/mm/dump_linuxpagetables.o] Error 1 make: *** [arch/powerpc/mm] Error 2 Christophe ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [v3,1/2] powerpc/mm: Dump linux pagetables 2017-04-12 16:40 ` Christophe LEROY @ 2017-04-13 11:31 ` Michael Ellerman 2017-04-13 12:51 ` Christophe LEROY 0 siblings, 1 reply; 9+ messages in thread From: Michael Ellerman @ 2017-04-13 11:31 UTC (permalink / raw) To: Christophe LEROY, Michael Ellerman, Rashmica Gupta; +Cc: linuxppc-dev Christophe LEROY <christophe.leroy@c-s.fr> writes: > Hi Rashmica, > > Le 17/11/2016 =C3=A0 13:03, Michael Ellerman a =C3=A9crit : >> On Fri, 2016-27-05 at 05:48:59 UTC, Rashmica Gupta wrote: >>> Useful to be able to dump the kernels page tables to check permissions >>> and memory types - derived from arm64's implementation. >>> >>> Add a debugfs file to check the page tables. To use this the PPC_PTDUMP >>> config option must be selected. >>> >>> Signed-off-by: Rashmica Gupta <rashmicy@gmail.com> >> >> Applied to powerpc next, thanks. >> >> https://git.kernel.org/powerpc/c/8eb07b187000d48152c4ef97f075bd > > For your information, compilation fails on mpc885_ads_defconfig when we=20 > activate PPC_PTDUMP: > > CC arch/powerpc/mm/dump_linuxpagetables.o > arch/powerpc/mm/dump_linuxpagetables.c: In function 'walk_pagetables': > arch/powerpc/mm/dump_linuxpagetables.c:369:10: error: 'KERN_VIRT_START'=20 > undeclared (first use in this function) Yeah it's basically 64-bit only at the moment. I have a TODO for it: https://github.com/linuxppc/linux/issues/88 But I haven't got around to doing it, maybe someone who has more interest in 32-bit will have time before me ;) cheers ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [v3,1/2] powerpc/mm: Dump linux pagetables 2017-04-13 11:31 ` Michael Ellerman @ 2017-04-13 12:51 ` Christophe LEROY 2017-04-13 13:15 ` Michael Ellerman 0 siblings, 1 reply; 9+ messages in thread From: Christophe LEROY @ 2017-04-13 12:51 UTC (permalink / raw) To: Michael Ellerman, Michael Ellerman, Rashmica Gupta; +Cc: linuxppc-dev Le 13/04/2017 à 13:31, Michael Ellerman a écrit : > Christophe LEROY <christophe.leroy@c-s.fr> writes: > >> Hi Rashmica, >> >> Le 17/11/2016 à 13:03, Michael Ellerman a écrit : >>> On Fri, 2016-27-05 at 05:48:59 UTC, Rashmica Gupta wrote: >>>> Useful to be able to dump the kernels page tables to check permissions >>>> and memory types - derived from arm64's implementation. >>>> >>>> Add a debugfs file to check the page tables. To use this the PPC_PTDUMP >>>> config option must be selected. >>>> >>>> Signed-off-by: Rashmica Gupta <rashmicy@gmail.com> >>> >>> Applied to powerpc next, thanks. >>> >>> https://git.kernel.org/powerpc/c/8eb07b187000d48152c4ef97f075bd >> >> For your information, compilation fails on mpc885_ads_defconfig when we >> activate PPC_PTDUMP: >> >> CC arch/powerpc/mm/dump_linuxpagetables.o >> arch/powerpc/mm/dump_linuxpagetables.c: In function 'walk_pagetables': >> arch/powerpc/mm/dump_linuxpagetables.c:369:10: error: 'KERN_VIRT_START' >> undeclared (first use in this function) > > Yeah it's basically 64-bit only at the moment. > > I have a TODO for it: https://github.com/linuxppc/linux/issues/88 > > But I haven't got around to doing it, maybe someone who has more > interest in 32-bit will have time before me ;) Well, I just did it a few minutes ago. Christophe ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [v3,1/2] powerpc/mm: Dump linux pagetables 2017-04-13 12:51 ` Christophe LEROY @ 2017-04-13 13:15 ` Michael Ellerman 0 siblings, 0 replies; 9+ messages in thread From: Michael Ellerman @ 2017-04-13 13:15 UTC (permalink / raw) To: Christophe LEROY, Michael Ellerman, Rashmica Gupta; +Cc: linuxppc-dev Christophe LEROY <christophe.leroy@c-s.fr> writes: > Le 13/04/2017 =C3=A0 13:31, Michael Ellerman a =C3=A9crit : >> Christophe LEROY <christophe.leroy@c-s.fr> writes: >>> Le 17/11/2016 =C3=A0 13:03, Michael Ellerman a =C3=A9crit : >>>> On Fri, 2016-27-05 at 05:48:59 UTC, Rashmica Gupta wrote: >>>>> Useful to be able to dump the kernels page tables to check permissions >>>>> and memory types - derived from arm64's implementation. >>>>> >>>>> Add a debugfs file to check the page tables. To use this the PPC_PTDU= MP >>>>> config option must be selected. >>>>> >>>>> Signed-off-by: Rashmica Gupta <rashmicy@gmail.com> >>>> >>>> Applied to powerpc next, thanks. >>>> >>>> https://git.kernel.org/powerpc/c/8eb07b187000d48152c4ef97f075bd >>> >>> For your information, compilation fails on mpc885_ads_defconfig when we >>> activate PPC_PTDUMP: >>> >>> CC arch/powerpc/mm/dump_linuxpagetables.o >>> arch/powerpc/mm/dump_linuxpagetables.c: In function 'walk_pagetables': >>> arch/powerpc/mm/dump_linuxpagetables.c:369:10: error: 'KERN_VIRT_START' >>> undeclared (first use in this function) >> >> Yeah it's basically 64-bit only at the moment. >> >> I have a TODO for it: https://github.com/linuxppc/linux/issues/88 >> >> But I haven't got around to doing it, maybe someone who has more >> interest in 32-bit will have time before me ;) > > Well, I just did it a few minutes ago. It's like magic! :) cheers ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2017-04-13 13:15 UTC | newest] Thread overview: 9+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2016-05-27 5:48 [v3 PATCH 1/2] powerpc/mm: Dump linux pagetables Rashmica Gupta 2016-05-27 5:49 ` [v3 PATCH 2/2] powerpc/mm: Dump hash table Rashmica Gupta 2016-11-17 12:04 ` [v3,2/2] " Michael Ellerman 2016-11-18 6:01 ` [v3 PATCH 2/2] " Aneesh Kumar K.V 2016-11-17 12:03 ` [v3,1/2] powerpc/mm: Dump linux pagetables Michael Ellerman 2017-04-12 16:40 ` Christophe LEROY 2017-04-13 11:31 ` Michael Ellerman 2017-04-13 12:51 ` Christophe LEROY 2017-04-13 13:15 ` Michael Ellerman
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).