[v3,07/10] arm64: ptdump: Parse the host stage-2 page-tables from the snapshot

Message ID 20231115171639.2852644-9-sebastianene@google.com
State New
Headers
Series arm64: ptdump: View the second stage page-tables |

Commit Message

Sebastian Ene Nov. 15, 2023, 5:16 p.m. UTC
  Add a walker function which configures ptdump to parse the page-tables
from the snapshot. Define the attributes used by the stage-2 parser and
build a description of an array which holds the name of the level.
Walk the entire address space configured in the pagetable and parse the
attribute descriptors.

Signed-off-by: Sebastian Ene <sebastianene@google.com>
---
 arch/arm64/mm/ptdump.c | 157 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 157 insertions(+)
  

Comments

kernel test robot Nov. 15, 2023, 9:57 p.m. UTC | #1
Hi Sebastian,

kernel test robot noticed the following build warnings:

[auto build test WARNING on arm64/for-next/core]
[also build test WARNING on kvmarm/next linus/master v6.7-rc1 next-20231115]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Sebastian-Ene/KVM-arm64-Add-snap-shooting-the-host-stage-2-pagetables/20231116-012017
base:   https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-next/core
patch link:    https://lore.kernel.org/r/20231115171639.2852644-9-sebastianene%40google.com
patch subject: [PATCH v3 07/10] arm64: ptdump: Parse the host stage-2 page-tables from the snapshot
config: arm64-randconfig-001-20231116 (https://download.01.org/0day-ci/archive/20231116/202311160522.Nr71iglr-lkp@intel.com/config)
compiler: aarch64-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231116/202311160522.Nr71iglr-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202311160522.Nr71iglr-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> arch/arm64/mm/ptdump.c:178:31: warning: 'stage2_pte_bits' defined but not used [-Wunused-const-variable=]
     178 | static const struct prot_bits stage2_pte_bits[] = {
         |                               ^~~~~~~~~~~~~~~


vim +/stage2_pte_bits +178 arch/arm64/mm/ptdump.c

   177	
 > 178	static const struct prot_bits stage2_pte_bits[] = {
   179		{
   180			.mask	= PTE_VALID,
   181			.val	= PTE_VALID,
   182			.set	= " ",
   183			.clear	= "F",
   184		}, {
   185			.mask	= KVM_PTE_LEAF_ATTR_HI_S2_XN,
   186			.val	= KVM_PTE_LEAF_ATTR_HI_S2_XN,
   187			.set	= "XN",
   188			.clear	= "  ",
   189		}, {
   190			.mask	= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R,
   191			.val	= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R,
   192			.set	= "R",
   193			.clear	= " ",
   194		}, {
   195			.mask	= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W,
   196			.val	= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W,
   197			.set	= "W",
   198			.clear	= " ",
   199		}, {
   200			.mask	= KVM_PTE_LEAF_ATTR_LO_S2_AF,
   201			.val	= KVM_PTE_LEAF_ATTR_LO_S2_AF,
   202			.set	= "AF",
   203			.clear	= "  ",
   204		}, {
   205			.mask	= PTE_NG,
   206			.val	= PTE_NG,
   207			.set	= "FnXS",
   208			.clear	= "  ",
   209		}, {
   210			.mask	= PTE_CONT,
   211			.val	= PTE_CONT,
   212			.set	= "CON",
   213			.clear	= "   ",
   214		}, {
   215			.mask	= PTE_TABLE_BIT,
   216			.val	= PTE_TABLE_BIT,
   217			.set	= "   ",
   218			.clear	= "BLK",
   219		}, {
   220			.mask	= KVM_PGTABLE_PROT_SW0,
   221			.val	= KVM_PGTABLE_PROT_SW0,
   222			.set	= "SW0", /* PKVM_PAGE_SHARED_OWNED */
   223		}, {
   224			.mask   = KVM_PGTABLE_PROT_SW1,
   225			.val	= KVM_PGTABLE_PROT_SW1,
   226			.set	= "SW1", /* PKVM_PAGE_SHARED_BORROWED */
   227		}, {
   228			.mask	= KVM_PGTABLE_PROT_SW2,
   229			.val	= KVM_PGTABLE_PROT_SW2,
   230			.set	= "SW2",
   231		}, {
   232			.mask   = KVM_PGTABLE_PROT_SW3,
   233			.val	= KVM_PGTABLE_PROT_SW3,
   234			.set	= "SW3",
   235		},
   236	};
   237
  
kernel test robot Nov. 18, 2023, 10:39 p.m. UTC | #2
Hi Sebastian,

kernel test robot noticed the following build warnings:

[auto build test WARNING on arm64/for-next/core]
[also build test WARNING on kvmarm/next linus/master v6.7-rc1 next-20231117]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Sebastian-Ene/KVM-arm64-Add-snap-shooting-the-host-stage-2-pagetables/20231116-012017
base:   https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-next/core
patch link:    https://lore.kernel.org/r/20231115171639.2852644-9-sebastianene%40google.com
patch subject: [PATCH v3 07/10] arm64: ptdump: Parse the host stage-2 page-tables from the snapshot
config: arm64-randconfig-r071-20231119 (https://download.01.org/0day-ci/archive/20231119/202311190602.TfLBzLiV-lkp@intel.com/config)
compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project.git 4a5ac14ee968ff0ad5d2cc1ffa0299048db4c88a)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231119/202311190602.TfLBzLiV-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202311190602.TfLBzLiV-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> arch/arm64/mm/ptdump.c:178:31: warning: unused variable 'stage2_pte_bits' [-Wunused-const-variable]
     178 | static const struct prot_bits stage2_pte_bits[] = {
         |                               ^
   1 warning generated.


vim +/stage2_pte_bits +178 arch/arm64/mm/ptdump.c

   177	
 > 178	static const struct prot_bits stage2_pte_bits[] = {
   179		{
   180			.mask	= PTE_VALID,
   181			.val	= PTE_VALID,
   182			.set	= " ",
   183			.clear	= "F",
   184		}, {
   185			.mask	= KVM_PTE_LEAF_ATTR_HI_S2_XN,
   186			.val	= KVM_PTE_LEAF_ATTR_HI_S2_XN,
   187			.set	= "XN",
   188			.clear	= "  ",
   189		}, {
   190			.mask	= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R,
   191			.val	= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R,
   192			.set	= "R",
   193			.clear	= " ",
   194		}, {
   195			.mask	= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W,
   196			.val	= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W,
   197			.set	= "W",
   198			.clear	= " ",
   199		}, {
   200			.mask	= KVM_PTE_LEAF_ATTR_LO_S2_AF,
   201			.val	= KVM_PTE_LEAF_ATTR_LO_S2_AF,
   202			.set	= "AF",
   203			.clear	= "  ",
   204		}, {
   205			.mask	= PTE_NG,
   206			.val	= PTE_NG,
   207			.set	= "FnXS",
   208			.clear	= "  ",
   209		}, {
   210			.mask	= PTE_CONT,
   211			.val	= PTE_CONT,
   212			.set	= "CON",
   213			.clear	= "   ",
   214		}, {
   215			.mask	= PTE_TABLE_BIT,
   216			.val	= PTE_TABLE_BIT,
   217			.set	= "   ",
   218			.clear	= "BLK",
   219		}, {
   220			.mask	= KVM_PGTABLE_PROT_SW0,
   221			.val	= KVM_PGTABLE_PROT_SW0,
   222			.set	= "SW0", /* PKVM_PAGE_SHARED_OWNED */
   223		}, {
   224			.mask   = KVM_PGTABLE_PROT_SW1,
   225			.val	= KVM_PGTABLE_PROT_SW1,
   226			.set	= "SW1", /* PKVM_PAGE_SHARED_BORROWED */
   227		}, {
   228			.mask	= KVM_PGTABLE_PROT_SW2,
   229			.val	= KVM_PGTABLE_PROT_SW2,
   230			.set	= "SW2",
   231		}, {
   232			.mask   = KVM_PGTABLE_PROT_SW3,
   233			.val	= KVM_PGTABLE_PROT_SW3,
   234			.set	= "SW3",
   235		},
   236	};
   237
  

Patch

diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c
index 0b4cb54e43ff..9f88542d5312 100644
--- a/arch/arm64/mm/ptdump.c
+++ b/arch/arm64/mm/ptdump.c
@@ -86,6 +86,7 @@  struct pg_state {
 	bool check_wx;
 	unsigned long wx_pages;
 	unsigned long uxn_pages;
+	struct ptdump_info_file_priv *f_priv;
 };
 
 struct prot_bits {
@@ -174,6 +175,66 @@  static const struct prot_bits pte_bits[] = {
 	}
 };
 
+static const struct prot_bits stage2_pte_bits[] = {
+	{
+		.mask	= PTE_VALID,
+		.val	= PTE_VALID,
+		.set	= " ",
+		.clear	= "F",
+	}, {
+		.mask	= KVM_PTE_LEAF_ATTR_HI_S2_XN,
+		.val	= KVM_PTE_LEAF_ATTR_HI_S2_XN,
+		.set	= "XN",
+		.clear	= "  ",
+	}, {
+		.mask	= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R,
+		.val	= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R,
+		.set	= "R",
+		.clear	= " ",
+	}, {
+		.mask	= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W,
+		.val	= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W,
+		.set	= "W",
+		.clear	= " ",
+	}, {
+		.mask	= KVM_PTE_LEAF_ATTR_LO_S2_AF,
+		.val	= KVM_PTE_LEAF_ATTR_LO_S2_AF,
+		.set	= "AF",
+		.clear	= "  ",
+	}, {
+		.mask	= PTE_NG,
+		.val	= PTE_NG,
+		.set	= "FnXS",
+		.clear	= "  ",
+	}, {
+		.mask	= PTE_CONT,
+		.val	= PTE_CONT,
+		.set	= "CON",
+		.clear	= "   ",
+	}, {
+		.mask	= PTE_TABLE_BIT,
+		.val	= PTE_TABLE_BIT,
+		.set	= "   ",
+		.clear	= "BLK",
+	}, {
+		.mask	= KVM_PGTABLE_PROT_SW0,
+		.val	= KVM_PGTABLE_PROT_SW0,
+		.set	= "SW0", /* PKVM_PAGE_SHARED_OWNED */
+	}, {
+		.mask   = KVM_PGTABLE_PROT_SW1,
+		.val	= KVM_PGTABLE_PROT_SW1,
+		.set	= "SW1", /* PKVM_PAGE_SHARED_BORROWED */
+	}, {
+		.mask	= KVM_PGTABLE_PROT_SW2,
+		.val	= KVM_PGTABLE_PROT_SW2,
+		.set	= "SW2",
+	}, {
+		.mask   = KVM_PGTABLE_PROT_SW3,
+		.val	= KVM_PGTABLE_PROT_SW3,
+		.set	= "SW3",
+	},
+};
+
 struct pg_level {
 	const struct prot_bits *bits;
 	const char *name;
@@ -286,6 +347,7 @@  static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
 			delta >>= 10;
 			unit++;
 		}
+
 		pt_dump_seq_printf(st->seq, "%9lu%c %s", delta, *unit,
 				   pg_info[st->level].name);
 		if (st->current_prot && pg_info[st->level].bits)
@@ -394,6 +456,11 @@  static void *ptdump_host_va(phys_addr_t phys)
 	return __va(phys);
 }
 
+static struct kvm_pgtable_mm_ops host_mmops = {
+	.phys_to_virt	=	ptdump_host_va,
+	.virt_to_phys	=	ptdump_host_pa,
+};
+
 static size_t stage2_get_pgd_len(void)
 {
 	u64 mmfr0, mmfr1, vtcr;
@@ -528,6 +595,95 @@  static void stage2_ptdump_end_walk(void *file_priv)
 	f_priv->file_priv = NULL;
 }
 
+static int stage2_ptdump_visitor(const struct kvm_pgtable_visit_ctx *ctx,
+				 enum kvm_pgtable_walk_flags visit)
+{
+	struct pg_state *st = ctx->arg;
+	struct ptdump_state *pt_st = &st->ptdump;
+
+	pt_st->note_page(pt_st, ctx->addr, ctx->level, ctx->old);
+
+	return 0;
+}
+
+static void stage2_ptdump_build_levels(struct pg_level *level,
+				       size_t num_levels,
+				       unsigned int start_level)
+{
+	static const char * const lvl_names[] = {"PGD", "PUD", "PMD", "PTE"};
+	int i, j, name_index;
+
+	if (num_levels > KVM_PGTABLE_MAX_LEVELS && start_level > 2) {
+		pr_warn("invalid configuration %lu levels start_lvl %u\n",
+			num_levels, start_level);
+		return;
+	}
+
+	for (i = start_level; i < num_levels; i++) {
+		name_index = i - start_level;
+		name_index = name_index * start_level + name_index;
+
+		level[i].name	= lvl_names[name_index];
+		level[i].num	= ARRAY_SIZE(stage2_pte_bits);
+		level[i].bits	= stage2_pte_bits;
+
+		for (j = 0; j < level[i].num; j++)
+			level[i].mask |= level[i].bits[j].mask;
+	}
+}
+
+static void stage2_ptdump_walk(struct seq_file *s, struct ptdump_info *info)
+{
+	struct ptdump_info_file_priv *f_priv =
+		container_of(info, struct ptdump_info_file_priv, info);
+	struct kvm_pgtable_snapshot *snapshot = f_priv->file_priv;
+	struct pg_state st;
+	struct kvm_pgtable *pgtable;
+	u64 start_ipa = 0, end_ipa;
+	struct addr_marker ipa_address_markers[3];
+	struct pg_level stage2_pg_level[KVM_PGTABLE_MAX_LEVELS] = {0};
+	struct kvm_pgtable_walker walker = (struct kvm_pgtable_walker) {
+		.cb	= stage2_ptdump_visitor,
+		.arg	= &st,
+		.flags	= KVM_PGTABLE_WALK_LEAF,
+	};
+
+	if (snapshot == NULL || !snapshot->pgtable.pgd)
+		return;
+
+	pgtable = &snapshot->pgtable;
+	pgtable->mm_ops = &host_mmops;
+	end_ipa = BIT(pgtable->ia_bits) - 1;
+
+	memset(&ipa_address_markers[0], 0, sizeof(ipa_address_markers));
+
+	ipa_address_markers[0].start_address = start_ipa;
+	ipa_address_markers[0].name = "IPA start";
+
+	ipa_address_markers[1].start_address = end_ipa;
+	ipa_address_markers[1].name = "IPA end";
+
+	stage2_ptdump_build_levels(stage2_pg_level, KVM_PGTABLE_MAX_LEVELS,
+				   pgtable->start_level);
+
+	st = (struct pg_state) {
+		.seq		= s,
+		.marker		= &ipa_address_markers[0],
+		.level		= -1,
+		.pg_level	= &stage2_pg_level[0],
+		.f_priv		= f_priv,
+		.ptdump		= {
+			.note_page	= note_page,
+			.range		= (struct ptdump_range[]) {
+				{start_ipa,	end_ipa},
+				{0,		0},
+			},
+		},
+	};
+
+	kvm_pgtable_walk(pgtable, start_ipa, end_ipa, &walker);
+}
+
 void ptdump_register_host_stage2(void)
 {
 	if (!is_protected_kvm_enabled())
@@ -537,6 +693,7 @@  void ptdump_register_host_stage2(void)
 		.mc_len			= host_s2_pgtable_pages(),
 		.ptdump_prepare_walk	= stage2_ptdump_prepare_walk,
 		.ptdump_end_walk	= stage2_ptdump_end_walk,
+		.ptdump_walk		= stage2_ptdump_walk,
 	};
 
 	ptdump_debugfs_kvm_register(&stage2_kernel_ptdump_info,