All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sebastian Ene <sebastianene@google.com>
To: Vincent Donnefort <vdonnefort@google.com>
Cc: will@kernel.org, catalin.marinas@arm.com, mark.rutland@arm.com,
	akpm@linux-foundation.org, maz@kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, kernel-team@android.com,
	qperret@google.com, smostafa@google.com
Subject: Re: [PATCH v2 11/11] arm64: ptdump: Add support for guest stage-2 pagetables dumping
Date: Mon, 23 Oct 2023 14:45:41 +0000	[thread overview]
Message-ID: <ZTaHFQotXvRWad3Y@google.com> (raw)
In-Reply-To: <ZTI85i0PmI_6doz1@google.com>

On Fri, Oct 20, 2023 at 09:40:06AM +0100, Vincent Donnefort wrote:
> On Thu, Oct 19, 2023 at 02:40:33PM +0000, Sebastian Ene wrote:
> > Register a debugfs file on guest creation to be able to view their
> > second translation tables with ptdump. This assumes that the host is in
> > control of the guest stage-2 and has direct access to the pagetables.
> 
> What about pKVM? The walker you wrote for the host stage-2 should be
> reusable in that case?
> 

Yes, when pKVM will be ready upstream the walker which duplicates the
pagetables for the host will be re-used for the guests. We will have to
add a separate HVC for this which receives as an argument the guest
vmid.

> > 
> > Signed-off-by: Sebastian Ene <sebastianene@google.com>
> > ---
> >  arch/arm64/include/asm/ptdump.h | 21 +++++++--
> >  arch/arm64/kvm/mmu.c            |  3 ++
> >  arch/arm64/mm/ptdump.c          | 84 +++++++++++++++++++++++++++++++++
> >  arch/arm64/mm/ptdump_debugfs.c  |  5 +-
> >  4 files changed, 108 insertions(+), 5 deletions(-)
> > 
> > diff --git a/arch/arm64/include/asm/ptdump.h b/arch/arm64/include/asm/ptdump.h
> > index 35b883524462..be86244d532b 100644
> > --- a/arch/arm64/include/asm/ptdump.h
> > +++ b/arch/arm64/include/asm/ptdump.h
> > @@ -5,6 +5,8 @@
> >  #ifndef __ASM_PTDUMP_H
> >  #define __ASM_PTDUMP_H
> >  
> > +#include <asm/kvm_pgtable.h>
> > +
> >  #ifdef CONFIG_PTDUMP_CORE
> >  
> >  #include <linux/mm_types.h>
> > @@ -30,14 +32,27 @@ struct ptdump_info {
> >  void ptdump_walk(struct seq_file *s, struct ptdump_info *info);
> >  #ifdef CONFIG_PTDUMP_DEBUGFS
> >  #define EFI_RUNTIME_MAP_END	DEFAULT_MAP_WINDOW_64
> > -void __init ptdump_debugfs_register(struct ptdump_info *info, const char *name);
> > +struct dentry *ptdump_debugfs_register(struct ptdump_info *info,
> > +				       const char *name);
> >  #else
> > -static inline void ptdump_debugfs_register(struct ptdump_info *info,
> > -					   const char *name) { }
> > +static inline struct dentry *ptdump_debugfs_register(struct ptdump_info *info,
> > +						     const char *name)
> > +{
> > +	return NULL;
> > +}
> >  #endif
> >  void ptdump_check_wx(void);
> >  #endif /* CONFIG_PTDUMP_CORE */
> >  
> > +#ifdef CONFIG_NVHE_EL2_PTDUMP_DEBUGFS
> > +void ptdump_register_guest_stage2(struct kvm_pgtable *pgt, void *lock);
> > +void ptdump_unregister_guest_stage2(struct kvm_pgtable *pgt);
> > +#else
> > +static inline void ptdump_register_guest_stage2(struct kvm_pgtable *pgt,
> > +						void *lock) { }
> > +static inline void ptdump_unregister_guest_stage2(struct kvm_pgtable *pgt) { }
> > +#endif /* CONFIG_NVHE_EL2_PTDUMP_DEBUGFS */
> 
> I believe this should be compatible with VHE as well, that option should be
> renamed.
> 

Good point, I will rename this.

> > +
> >  #ifdef CONFIG_DEBUG_WX
> >  #define debug_checkwx()	ptdump_check_wx()
> >  #else
> > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
> > index 482280fe22d7..e47988dba34d 100644
> > --- a/arch/arm64/kvm/mmu.c
> > +++ b/arch/arm64/kvm/mmu.c
> > @@ -11,6 +11,7 @@
> >  #include <linux/sched/signal.h>
> >  #include <trace/events/kvm.h>
> >  #include <asm/pgalloc.h>
> > +#include <asm/ptdump.h>
> >  #include <asm/cacheflush.h>
> >  #include <asm/kvm_arm.h>
> >  #include <asm/kvm_mmu.h>
> > @@ -908,6 +909,7 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
> >  	if (err)
> >  		goto out_free_pgtable;
> >  
> > +	ptdump_register_guest_stage2(pgt, &kvm->mmu_lock);
> >  	mmu->last_vcpu_ran = alloc_percpu(typeof(*mmu->last_vcpu_ran));
> >  	if (!mmu->last_vcpu_ran) {
> >  		err = -ENOMEM;
> > @@ -1021,6 +1023,7 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
> >  	write_unlock(&kvm->mmu_lock);
> >  
> >  	if (pgt) {
> > +		ptdump_unregister_guest_stage2(pgt);
> >  		kvm_pgtable_stage2_destroy(pgt);
> >  		kfree(pgt);
> >  	}
> > diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c
> > index 4687840dcb69..facfb15468f5 100644
> > --- a/arch/arm64/mm/ptdump.c
> > +++ b/arch/arm64/mm/ptdump.c
> > @@ -26,6 +26,7 @@
> >  #include <asm/ptdump.h>
> >  #include <asm/kvm_pkvm.h>
> >  #include <asm/kvm_pgtable.h>
> > +#include <asm/kvm_host.h>
> >  
> >  
> >  enum address_markers_idx {
> > @@ -543,6 +544,22 @@ void ptdump_check_wx(void)
> >  #ifdef CONFIG_NVHE_EL2_PTDUMP_DEBUGFS
> >  static struct ptdump_info stage2_kernel_ptdump_info;
> >  
> > +#define GUEST_NAME_LEN	(32U)
> > +
> > +struct ptdump_registered_guest {
> > +	struct list_head		reg_list;
> > +	struct ptdump_info		info;
> > +	struct mm_struct		mem;
> > +	struct kvm_pgtable_snapshot	snapshot;
> > +	struct dentry			*dentry;
> > +	rwlock_t			*lock;
> > +	char				reg_name[GUEST_NAME_LEN];
> > +};
> > +
> > +static LIST_HEAD(ptdump_guest_list);
> > +static DEFINE_MUTEX(ptdump_list_lock);
> > +static u16 guest_no;
> 
> This is not robust enough: If 1 VM starts then 65535 others which are killed.
> guest_no overflows. The next number is 0 which is already taken.
>

Yes, I guess this should be improved. In the case you described we won't
register any debugfs file because of the name clash.

> Linux has and ID allocation to solve this problem, but I don't think this is
> necessary anyway. This should simply reuse the struct kvm->debugfs_dentry.
> 
> Also probably most of the informations contained in ptdump_registered_guest can
> be found in struct kvm. The debugfs should then probably simply take struct kvm
> for the private argument.
>

I would prefer to keep it as a separate struct here as it gives some
flexibility if we need to extend it for guests pKVM support. I think we
can drop the struct mm_struct from here.

Thanks,
Sebastian

> > +
> >  static phys_addr_t ptdump_host_pa(void *addr)
> >  {
> >  	return __pa(addr);
> > @@ -740,6 +757,73 @@ static void stage2_ptdump_walk(struct seq_file *s, struct ptdump_info *info)
> >  
> >  	kvm_pgtable_walk(pgtable, start_ipa, end_ipa, &walker);
> >  }
> 
> [...]

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

WARNING: multiple messages have this Message-ID (diff)
From: Sebastian Ene <sebastianene@google.com>
To: Vincent Donnefort <vdonnefort@google.com>
Cc: will@kernel.org, catalin.marinas@arm.com, mark.rutland@arm.com,
	akpm@linux-foundation.org, maz@kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, kernel-team@android.com,
	qperret@google.com, smostafa@google.com
Subject: Re: [PATCH v2 11/11] arm64: ptdump: Add support for guest stage-2 pagetables dumping
Date: Mon, 23 Oct 2023 14:45:41 +0000	[thread overview]
Message-ID: <ZTaHFQotXvRWad3Y@google.com> (raw)
In-Reply-To: <ZTI85i0PmI_6doz1@google.com>

On Fri, Oct 20, 2023 at 09:40:06AM +0100, Vincent Donnefort wrote:
> On Thu, Oct 19, 2023 at 02:40:33PM +0000, Sebastian Ene wrote:
> > Register a debugfs file on guest creation to be able to view their
> > second translation tables with ptdump. This assumes that the host is in
> > control of the guest stage-2 and has direct access to the pagetables.
> 
> What about pKVM? The walker you wrote for the host stage-2 should be
> reusable in that case?
> 

Yes, when pKVM will be ready upstream the walker which duplicates the
pagetables for the host will be re-used for the guests. We will have to
add a separate HVC for this which receives as an argument the guest
vmid.

> > 
> > Signed-off-by: Sebastian Ene <sebastianene@google.com>
> > ---
> >  arch/arm64/include/asm/ptdump.h | 21 +++++++--
> >  arch/arm64/kvm/mmu.c            |  3 ++
> >  arch/arm64/mm/ptdump.c          | 84 +++++++++++++++++++++++++++++++++
> >  arch/arm64/mm/ptdump_debugfs.c  |  5 +-
> >  4 files changed, 108 insertions(+), 5 deletions(-)
> > 
> > diff --git a/arch/arm64/include/asm/ptdump.h b/arch/arm64/include/asm/ptdump.h
> > index 35b883524462..be86244d532b 100644
> > --- a/arch/arm64/include/asm/ptdump.h
> > +++ b/arch/arm64/include/asm/ptdump.h
> > @@ -5,6 +5,8 @@
> >  #ifndef __ASM_PTDUMP_H
> >  #define __ASM_PTDUMP_H
> >  
> > +#include <asm/kvm_pgtable.h>
> > +
> >  #ifdef CONFIG_PTDUMP_CORE
> >  
> >  #include <linux/mm_types.h>
> > @@ -30,14 +32,27 @@ struct ptdump_info {
> >  void ptdump_walk(struct seq_file *s, struct ptdump_info *info);
> >  #ifdef CONFIG_PTDUMP_DEBUGFS
> >  #define EFI_RUNTIME_MAP_END	DEFAULT_MAP_WINDOW_64
> > -void __init ptdump_debugfs_register(struct ptdump_info *info, const char *name);
> > +struct dentry *ptdump_debugfs_register(struct ptdump_info *info,
> > +				       const char *name);
> >  #else
> > -static inline void ptdump_debugfs_register(struct ptdump_info *info,
> > -					   const char *name) { }
> > +static inline struct dentry *ptdump_debugfs_register(struct ptdump_info *info,
> > +						     const char *name)
> > +{
> > +	return NULL;
> > +}
> >  #endif
> >  void ptdump_check_wx(void);
> >  #endif /* CONFIG_PTDUMP_CORE */
> >  
> > +#ifdef CONFIG_NVHE_EL2_PTDUMP_DEBUGFS
> > +void ptdump_register_guest_stage2(struct kvm_pgtable *pgt, void *lock);
> > +void ptdump_unregister_guest_stage2(struct kvm_pgtable *pgt);
> > +#else
> > +static inline void ptdump_register_guest_stage2(struct kvm_pgtable *pgt,
> > +						void *lock) { }
> > +static inline void ptdump_unregister_guest_stage2(struct kvm_pgtable *pgt) { }
> > +#endif /* CONFIG_NVHE_EL2_PTDUMP_DEBUGFS */
> 
> I believe this should be compatible with VHE as well, that option should be
> renamed.
> 

Good point, I will rename this.

> > +
> >  #ifdef CONFIG_DEBUG_WX
> >  #define debug_checkwx()	ptdump_check_wx()
> >  #else
> > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
> > index 482280fe22d7..e47988dba34d 100644
> > --- a/arch/arm64/kvm/mmu.c
> > +++ b/arch/arm64/kvm/mmu.c
> > @@ -11,6 +11,7 @@
> >  #include <linux/sched/signal.h>
> >  #include <trace/events/kvm.h>
> >  #include <asm/pgalloc.h>
> > +#include <asm/ptdump.h>
> >  #include <asm/cacheflush.h>
> >  #include <asm/kvm_arm.h>
> >  #include <asm/kvm_mmu.h>
> > @@ -908,6 +909,7 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
> >  	if (err)
> >  		goto out_free_pgtable;
> >  
> > +	ptdump_register_guest_stage2(pgt, &kvm->mmu_lock);
> >  	mmu->last_vcpu_ran = alloc_percpu(typeof(*mmu->last_vcpu_ran));
> >  	if (!mmu->last_vcpu_ran) {
> >  		err = -ENOMEM;
> > @@ -1021,6 +1023,7 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
> >  	write_unlock(&kvm->mmu_lock);
> >  
> >  	if (pgt) {
> > +		ptdump_unregister_guest_stage2(pgt);
> >  		kvm_pgtable_stage2_destroy(pgt);
> >  		kfree(pgt);
> >  	}
> > diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c
> > index 4687840dcb69..facfb15468f5 100644
> > --- a/arch/arm64/mm/ptdump.c
> > +++ b/arch/arm64/mm/ptdump.c
> > @@ -26,6 +26,7 @@
> >  #include <asm/ptdump.h>
> >  #include <asm/kvm_pkvm.h>
> >  #include <asm/kvm_pgtable.h>
> > +#include <asm/kvm_host.h>
> >  
> >  
> >  enum address_markers_idx {
> > @@ -543,6 +544,22 @@ void ptdump_check_wx(void)
> >  #ifdef CONFIG_NVHE_EL2_PTDUMP_DEBUGFS
> >  static struct ptdump_info stage2_kernel_ptdump_info;
> >  
> > +#define GUEST_NAME_LEN	(32U)
> > +
> > +struct ptdump_registered_guest {
> > +	struct list_head		reg_list;
> > +	struct ptdump_info		info;
> > +	struct mm_struct		mem;
> > +	struct kvm_pgtable_snapshot	snapshot;
> > +	struct dentry			*dentry;
> > +	rwlock_t			*lock;
> > +	char				reg_name[GUEST_NAME_LEN];
> > +};
> > +
> > +static LIST_HEAD(ptdump_guest_list);
> > +static DEFINE_MUTEX(ptdump_list_lock);
> > +static u16 guest_no;
> 
> This is not robust enough: If 1 VM starts then 65535 others which are killed.
> guest_no overflows. The next number is 0 which is already taken.
>

Yes, I guess this should be improved. In the case you described we won't
register any debugfs file because of the name clash.

> Linux has and ID allocation to solve this problem, but I don't think this is
> necessary anyway. This should simply reuse the struct kvm->debugfs_dentry.
> 
> Also probably most of the informations contained in ptdump_registered_guest can
> be found in struct kvm. The debugfs should then probably simply take struct kvm
> for the private argument.
>

I would prefer to keep it as a separate struct here as it gives some
flexibility if we need to extend it for guests pKVM support. I think we
can drop the struct mm_struct from here.

Thanks,
Sebastian

> > +
> >  static phys_addr_t ptdump_host_pa(void *addr)
> >  {
> >  	return __pa(addr);
> > @@ -740,6 +757,73 @@ static void stage2_ptdump_walk(struct seq_file *s, struct ptdump_info *info)
> >  
> >  	kvm_pgtable_walk(pgtable, start_ipa, end_ipa, &walker);
> >  }
> 
> [...]

  reply	other threads:[~2023-10-23 14:46 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-19 14:40 [PATCH v2 00/11] arm64: ptdump: View the second stage page-tables Sebastian Ene
2023-10-19 14:40 ` Sebastian Ene
2023-10-19 14:40 ` [PATCH v2 01/11] KVM: arm64: Add snap shooting the host stage-2 pagetables Sebastian Ene
2023-10-19 14:40   ` Sebastian Ene
2023-10-26 12:45   ` kernel test robot
2023-10-26 12:45     ` kernel test robot
2023-10-19 14:40 ` [PATCH v2 02/11] arm64: ptdump: Use the mask from the state structure Sebastian Ene
2023-10-19 14:40   ` Sebastian Ene
2023-10-19 14:40 ` [PATCH v2 03/11] arm64: ptdump: Add the walker function to the ptdump info structure Sebastian Ene
2023-10-19 14:40   ` Sebastian Ene
2023-10-19 14:40 ` [PATCH v2 04/11] KVM: arm64: Move pagetable definitions to common header Sebastian Ene
2023-10-19 14:40   ` Sebastian Ene
2023-10-19 14:40 ` [PATCH v2 05/11] arm64: ptdump: Introduce stage-2 pagetables format description Sebastian Ene
2023-10-19 14:40   ` Sebastian Ene
2023-10-19 14:40 ` [PATCH v2 06/11] arm64: ptdump: Add hooks on debugfs file operations Sebastian Ene
2023-10-19 14:40   ` Sebastian Ene
2023-10-19 14:40 ` [PATCH v2 07/11] arm64: ptdump: Register a debugfs entry for the host stage-2 page-tables Sebastian Ene
2023-10-19 14:40   ` Sebastian Ene
2023-10-19 14:40 ` [PATCH v2 08/11] arm64: ptdump: Parse the host stage-2 page-tables from the snapshot Sebastian Ene
2023-10-19 14:40   ` Sebastian Ene
2023-10-19 14:40 ` [PATCH v2 09/11] arm64: ptdump: Interpret memory attributes based on runtime configuration Sebastian Ene
2023-10-19 14:40   ` Sebastian Ene
2023-10-19 14:40 ` [PATCH v2 10/11] arm64: ptdump: Interpret pKVM ownership annotations Sebastian Ene
2023-10-19 14:40   ` Sebastian Ene
2023-10-19 14:40 ` [PATCH v2 11/11] arm64: ptdump: Add support for guest stage-2 pagetables dumping Sebastian Ene
2023-10-19 14:40   ` Sebastian Ene
2023-10-20  8:40   ` Vincent Donnefort
2023-10-20  8:40     ` Vincent Donnefort
2023-10-23 14:45     ` Sebastian Ene [this message]
2023-10-23 14:45       ` Sebastian Ene
2023-10-20  8:19 ` [PATCH v2 00/11] arm64: ptdump: View the second stage page-tables Vincent Donnefort
2023-10-20  8:19   ` Vincent Donnefort
2023-10-23 14:32   ` Sebastian Ene
2023-10-23 14:32     ` Sebastian Ene

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZTaHFQotXvRWad3Y@google.com \
    --to=sebastianene@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=catalin.marinas@arm.com \
    --cc=kernel-team@android.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=maz@kernel.org \
    --cc=qperret@google.com \
    --cc=smostafa@google.com \
    --cc=vdonnefort@google.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.