All of lore.kernel.org
 help / color / mirror / Atom feed
From: marc.zyngier@arm.com (Marc Zyngier)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH v2 6/6] arm/arm64: KVM: Introduce stage2_unmap_vm
Date: Mon, 08 Dec 2014 12:08:19 +0000	[thread overview]
Message-ID: <548594B3.8040507@arm.com> (raw)
In-Reply-To: <1417641522-29056-7-git-send-email-christoffer.dall@linaro.org>

On 03/12/14 21:18, Christoffer Dall wrote:
> Introduce a new function to unmap user RAM regions in the stage2 page
> tables.  This is needed on reboot (or when the guest turns off the MMU)
> to ensure we fault in pages again and make the dcache, RAM, and icache
> coherent.
> 
> Using unmap_stage2_range for the whole guest physical range does not
> work, because that unmaps IO regions (such as the GIC) which will not be
> recreated or in the best case faulted in on a page-by-page basis.
> 
> Call this function on secondary and subsequent calls to the
> KVM_ARM_VCPU_INIT ioctl so that a reset VCPU will detect the guest
> Stage-1 MMU is off when faulting in pages and make the caches coherent.
> 
> Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>
> ---
>  arch/arm/include/asm/kvm_mmu.h   |  1 +
>  arch/arm/kvm/arm.c               |  7 +++++
>  arch/arm/kvm/mmu.c               | 65 ++++++++++++++++++++++++++++++++++++++++
>  arch/arm64/include/asm/kvm_mmu.h |  1 +
>  4 files changed, 74 insertions(+)
> 
> diff --git a/arch/arm/include/asm/kvm_mmu.h b/arch/arm/include/asm/kvm_mmu.h
> index acb0d57..4654c42 100644
> --- a/arch/arm/include/asm/kvm_mmu.h
> +++ b/arch/arm/include/asm/kvm_mmu.h
> @@ -52,6 +52,7 @@ int create_hyp_io_mappings(void *from, void *to, phys_addr_t);
>  void free_boot_hyp_pgd(void);
>  void free_hyp_pgds(void);
>  
> +void stage2_unmap_vm(struct kvm *kvm);
>  int kvm_alloc_stage2_pgd(struct kvm *kvm);
>  void kvm_free_stage2_pgd(struct kvm *kvm);
>  int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
> diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c
> index 4043769..da87c07 100644
> --- a/arch/arm/kvm/arm.c
> +++ b/arch/arm/kvm/arm.c
> @@ -701,6 +701,13 @@ static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
>  	if (ret)
>  		return ret;
>  
> +	/*
> +	 * Ensure a rebooted VM will fault in RAM pages and detect if the
> +	 * guest MMU is turned off and flush the caches as needed.
> +	 */
> +	if (vcpu->arch.has_run_once)
> +		stage2_unmap_vm(vcpu->kvm);
> +
>  	vcpu_reset_hcr(vcpu);
>  
>  	/*
> diff --git a/arch/arm/kvm/mmu.c b/arch/arm/kvm/mmu.c
> index 57a403a..b1f3c9a 100644
> --- a/arch/arm/kvm/mmu.c
> +++ b/arch/arm/kvm/mmu.c
> @@ -611,6 +611,71 @@ static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
>  	unmap_range(kvm, kvm->arch.pgd, start, size);
>  }
>  
> +static void stage2_unmap_memslot(struct kvm *kvm,
> +				 struct kvm_memory_slot *memslot)
> +{
> +	hva_t hva = memslot->userspace_addr;
> +	phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
> +	phys_addr_t size = PAGE_SIZE * memslot->npages;
> +	hva_t reg_end = hva + size;
> +
> +	/*
> +	 * A memory region could potentially cover multiple VMAs, and any holes
> +	 * between them, so iterate over all of them to find out if we should
> +	 * unmap any of them.
> +	 *
> +	 *     +--------------------------------------------+
> +	 * +---------------+----------------+   +----------------+
> +	 * |   : VMA 1     |      VMA 2     |   |    VMA 3  :    |
> +	 * +---------------+----------------+   +----------------+
> +	 *     |               memory region                |
> +	 *     +--------------------------------------------+
> +	 */
> +	do {
> +		struct vm_area_struct *vma = find_vma(current->mm, hva);
> +		hva_t vm_start, vm_end;
> +
> +		if (!vma || vma->vm_start >= reg_end)
> +			break;
> +
> +		/*
> +		 * Take the intersection of this VMA with the memory region
> +		 */
> +		vm_start = max(hva, vma->vm_start);
> +		vm_end = min(reg_end, vma->vm_end);
> +
> +		if (!(vma->vm_flags & VM_PFNMAP)) {
> +			gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
> +			unmap_stage2_range(kvm, gpa, vm_end - vm_start);
> +		}
> +		hva = vm_end;
> +	} while (hva < reg_end);
> +}
> +
> +/**
> + * stage2_unmap_vm - Unmap Stage-2 RAM mappings
> + * @kvm: The struct kvm pointer
> + *
> + * Go through the memregions and unmap any reguler RAM
> + * backing memory already mapped to the VM.
> + */
> +void stage2_unmap_vm(struct kvm *kvm)
> +{
> +	struct kvm_memslots *slots;
> +	struct kvm_memory_slot *memslot;
> +	int idx;
> +
> +	idx = srcu_read_lock(&kvm->srcu);
> +	spin_lock(&kvm->mmu_lock);
> +
> +	slots = kvm_memslots(kvm);
> +	kvm_for_each_memslot(memslot, slots)
> +		stage2_unmap_memslot(kvm, memslot);
> +
> +	spin_unlock(&kvm->mmu_lock);
> +	srcu_read_unlock(&kvm->srcu, idx);
> +}
> +
>  /**
>   * kvm_free_stage2_pgd - free all stage-2 tables
>   * @kvm:	The KVM struct pointer for the VM.
> diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h
> index 0caf7a5..061fed7 100644
> --- a/arch/arm64/include/asm/kvm_mmu.h
> +++ b/arch/arm64/include/asm/kvm_mmu.h
> @@ -83,6 +83,7 @@ int create_hyp_io_mappings(void *from, void *to, phys_addr_t);
>  void free_boot_hyp_pgd(void);
>  void free_hyp_pgds(void);
>  
> +void stage2_unmap_vm(struct kvm *kvm);
>  int kvm_alloc_stage2_pgd(struct kvm *kvm);
>  void kvm_free_stage2_pgd(struct kvm *kvm);
>  int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
> 

Acked-by: Marc Zyngier <marc.zyngier@arm.com>

	M.
-- 
Jazz is not dead. It just smells funny...

WARNING: multiple messages have this Message-ID (diff)
From: Marc Zyngier <marc.zyngier@arm.com>
To: Christoffer Dall <christoffer.dall@linaro.org>,
	"kvmarm@lists.cs.columbia.edu" <kvmarm@lists.cs.columbia.edu>,
	"linux-arm-kernel@lists.infradead.org"
	<linux-arm-kernel@lists.infradead.org>
Cc: "kvm@vger.kernel.org" <kvm@vger.kernel.org>,
	Ard Biesheuvel <ard.biesheuvel@linaro.org>,
	Peter Maydell <peter.maydell@linaro.org>,
	Laszlo Ersek <lersek@redhat.com>,
	Andrew Jones <drjones@redhat.com>
Subject: Re: [PATCH v2 6/6] arm/arm64: KVM: Introduce stage2_unmap_vm
Date: Mon, 08 Dec 2014 12:08:19 +0000	[thread overview]
Message-ID: <548594B3.8040507@arm.com> (raw)
In-Reply-To: <1417641522-29056-7-git-send-email-christoffer.dall@linaro.org>

On 03/12/14 21:18, Christoffer Dall wrote:
> Introduce a new function to unmap user RAM regions in the stage2 page
> tables.  This is needed on reboot (or when the guest turns off the MMU)
> to ensure we fault in pages again and make the dcache, RAM, and icache
> coherent.
> 
> Using unmap_stage2_range for the whole guest physical range does not
> work, because that unmaps IO regions (such as the GIC) which will not be
> recreated or in the best case faulted in on a page-by-page basis.
> 
> Call this function on secondary and subsequent calls to the
> KVM_ARM_VCPU_INIT ioctl so that a reset VCPU will detect the guest
> Stage-1 MMU is off when faulting in pages and make the caches coherent.
> 
> Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>
> ---
>  arch/arm/include/asm/kvm_mmu.h   |  1 +
>  arch/arm/kvm/arm.c               |  7 +++++
>  arch/arm/kvm/mmu.c               | 65 ++++++++++++++++++++++++++++++++++++++++
>  arch/arm64/include/asm/kvm_mmu.h |  1 +
>  4 files changed, 74 insertions(+)
> 
> diff --git a/arch/arm/include/asm/kvm_mmu.h b/arch/arm/include/asm/kvm_mmu.h
> index acb0d57..4654c42 100644
> --- a/arch/arm/include/asm/kvm_mmu.h
> +++ b/arch/arm/include/asm/kvm_mmu.h
> @@ -52,6 +52,7 @@ int create_hyp_io_mappings(void *from, void *to, phys_addr_t);
>  void free_boot_hyp_pgd(void);
>  void free_hyp_pgds(void);
>  
> +void stage2_unmap_vm(struct kvm *kvm);
>  int kvm_alloc_stage2_pgd(struct kvm *kvm);
>  void kvm_free_stage2_pgd(struct kvm *kvm);
>  int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
> diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c
> index 4043769..da87c07 100644
> --- a/arch/arm/kvm/arm.c
> +++ b/arch/arm/kvm/arm.c
> @@ -701,6 +701,13 @@ static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
>  	if (ret)
>  		return ret;
>  
> +	/*
> +	 * Ensure a rebooted VM will fault in RAM pages and detect if the
> +	 * guest MMU is turned off and flush the caches as needed.
> +	 */
> +	if (vcpu->arch.has_run_once)
> +		stage2_unmap_vm(vcpu->kvm);
> +
>  	vcpu_reset_hcr(vcpu);
>  
>  	/*
> diff --git a/arch/arm/kvm/mmu.c b/arch/arm/kvm/mmu.c
> index 57a403a..b1f3c9a 100644
> --- a/arch/arm/kvm/mmu.c
> +++ b/arch/arm/kvm/mmu.c
> @@ -611,6 +611,71 @@ static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
>  	unmap_range(kvm, kvm->arch.pgd, start, size);
>  }
>  
> +static void stage2_unmap_memslot(struct kvm *kvm,
> +				 struct kvm_memory_slot *memslot)
> +{
> +	hva_t hva = memslot->userspace_addr;
> +	phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
> +	phys_addr_t size = PAGE_SIZE * memslot->npages;
> +	hva_t reg_end = hva + size;
> +
> +	/*
> +	 * A memory region could potentially cover multiple VMAs, and any holes
> +	 * between them, so iterate over all of them to find out if we should
> +	 * unmap any of them.
> +	 *
> +	 *     +--------------------------------------------+
> +	 * +---------------+----------------+   +----------------+
> +	 * |   : VMA 1     |      VMA 2     |   |    VMA 3  :    |
> +	 * +---------------+----------------+   +----------------+
> +	 *     |               memory region                |
> +	 *     +--------------------------------------------+
> +	 */
> +	do {
> +		struct vm_area_struct *vma = find_vma(current->mm, hva);
> +		hva_t vm_start, vm_end;
> +
> +		if (!vma || vma->vm_start >= reg_end)
> +			break;
> +
> +		/*
> +		 * Take the intersection of this VMA with the memory region
> +		 */
> +		vm_start = max(hva, vma->vm_start);
> +		vm_end = min(reg_end, vma->vm_end);
> +
> +		if (!(vma->vm_flags & VM_PFNMAP)) {
> +			gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
> +			unmap_stage2_range(kvm, gpa, vm_end - vm_start);
> +		}
> +		hva = vm_end;
> +	} while (hva < reg_end);
> +}
> +
> +/**
> + * stage2_unmap_vm - Unmap Stage-2 RAM mappings
> + * @kvm: The struct kvm pointer
> + *
> + * Go through the memregions and unmap any reguler RAM
> + * backing memory already mapped to the VM.
> + */
> +void stage2_unmap_vm(struct kvm *kvm)
> +{
> +	struct kvm_memslots *slots;
> +	struct kvm_memory_slot *memslot;
> +	int idx;
> +
> +	idx = srcu_read_lock(&kvm->srcu);
> +	spin_lock(&kvm->mmu_lock);
> +
> +	slots = kvm_memslots(kvm);
> +	kvm_for_each_memslot(memslot, slots)
> +		stage2_unmap_memslot(kvm, memslot);
> +
> +	spin_unlock(&kvm->mmu_lock);
> +	srcu_read_unlock(&kvm->srcu, idx);
> +}
> +
>  /**
>   * kvm_free_stage2_pgd - free all stage-2 tables
>   * @kvm:	The KVM struct pointer for the VM.
> diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h
> index 0caf7a5..061fed7 100644
> --- a/arch/arm64/include/asm/kvm_mmu.h
> +++ b/arch/arm64/include/asm/kvm_mmu.h
> @@ -83,6 +83,7 @@ int create_hyp_io_mappings(void *from, void *to, phys_addr_t);
>  void free_boot_hyp_pgd(void);
>  void free_hyp_pgds(void);
>  
> +void stage2_unmap_vm(struct kvm *kvm);
>  int kvm_alloc_stage2_pgd(struct kvm *kvm);
>  void kvm_free_stage2_pgd(struct kvm *kvm);
>  int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
> 

Acked-by: Marc Zyngier <marc.zyngier@arm.com>

	M.
-- 
Jazz is not dead. It just smells funny...

  reply	other threads:[~2014-12-08 12:08 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-03 21:18 [PATCH v2 0/6] Improve PSCI system events and fix reboot bugs Christoffer Dall
2014-12-03 21:18 ` Christoffer Dall
2014-12-03 21:18 ` [PATCH v2 1/6] arm/arm64: KVM: Don't clear the VCPU_POWER_OFF flag Christoffer Dall
2014-12-03 21:18   ` Christoffer Dall
2014-12-08 11:46   ` Marc Zyngier
2014-12-08 11:46     ` Marc Zyngier
2014-12-03 21:18 ` [PATCH v2 2/6] arm/arm64: KVM: Correct KVM_ARM_VCPU_INIT power off option Christoffer Dall
2014-12-03 21:18   ` Christoffer Dall
2014-12-08 11:47   ` Marc Zyngier
2014-12-08 11:47     ` Marc Zyngier
2014-12-03 21:18 ` [PATCH v2 3/6] arm/arm64: KVM: Reset the HCR on each vcpu when resetting the vcpu Christoffer Dall
2014-12-03 21:18   ` Christoffer Dall
2014-12-08 11:49   ` Marc Zyngier
2014-12-08 11:49     ` Marc Zyngier
2014-12-03 21:18 ` [PATCH v2 4/6] arm/arm64: KVM: Clarify KVM_ARM_VCPU_INIT ABI Christoffer Dall
2014-12-03 21:18   ` Christoffer Dall
2014-12-08 11:52   ` Marc Zyngier
2014-12-08 11:52     ` Marc Zyngier
2014-12-03 21:18 ` [PATCH v2 5/6] arm/arm64: KVM: Turn off vcpus on PSCI shutdown/reboot Christoffer Dall
2014-12-03 21:18   ` Christoffer Dall
2014-12-08 12:04   ` Marc Zyngier
2014-12-08 12:04     ` Marc Zyngier
2014-12-08 12:58     ` Christoffer Dall
2014-12-08 12:58       ` Christoffer Dall
2014-12-08 13:19       ` Marc Zyngier
2014-12-08 13:19         ` Marc Zyngier
2014-12-12 19:42         ` Christoffer Dall
2014-12-12 19:42           ` Christoffer Dall
2014-12-12 19:49         ` Christoffer Dall
2014-12-12 19:49           ` Christoffer Dall
2014-12-12 21:04           ` Marc Zyngier
2014-12-12 21:04             ` Marc Zyngier
2014-12-03 21:18 ` [PATCH v2 6/6] arm/arm64: KVM: Introduce stage2_unmap_vm Christoffer Dall
2014-12-03 21:18   ` Christoffer Dall
2014-12-08 12:08   ` Marc Zyngier [this message]
2014-12-08 12:08     ` Marc Zyngier
2014-12-05 17:24 ` [PATCH v2 0/6] Improve PSCI system events and fix reboot bugs Andrew Jones
2014-12-05 17:24   ` Andrew Jones
2014-12-08 11:24 ` Peter Maydell
2014-12-08 11:24   ` Peter Maydell

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=548594B3.8040507@arm.com \
    --to=marc.zyngier@arm.com \
    --cc=linux-arm-kernel@lists.infradead.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.