Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp561711imu; Wed, 9 Jan 2019 02:31:09 -0800 (PST) X-Google-Smtp-Source: ALg8bN4A0iOriziiMUwkk2lBZZpHj/ba88kMj5iexEKPtawlNqNqFLGVs7DOrsvTGC8cP8kq4S0t X-Received: by 2002:a62:140a:: with SMTP id 10mr5292511pfu.157.1547029869721; Wed, 09 Jan 2019 02:31:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547029869; cv=none; d=google.com; s=arc-20160816; b=GeriJOujlJgtcqSfWel1oTTDUpiHTa9A1zcnqVT4wwJbwUOk96A1yiQxsCGRV54ArU WODIDxUIeAqKPdqL0hFWbcjIJ6Zgqv3OFlxXD4HXTzs2sCflxdTOX7ual3gMYzV5CAL2 Q/owpNDgdjaZsp2DNwcG+N6G0vvpQfYsVRh9zOgjNIhvyf5mo1mjtOf/AdXy8YQXB3sY vzAYlczwwfRS6H7GbIvBdPW06hN3BseabO4nG2Ul+srx4NddYhETNtUU8vZS14ytmoH8 BEMNsY/ta9OHNHLr2QUhxcZZcu1E84bHXPZUFhd2OYFEt7SgclyAuiuXnwGYRdI8/oAK Rxhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=Znhnp2Ij9KE4Xv9eLLSuWtVI/Wo9TxZTgtm0b0dFmCM=; b=G+JLPaQS4FOfvnQTugBul7UHu9URiADWinBv05Q6GWXNlG0MPih7VYulrlkYWASGKA IzsQBh/IFxnXG8WcXsJcRo07/4QhKMclYnqRnvdUVbfVzKXnufz5fxREkMARzXPOWl0N 6JKZABVldlmM5KPIvkFcSLsvY+OBmxehEBKgn2WATrcuZ08gy2jq22IjhHp16RfqdgrK MYKvb+cGNElhqbwd7/Z2wiZVxErnTe+yAMtd3hL0w/mTkRDXLx+upaL6r3mHGSkKpG17 WDuE34H+QzNr/6I/Xtl1sqMYMYUdF0P9iGedISpkQtLyY9CTtydnyIuBdfRlSBrmfJ5+ 908g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.de header.s=amazon201209 header.b="ugIis/uk"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o5si7208996plk.360.2019.01.09.02.30.54; Wed, 09 Jan 2019 02:31:09 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.de header.s=amazon201209 header.b="ugIis/uk"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730550AbfAIJmf (ORCPT + 99 others); Wed, 9 Jan 2019 04:42:35 -0500 Received: from smtp-fw-9101.amazon.com ([207.171.184.25]:61119 "EHLO smtp-fw-9101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730434AbfAIJm2 (ORCPT ); Wed, 9 Jan 2019 04:42:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1547026947; x=1578562947; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=Znhnp2Ij9KE4Xv9eLLSuWtVI/Wo9TxZTgtm0b0dFmCM=; b=ugIis/ukKHT8vnRTWbdyaoLNf1B5Fl2Ba+6dR8WVbtylNQdiMKJvBK9t DndF/ubwgj+C/YSzsFDwKQdrq2p1FkUtA19U30tUFyVNkD5Og07GgPpCc 10VFzxyzTe9jxlpaSPwYtcunsPa/1PtwL+v3cHU8yFyGGGpLEOFFpNxAs 4=; X-IronPort-AV: E=Sophos;i="5.56,253,1539648000"; d="scan'208";a="780512362" Received: from sea3-co-svc-lb6-vlan3.sea.amazon.com (HELO email-inbound-relay-2b-4ff6265a.us-west-2.amazon.com) ([10.47.22.38]) by smtp-border-fw-out-9101.sea19.amazon.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 09 Jan 2019 09:42:22 +0000 Received: from u54e1ad5160425a4b64ea.ant.amazon.com (pdx2-ws-svc-lb17-vlan2.amazon.com [10.247.140.66]) by email-inbound-relay-2b-4ff6265a.us-west-2.amazon.com (8.14.7/8.14.7) with ESMTP id x099gIPj119306 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 9 Jan 2019 09:42:20 GMT Received: from u54e1ad5160425a4b64ea.ant.amazon.com (localhost [127.0.0.1]) by u54e1ad5160425a4b64ea.ant.amazon.com (8.15.2/8.15.2/Debian-3) with ESMTP id x099gICM031307; Wed, 9 Jan 2019 10:42:18 +0100 Received: (from karahmed@localhost) by u54e1ad5160425a4b64ea.ant.amazon.com (8.15.2/8.15.2/Submit) id x099gHGZ031306; Wed, 9 Jan 2019 10:42:17 +0100 From: KarimAllah Ahmed To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, pbonzini@redhat.com, rkrcmar@redhat.com Cc: KarimAllah Ahmed Subject: [PATCH v5 04/13] KVM: Introduce a new guest mapping API Date: Wed, 9 Jan 2019 10:42:04 +0100 Message-Id: <1547026933-31226-5-git-send-email-karahmed@amazon.de> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1547026933-31226-1-git-send-email-karahmed@amazon.de> References: <1547026933-31226-1-git-send-email-karahmed@amazon.de> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In KVM, specially for nested guests, there is a dominant pattern of: => map guest memory -> do_something -> unmap guest memory In addition to all this unnecessarily noise in the code due to boiler plate code, most of the time the mapping function does not properly handle memory that is not backed by "struct page". This new guest mapping API encapsulate most of this boiler plate code and also handles guest memory that is not backed by "struct page". The current implementation of this API is using memremap for memory that is not backed by a "struct page" which would lead to a huge slow-down if it was used for high-frequency mapping operations. The API does not have any effect on current setups where guest memory is backed by a "struct page". Further patches are going to also introduce a pfn-cache which would significantly improve the performance of the memremap case. Signed-off-by: KarimAllah Ahmed --- v3 -> v4: - Update the commit message. v1 -> v2: - Drop the caching optimization (pbonzini) - Use 'hva' instead of 'kaddr' (pbonzini) - Return 0/-EINVAL/-EFAULT instead of true/false. -EFAULT will be used for AMD patch (pbonzini) - Introduce __kvm_map_gfn which accepts a memory slot and use it (pbonzini) - Only clear map->hva instead of memsetting the whole structure. - Drop kvm_vcpu_map_valid since it is no longer used. - Fix EXPORT_MODULE naming. --- include/linux/kvm_host.h | 9 ++++++++ virt/kvm/kvm_main.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index c38cc5e..8a2f5fa 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -205,6 +205,13 @@ enum { READING_SHADOW_PAGE_TABLES, }; +struct kvm_host_map { + struct page *page; + void *hva; + kvm_pfn_t pfn; + kvm_pfn_t gfn; +}; + /* * Sometimes a large or cross-page mmio needs to be broken up into separate * exits for userspace servicing. @@ -710,7 +717,9 @@ struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu); struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn); kvm_pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn); kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn); +int kvm_vcpu_map(struct kvm_vcpu *vcpu, gpa_t gpa, struct kvm_host_map *map); struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn); +void kvm_vcpu_unmap(struct kvm_host_map *map, bool dirty); unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn); unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable); int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data, int offset, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 1f888a1..4d8f2e3 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1733,6 +1733,59 @@ struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) } EXPORT_SYMBOL_GPL(gfn_to_page); +static int __kvm_map_gfn(struct kvm_memory_slot *slot, gfn_t gfn, + struct kvm_host_map *map) +{ + kvm_pfn_t pfn; + void *hva = NULL; + struct page *page = NULL; + + pfn = gfn_to_pfn_memslot(slot, gfn); + if (is_error_noslot_pfn(pfn)) + return -EINVAL; + + if (pfn_valid(pfn)) { + page = pfn_to_page(pfn); + hva = kmap(page); + } else { + hva = memremap(pfn_to_hpa(pfn), PAGE_SIZE, MEMREMAP_WB); + } + + if (!hva) + return -EFAULT; + + map->page = page; + map->hva = hva; + map->pfn = pfn; + map->gfn = gfn; + + return 0; +} + +int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map) +{ + return __kvm_map_gfn(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn, map); +} +EXPORT_SYMBOL_GPL(kvm_vcpu_map); + +void kvm_vcpu_unmap(struct kvm_host_map *map, bool dirty) +{ + if (!map->hva) + return; + + if (map->page) + kunmap(map->page); + else + memunmap(map->hva); + + if (dirty) + kvm_release_pfn_dirty(map->pfn); + else + kvm_release_pfn_clean(map->pfn); + map->hva = NULL; +} +EXPORT_SYMBOL_GPL(kvm_vcpu_unmap); + struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn) { kvm_pfn_t pfn; -- 2.7.4