From patchwork Mon Nov 17 14:58:53 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 5322251 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 786419F1E1 for ; Mon, 17 Nov 2014 14:59:07 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A079A20176 for ; Mon, 17 Nov 2014 14:59:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D02C820173 for ; Mon, 17 Nov 2014 14:59:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752984AbaKQO7B (ORCPT ); Mon, 17 Nov 2014 09:59:01 -0500 Received: from mail-wg0-f49.google.com ([74.125.82.49]:53219 "EHLO mail-wg0-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752941AbaKQO66 (ORCPT ); Mon, 17 Nov 2014 09:58:58 -0500 Received: by mail-wg0-f49.google.com with SMTP id x13so24916462wgg.22 for ; Mon, 17 Nov 2014 06:58:57 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=/AwN2+nP93DpAr9qaAe6P+xsB/RbxYAQxC0SN/jg6Rw=; b=Sbw9wYdBqtzuAfF/QV0kJ6J/370ntdRatr2ygEuxjcBwhdYxYFgHMgyUofZixRi0Bd yXo7IzRzLpflxbQLRR3QO3AsqzJoMlAl1OBbo0zv57LsPYRhQ7twQhc0eF2vZwgRraJ8 Z8xuwQNhYd4HWhH7x1QgbMjAAYa5LocS5JSRmKBMlqIgzs/9SYrqNPy6OCDpWvFOUJlN OuL0R53rfUvBGI2OPHAxGZkrbH09gM0yRBgNhaLCHCCJj3RkhFXjGCPstzuM2pYx8PwH d0OJ6kH4P+0zdrWhsUoMt56UXroVKwx2iZw/MMyArBERT2mHNwVFaocR+1bDNwQ7In32 bmgg== X-Gm-Message-State: ALoCoQmWT5KcOX9t3B1F1VSMX/zfoLWT8DbSEjGWKePdwtgr6w8qz3NqdMbyAz/gL4rmUr88pl74 X-Received: by 10.180.84.5 with SMTP id u5mr32723379wiy.12.1416236337163; Mon, 17 Nov 2014 06:58:57 -0800 (PST) Received: from ards-macbook-pro.local (cag06-7-83-153-85-71.fbx.proxad.net. [83.153.85.71]) by mx.google.com with ESMTPSA id n3sm8751786wjz.21.2014.11.17.06.58.55 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 17 Nov 2014 06:58:56 -0800 (PST) From: Ard Biesheuvel To: kvmarm@lists.cs.columbia.edu, christoffer.dall@linaro.org, marc.zyngier@arm.com, lersek@redhat.com, drjones@redhat.com, wei@redhat.com Cc: kvm@vger.kernel.org, Ard Biesheuvel Subject: [PATCH 3/3] arm, arm64: KVM: handle potential incoherency of readonly memslots Date: Mon, 17 Nov 2014 15:58:53 +0100 Message-Id: <1416236333-9378-3-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1416236333-9378-1-git-send-email-ard.biesheuvel@linaro.org> References: <1416236333-9378-1-git-send-email-ard.biesheuvel@linaro.org> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Readonly memslots are often used to implement emulation of ROMs and NOR flashes, in which case the guest may legally map these regions as uncached. To deal with the incoherency associated with uncached guest mappings, treat all readonly memslots as incoherent, and ensure that pages that belong to regions tagged as such are flushed to DRAM before being passed to the guest. Signed-off-by: Ard Biesheuvel Acked-by: Paolo Bonzini Acked-by: Paolo Bonzini --- arch/arm/kvm/mmu.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/arch/arm/kvm/mmu.c b/arch/arm/kvm/mmu.c index cb924c6d56a6..f2a9874ff5cb 100644 --- a/arch/arm/kvm/mmu.c +++ b/arch/arm/kvm/mmu.c @@ -919,7 +919,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, if (!hugetlb && !force_pte) hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa); - fault_ipa_uncached = false; + fault_ipa_uncached = memslot->flags & KVM_MEMSLOT_INCOHERENT; if (hugetlb) { pmd_t new_pmd = pfn_pmd(pfn, mem_type); @@ -1298,11 +1298,12 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, hva = vm_end; } while (hva < reg_end); - if (ret) { - spin_lock(&kvm->mmu_lock); + spin_lock(&kvm->mmu_lock); + if (ret) unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size); - spin_unlock(&kvm->mmu_lock); - } + else + stage2_flush_memslot(kvm, memslot); + spin_unlock(&kvm->mmu_lock); return ret; } @@ -1314,6 +1315,15 @@ void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free, int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, unsigned long npages) { + /* + * Readonly memslots are not incoherent with the caches by definition, + * but in practice, they are used mostly to emulate ROMs or NOR flashes + * that the guest may consider devices and hence map as uncached. + * To prevent incoherency issues in these cases, tag all readonly + * regions as incoherent. + */ + if (slot->flags & KVM_MEM_READONLY) + slot->flags |= KVM_MEMSLOT_INCOHERENT; return 0; }