Received: by 10.192.165.156 with SMTP id m28csp3011388imm; Sun, 15 Apr 2018 14:58:47 -0700 (PDT) X-Google-Smtp-Source: AIpwx48z5ZQxBqw5T8lZFhvlHXxU/HBhmaPM2pIaDHUPvZJMb9rJ0Ty139N7gWwXO7woPdkPxTta X-Received: by 10.99.117.29 with SMTP id q29mr3865869pgc.269.1523829527050; Sun, 15 Apr 2018 14:58:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523829527; cv=none; d=google.com; s=arc-20160816; b=WlmvjcdnJPvtdE+YawZv8NiISa0rQlWzDYJODXafcaj1eqh47mBsOkg+Nvc7PFkW2u djJtT8y+HEJrvLHG9oapv8nNbTaPNwOoqbdpFwTYdEApvuae/JCa8IgDwhpdhZHchGM6 3hiFNINLKHbZWtReAMLS7mSdnpfLq7tYgQDiqLpaIt/kw1GP8eMzhgnipREIIP+U6aJK YwU8H91LVY8ffiAzSsNApPBrLpGTsflD2bzowPOMHsYsamJg+4Qnfm8ZygS8fG0Iz46J K0ffjhx0SwpJoiETy+19cLJ7zdxFiopDI1dgFVJbbP9KDGwCjqQjc08jehZPuBWY1SyL b3zw== 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:arc-authentication-results; bh=tZb6UvWtJdkQyBMAdd4QCdXSlqJBimfcS+7xGEqFH74=; b=ltHmcf6jtcdmrTl1DUzgdkKJGFnAayko2XKxRgCINPk9dl5zn4pgOLTUaMsqAhRCOS IeYWoWYMgHBqmgsTGp43y2aHwIm3HiWQA9afe14s80gP63zl8OZcBAE5P8AbYZw9pVQp GvjemMj+hiCm2LMU1hq35rPn2fIePLGmysdNU6/CYEnZUS+/6UBXSHwmmGJgGD3amATG zyYd5sUyx1jyAOOLCrVUofrwBL/WCz/5nxIL5l8vSLapVI9h6dTNNkFSPor6aSXsnD0b sZzrG2NTqqIGF439DtnzSDM0B+8J9ZEMrv4Yl78PRpShqwUrugYY4ie3c7SqCCyzyKDo u5Ow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.de header.s=amazon201209 header.b=sKNINFtY; 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 f4-v6si5731317plf.543.2018.04.15.14.58.14; Sun, 15 Apr 2018 14:58:47 -0700 (PDT) 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=sKNINFtY; 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 S1753162AbeDOV4T (ORCPT + 99 others); Sun, 15 Apr 2018 17:56:19 -0400 Received: from smtp-fw-33001.amazon.com ([207.171.190.10]:7225 "EHLO smtp-fw-33001.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752777AbeDOVzB (ORCPT ); Sun, 15 Apr 2018 17:55:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1523829301; x=1555365301; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=tZb6UvWtJdkQyBMAdd4QCdXSlqJBimfcS+7xGEqFH74=; b=sKNINFtYBHytF4HAeREm9l05QVSV+dDIlLUcs4xUF/vvJ/x2TIfH8Rkc GFVTBG8RxuaHIsZ/9E2Ct6wJDrHU4wTuoOFKtIVwJ8H6UUeBMqUqhF1NS PKhzMwaZ6uNta1kOOXMlDZXvYwrn4yFdOjUaqamgcDdrNTEn/FoZBM5Za E=; X-IronPort-AV: E=Sophos;i="5.48,456,1517875200"; d="scan'208";a="726275413" Received: from sea3-co-svc-lb6-vlan2.sea.amazon.com (HELO email-inbound-relay-2c-1968f9fa.us-west-2.amazon.com) ([10.47.22.34]) by smtp-border-fw-out-33001.sea14.amazon.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 15 Apr 2018 21:54:54 +0000 Received: from u54e1ad5160425a4b64ea.ant.amazon.com (pdx2-ws-svc-lb17-vlan2.amazon.com [10.247.140.66]) by email-inbound-relay-2c-1968f9fa.us-west-2.amazon.com (8.14.7/8.14.7) with ESMTP id w3FLsoUx002884 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 15 Apr 2018 21:54:52 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 w3FLsn6K013432; Sun, 15 Apr 2018 23:54:50 +0200 Received: (from karahmed@localhost) by u54e1ad5160425a4b64ea.ant.amazon.com (8.15.2/8.15.2/Submit) id w3FLsnSu013431; Sun, 15 Apr 2018 23:54:49 +0200 From: KarimAllah Ahmed To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, pbonzini@redhat.com, rkrcmar@redhat.com Cc: KarimAllah Ahmed Subject: [PATCH v2 05/12] KVM: Introduce a new guest mapping API Date: Sun, 15 Apr 2018 23:53:11 +0200 Message-Id: <1523829198-13236-6-git-send-email-karahmed@amazon.de> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1523829198-13236-1-git-send-email-karahmed@amazon.de> References: <1523829198-13236-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". Keep in mind that memremap is horribly slow, so this mapping API should not be used for high-frequency mapping operations. But rather for low-frequency mappings. Signed-off-by: KarimAllah Ahmed --- 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 | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index fe4f46b..15b9244 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -204,6 +204,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. @@ -700,6 +707,8 @@ 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); +void kvm_vcpu_unmap(struct kvm_host_map *map); struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn); 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); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index c7b2e92..70c3e56 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1631,6 +1631,56 @@ 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) +{ + if (!map->hva) + return; + + if (map->page) + kunmap(map->page); + else + memunmap(map->hva); + + kvm_release_pfn_dirty(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