From: David Gibson Date: Wed, 26 Sep 2018 03:27:41 +0000 Subject: Re: [RFC PATCH 10/32] KVM: PPC: Book3S HV: Add a debugfs file to dump radix mappings Message-Id: <20180926032741.GL30868@umbus.fritz.box> MIME-Version: 1 Content-Type: multipart/mixed; boundary="eeg465crObqkzWj4" List-Id: References: <1537524123-9578-11-git-send-email-paulus@ozlabs.org> In-Reply-To: <1537524123-9578-11-git-send-email-paulus@ozlabs.org> To: kvm-ppc@vger.kernel.org --eeg465crObqkzWj4 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Sep 21, 2018 at 08:01:41PM +1000, Paul Mackerras wrote: > This adds a file called 'radix' in the debugfs directory for the > guest, which when read gives all of the valid leaf PTEs in the > partition-scoped radix tree for a radix guest, in human-readable > format. It is analogous to the existing 'htab' file which dumps > the HPT entries for a HPT guest. >=20 > Signed-off-by: Paul Mackerras Reviewed-by: David Gibson > --- > arch/powerpc/include/asm/kvm_book3s_64.h | 1 + > arch/powerpc/include/asm/kvm_host.h | 1 + > arch/powerpc/kvm/book3s_64_mmu_radix.c | 179 +++++++++++++++++++++++++= ++++++ > arch/powerpc/kvm/book3s_hv.c | 2 + > 4 files changed, 183 insertions(+) >=20 > diff --git a/arch/powerpc/include/asm/kvm_book3s_64.h b/arch/powerpc/incl= ude/asm/kvm_book3s_64.h > index dc435a5..af25aaa 100644 > --- a/arch/powerpc/include/asm/kvm_book3s_64.h > +++ b/arch/powerpc/include/asm/kvm_book3s_64.h > @@ -435,6 +435,7 @@ static inline struct kvm_memslots *kvm_memslots_raw(s= truct kvm *kvm) > } > =20 > extern void kvmppc_mmu_debugfs_init(struct kvm *kvm); > +extern void kvmhv_radix_debugfs_init(struct kvm *kvm); > =20 > extern void kvmhv_rm_send_ipi(int cpu); > =20 > diff --git a/arch/powerpc/include/asm/kvm_host.h b/arch/powerpc/include/a= sm/kvm_host.h > index 3cd0b9f..a3d4f61 100644 > --- a/arch/powerpc/include/asm/kvm_host.h > +++ b/arch/powerpc/include/asm/kvm_host.h > @@ -291,6 +291,7 @@ struct kvm_arch { > u64 process_table; > struct dentry *debugfs_dir; > struct dentry *htab_dentry; > + struct dentry *radix_dentry; > struct kvm_resize_hpt *resize_hpt; /* protected by kvm->lock */ > #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */ > #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE > diff --git a/arch/powerpc/kvm/book3s_64_mmu_radix.c b/arch/powerpc/kvm/bo= ok3s_64_mmu_radix.c > index 933c574..71951b5 100644 > --- a/arch/powerpc/kvm/book3s_64_mmu_radix.c > +++ b/arch/powerpc/kvm/book3s_64_mmu_radix.c > @@ -10,6 +10,9 @@ > #include > #include > #include > +#include > +#include > +#include > =20 > #include > #include > @@ -853,6 +856,182 @@ static void pmd_ctor(void *addr) > memset(addr, 0, RADIX_PMD_TABLE_SIZE); > } > =20 > +struct debugfs_radix_state { > + struct kvm *kvm; > + struct mutex mutex; > + unsigned long gpa; > + int chars_left; > + int buf_index; > + char buf[128]; > + u8 hdr; > +}; > + > +static int debugfs_radix_open(struct inode *inode, struct file *file) > +{ > + struct kvm *kvm =3D inode->i_private; > + struct debugfs_radix_state *p; > + > + p =3D kzalloc(sizeof(*p), GFP_KERNEL); > + if (!p) > + return -ENOMEM; > + > + kvm_get_kvm(kvm); > + p->kvm =3D kvm; > + mutex_init(&p->mutex); > + file->private_data =3D p; > + > + return nonseekable_open(inode, file); > +} > + > +static int debugfs_radix_release(struct inode *inode, struct file *file) > +{ > + struct debugfs_radix_state *p =3D file->private_data; > + > + kvm_put_kvm(p->kvm); > + kfree(p); > + return 0; > +} > + > +static ssize_t debugfs_radix_read(struct file *file, char __user *buf, > + size_t len, loff_t *ppos) > +{ > + struct debugfs_radix_state *p =3D file->private_data; > + ssize_t ret, r; > + unsigned long n; > + struct kvm *kvm; > + unsigned long gpa; > + pgd_t *pgt; > + pgd_t pgd, *pgdp; > + pud_t pud, *pudp; > + pmd_t pmd, *pmdp; > + pte_t *ptep; > + int shift; > + unsigned long pte; > + > + kvm =3D p->kvm; > + if (!kvm_is_radix(kvm)) > + return 0; > + > + ret =3D mutex_lock_interruptible(&p->mutex); > + if (ret) > + return ret; > + > + if (p->chars_left) { > + n =3D p->chars_left; > + if (n > len) > + n =3D len; > + r =3D copy_to_user(buf, p->buf + p->buf_index, n); > + n -=3D r; > + p->chars_left -=3D n; > + p->buf_index +=3D n; > + buf +=3D n; > + len -=3D n; > + ret =3D n; > + if (r) { > + if (!n) > + ret =3D -EFAULT; > + goto out; > + } > + } > + > + gpa =3D p->gpa; > + pgt =3D kvm->arch.pgtable; > + while (len !=3D 0 && gpa < RADIX_PGTABLE_RANGE) { > + if (!p->hdr) { > + n =3D scnprintf(p->buf, sizeof(p->buf), > + "pgdir: %lx\n", (unsigned long)pgt); > + p->hdr =3D 1; > + goto copy; > + } > + > + pgdp =3D pgt + pgd_index(gpa); > + pgd =3D READ_ONCE(*pgdp); > + if (!(pgd_val(pgd) & _PAGE_PRESENT)) { > + gpa =3D (gpa & PGDIR_MASK) + PGDIR_SIZE; > + continue; > + } > + > + pudp =3D pud_offset(&pgd, gpa); > + pud =3D READ_ONCE(*pudp); > + if (!(pud_val(pud) & _PAGE_PRESENT)) { > + gpa =3D (gpa & PUD_MASK) + PUD_SIZE; > + continue; > + } > + if (pud_val(pud) & _PAGE_PTE) { > + pte =3D pud_val(pud); > + shift =3D PUD_SHIFT; > + goto leaf; > + } > + > + pmdp =3D pmd_offset(&pud, gpa); > + pmd =3D READ_ONCE(*pmdp); > + if (!(pmd_val(pmd) & _PAGE_PRESENT)) { > + gpa =3D (gpa & PMD_MASK) + PMD_SIZE; > + continue; > + } > + if (pmd_val(pmd) & _PAGE_PTE) { > + pte =3D pmd_val(pmd); > + shift =3D PMD_SHIFT; > + goto leaf; > + } > + > + ptep =3D pte_offset_kernel(&pmd, gpa); > + pte =3D pte_val(READ_ONCE(*ptep)); > + if (!(pte & _PAGE_PRESENT)) { > + gpa +=3D PAGE_SIZE; > + continue; > + } > + shift =3D PAGE_SHIFT; > + leaf: > + n =3D scnprintf(p->buf, sizeof(p->buf), > + " %lx: %lx %d\n", gpa, pte, shift); > + gpa +=3D 1ul << shift; > + copy: > + p->chars_left =3D n; > + if (n > len) > + n =3D len; > + r =3D copy_to_user(buf, p->buf, n); > + n -=3D r; > + p->chars_left -=3D n; > + p->buf_index =3D n; > + buf +=3D n; > + len -=3D n; > + ret +=3D n; > + if (r) { > + if (!ret) > + ret =3D -EFAULT; > + break; > + } > + } > + p->gpa =3D gpa; > + > + out: > + mutex_unlock(&p->mutex); > + return ret; > +} > + > +static ssize_t debugfs_radix_write(struct file *file, const char __user = *buf, > + size_t len, loff_t *ppos) > +{ > + return -EACCES; > +} > + > +static const struct file_operations debugfs_radix_fops =3D { > + .owner =3D THIS_MODULE, > + .open =3D debugfs_radix_open, > + .release =3D debugfs_radix_release, > + .read =3D debugfs_radix_read, > + .write =3D debugfs_radix_write, > + .llseek =3D generic_file_llseek, > +}; > + > +void kvmhv_radix_debugfs_init(struct kvm *kvm) > +{ > + kvm->arch.radix_dentry =3D debugfs_create_file("radix", 0400, > + kvm->arch.debugfs_dir, kvm, > + &debugfs_radix_fops); > +} > + > int kvmppc_radix_init(void) > { > unsigned long size =3D sizeof(void *) << RADIX_PTE_INDEX_SIZE; > diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c > index e0e92f2..e699787 100644 > --- a/arch/powerpc/kvm/book3s_hv.c > +++ b/arch/powerpc/kvm/book3s_hv.c > @@ -4475,6 +4475,8 @@ static int kvmppc_core_init_vm_hv(struct kvm *kvm) > snprintf(buf, sizeof(buf), "vm%d", current->pid); > kvm->arch.debugfs_dir =3D debugfs_create_dir(buf, kvm_debugfs_dir); > kvmppc_mmu_debugfs_init(kvm); > + if (radix_enabled()) > + kvmhv_radix_debugfs_init(kvm); > =20 > return 0; > } --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --eeg465crObqkzWj4 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEdfRlhq5hpmzETofcbDjKyiDZs5IFAluq/KsACgkQbDjKyiDZ s5JB4Q/+MHE7BRFgCfWUnofa6gA44Zyh4YICgX+TxMMCYML9mtWzsQHdbcvt2lDt WqjEg4pAwXo/4+ItFICRcKYm9nrZzFsMi+8xk9y+cE6vsc4fIqw+bxVuzh/enxZW dEMlayvTiO+fAOF3RWZYyuXoJqrNA5NiRDOTGY2QrmPMJDnJs66GPFiPRSEJejS6 SgN6c2f8q0KNZia6ODR6ftofUudxJENJeMN3UgOZykLNc7WLZfEuoc8k4DQj6mmU Mb+qENNi11xZsrBZdyHv5JXcxmcGtq/DL+xPKXScXm3SA7whog4Xi5qCs4wNtkas ZtcyebHy1ydn5mI9TDLLNle4olyegU6iSriqKYwOWi39VHOAxeeaPd2XB9e6tRD5 nloetT1cbumfnvnmR8Nx75feyWfMlCfPtGZwDUd8e4WDjfdm9a8gS0MOkZt7Wro6 UbF4piMhaygfQlcF73qk5yh2OhevNSRQPwlVQ5GGGP8qCk8jvkmyldYklu3VFN1U unv06bzZ/zCd2EG1yHh/4M7aU5VqCs5dOgGzhlWW4H1jynBIQj3OiSLmQAnkW82r zJQv1qlrPcrF+pJxfJLBhE457WagZUc9y+w3TlrX1IMufh34bH08itqugU5ECmbi HklDmCYjFgFQMGNySiuQknoApQMp073G4WzMBArN7Y8ptujMlLQ= =UB8Y -----END PGP SIGNATURE----- --eeg465crObqkzWj4--