linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jiri Slaby <jslaby@suse.cz>
To: stable@vger.kernel.org
Cc: linux-kernel@vger.kernel.org,
	Christoffer Dall <christoffer.dall@linaro.org>,
	Shannon Zhao <shannon.zhao@linaro.org>,
	Jiri Slaby <jslaby@suse.cz>
Subject: [PATCH 3.12 57/63] arm/arm64: KVM: Introduce stage2_unmap_vm
Date: Thu, 30 Apr 2015 14:12:26 +0200	[thread overview]
Message-ID: <fc234577d5802f898551d89b38650cab4c98ed12.1430387326.git.jslaby@suse.cz> (raw)
In-Reply-To: <45aaf85687dd6ac119c55c5ec0dbe0bef0e62235.1430387326.git.jslaby@suse.cz>
In-Reply-To: <cover.1430387326.git.jslaby@suse.cz>

From: Christoffer Dall <christoffer.dall@linaro.org>

3.12-stable review patch.  If anyone has any objections, please let me know.

===============

commit 957db105c99792ae8ef61ffc9ae77d910f6471da upstream.

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.

Acked-by: Marc Zyngier <marc.zyngier@arm.com>
Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>
Signed-off-by: Shannon Zhao <shannon.zhao@linaro.org>
Signed-off-by: Jiri Slaby <jslaby@suse.cz>
---
 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 17b93071bb17..8cd885699420 100644
--- a/arch/arm/include/asm/kvm_mmu.h
+++ b/arch/arm/include/asm/kvm_mmu.h
@@ -47,6 +47,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 8f4761b5af85..d1c5946e33a2 100644
--- a/arch/arm/kvm/arm.c
+++ b/arch/arm/kvm/arm.c
@@ -673,6 +673,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 5c31e3fff597..a79baa59fe15 100644
--- a/arch/arm/kvm/mmu.c
+++ b/arch/arm/kvm/mmu.c
@@ -528,6 +528,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 5966ad5a356f..6e127e7ca687 100644
--- a/arch/arm64/include/asm/kvm_mmu.h
+++ b/arch/arm64/include/asm/kvm_mmu.h
@@ -74,6 +74,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,
-- 
2.3.5


  parent reply	other threads:[~2015-04-30 12:14 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-30 12:12 [PATCH 3.12 00/63] 3.12.42-stable review Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 01/63] KVM: ARM: Fix calculation of virtual CPU ID Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 02/63] KVM: ARM: fix the size of TTBCR_{T0SZ,T1SZ} masks Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 03/63] ARM: KVM: Yield CPU when vcpu executes a WFE Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 04/63] ARM: KVM: Fix MPIDR computing to support virtual clusters Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 05/63] KVM: ARM: Update comments for kvm_handle_wfi Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 06/63] ARM: KVM: fix L2CTLR to be per-cluster Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 07/63] arm/arm64: KVM: PSCI: use MPIDR to identify a target CPU Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 08/63] arm64: KVM: Yield CPU when vcpu executes a WFE Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 09/63] arm/arm64: KVM: arch_timer: Initialize cntvoff at kvm_init Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 10/63] ARM: KVM: Allow creating the VGIC after VCPUs Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 11/63] arm/arm64: kvm: Set vcpu->cpu to -1 on vcpu_put Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 12/63] arm64: KVM: Force undefined exception for Guest SMC intructions Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 13/63] arm: KVM: Don't return PSCI_INVAL if waitqueue is inactive Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 14/63] KVM: ARM: Remove duplicate include Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 15/63] arm: kvm: implement CPU PM notifier Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 16/63] arm/arm64: KVM: detect CPU reset on CPU_PM_EXIT Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 17/63] arm64: KVM: force cache clean on page fault when caches are off Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 18/63] arm64: KVM: allows discrimination of AArch32 sysreg access Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 19/63] arm64: KVM: trap VM system registers until MMU and caches are ON Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 20/63] ARM: KVM: introduce kvm_p*d_addr_end Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 21/63] arm64: KVM: flush VM pages before letting the guest enable caches Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 22/63] ARM: KVM: force cache clean on page fault when caches are off Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 23/63] ARM: KVM: fix handling of trapped 64bit coprocessor accesses Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 24/63] ARM: KVM: fix ordering of " Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 25/63] ARM: KVM: introduce per-vcpu HYP Configuration Register Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 26/63] ARM: KVM: add world-switch for AMAIR{0,1} Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 27/63] ARM: KVM: trap VM system registers until MMU and caches are ON Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 28/63] ARM: KVM: fix non-VGIC compilation Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 29/63] ARM: KVM: disable KVM in Kconfig on big-endian systems Jiri Slaby
2015-04-30 12:11 ` [PATCH 3.12 30/63] KVM: arm/arm64: vgic: fix GICD_ICFGR register accesses Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 31/63] KVM: ARM: vgic: Fix the overlap check action about setting the GICD & GICC base address Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 32/63] arm64: kvm: use inner-shareable barriers for inner-shareable maintenance Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 33/63] kvm: arm64: vgic: fix hyp panic with 64k pages on juno platform Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 34/63] arm/arm64: KVM: Fix and refactor unmap_range Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 35/63] ARM: KVM: Unmap IPA on memslot delete/move Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 36/63] ARM: KVM: user_mem_abort: support stage 2 MMIO page mapping Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 37/63] arm64: KVM: export demux regids as KVM_REG_ARM64 Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 38/63] ARM: virt: fix wrong HSCTLR.EE bit setting Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 39/63] ARM64: KVM: store kvm_vcpu_fault_info est_el2 as word Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 40/63] KVM: ARM/arm64: fix non-const declaration of function returning const Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 41/63] KVM: ARM/arm64: fix broken __percpu annotation Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 42/63] KVM: ARM/arm64: avoid returning negative error code as bool Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 43/63] KVM: vgic: return int instead of bool when checking I/O ranges Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 44/63] ARM/ARM64: KVM: Nuke Hyp-mode tlbs before enabling MMU Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 45/63] arm/arm64: KVM: Complete WFI/WFE instructions Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 46/63] ARM/arm64: KVM: fix use of WnR bit in kvm_is_write_fault() Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 47/63] KVM: ARM: vgic: plug irq injection race Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 48/63] arm/arm64: KVM: Fix VTTBR_BADDR_MASK and pgd alloc Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 49/63] arm: kvm: fix CPU hotplug Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 50/63] arm/arm64: KVM: Ensure memslots are within KVM_PHYS_SIZE Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 51/63] arm: kvm: STRICT_MM_TYPECHECKS fix for user_mem_abort Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 52/63] arm64/kvm: Fix assembler compatibility of macros Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 53/63] arm/arm64: kvm: drop inappropriate use of kvm_is_mmio_pfn() Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 54/63] arm/arm64: KVM: Don't clear the VCPU_POWER_OFF flag Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 55/63] arm/arm64: KVM: Correct KVM_ARM_VCPU_INIT power off option Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 56/63] arm/arm64: KVM: Reset the HCR on each vcpu when resetting the vcpu Jiri Slaby
2015-04-30 12:12 ` Jiri Slaby [this message]
2015-04-30 12:12 ` [PATCH 3.12 58/63] arm/arm64: KVM: Don't allow creating VCPUs after vgic_initialized Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 59/63] arm/arm64: KVM: Require in-kernel vgic for the arch timers Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 60/63] arm64: KVM: Fix TLB invalidation by IPA/VMID Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 61/63] arm64: KVM: Fix HCR setting for 32bit guests Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 62/63] arm64: KVM: Do not use pgd_index to index stage-2 pgd Jiri Slaby
2015-04-30 12:12 ` [PATCH 3.12 63/63] arm/arm64: KVM: Keep elrsr/aisr in sync with software model Jiri Slaby
2015-04-30 13:12 ` [PATCH 3.12 00/63] 3.12.42-stable review Guenter Roeck
2015-05-04 13:27   ` Jiri Slaby
2015-04-30 14:26 ` Shuah Khan

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=fc234577d5802f898551d89b38650cab4c98ed12.1430387326.git.jslaby@suse.cz \
    --to=jslaby@suse.cz \
    --cc=christoffer.dall@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=shannon.zhao@linaro.org \
    --cc=stable@vger.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 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).