From nobody Sun May 5 00:53:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1630084909; cv=none; d=zohomail.com; s=zohoarc; b=czrYBVaH1iWDxHLYFfAvdPXL25B6oQsPSmhq12nNRIuQde3riL7zSHTsXZFF6cYMIRloWPZiPm992x1++ZaVwv5VfBmVExO3KrKWSWAPcDjnao1/Sqio6fc23p+JjYlQtAgBoVILfLV78CV+6FicdO+tItF8sOSbfXp6NzQbdgA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630084909; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=8Nk88C9e1YSHytuy/urYiBm6Fh5qFSuemVpCrvWv3wM=; b=Mmqg46qw9aO61Kdmjwhj0eik+KU+2IoRhXGP0o6uhCQAFWihTBbcbfAOwUSthwFlWWEagplsYhNL2IrSupABy+YueJqm0A0foCr5WyBUH4GudfFgvH9FZjWTy5/XRNRqrsSWFIRGwk8YmPQHpNl9z8sRDVHhjP6Zgifah71olms= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1630084909685575.8417574463339; Fri, 27 Aug 2021 10:21:49 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.173801.317101 (Exim 4.92) (envelope-from ) id 1mJfXk-0001NR-Hm; Fri, 27 Aug 2021 17:21:28 +0000 Received: by outflank-mailman (output) from mailman id 173801.317101; Fri, 27 Aug 2021 17:21:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfXk-0001NK-En; Fri, 27 Aug 2021 17:21:28 +0000 Received: by outflank-mailman (input) for mailman id 173801; Fri, 27 Aug 2021 17:21:27 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfXj-00016Q-Ml for xen-devel@lists.xenproject.org; Fri, 27 Aug 2021 17:21:27 +0000 Received: from mail-pg1-x534.google.com (unknown [2607:f8b0:4864:20::534]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id a8e4df1c-2153-4e7c-b0a1-865dfab1a0ac; Fri, 27 Aug 2021 17:21:24 +0000 (UTC) Received: by mail-pg1-x534.google.com with SMTP id w8so6495932pgf.5 for ; Fri, 27 Aug 2021 10:21:24 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:36:ef50:8fcd:44d1:eb17]) by smtp.gmail.com with ESMTPSA id f5sm7155015pjo.23.2021.08.27.10.21.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Aug 2021 10:21:23 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: a8e4df1c-2153-4e7c-b0a1-865dfab1a0ac DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8Nk88C9e1YSHytuy/urYiBm6Fh5qFSuemVpCrvWv3wM=; b=db5MNBSjoza6wsbnzpTXcqhXMWNAkPil3VzTzRkr8eab8nRn2vsvGPetAqygAw9Oti z9PQvIHMo6mHcaqWgvsteCbwKuLYm5f3eLHVWICjJlCTf6EK3HrUHXv9NRulvSbupxpB sJCrCbtW71aQAWI7MFFPYOvcSuzX4caFNCQXBsgoBxwA+2lUG0w6wj0SNE6/dFS/ZZMN fZWAla5uWMXjB9Y5JLmibogA2NGNW9mHorSwWfKfZL+QylXBH0ubW+UylhiSONw5dBIT raikB2e0Z44S56mI5Y4m5Htbrd0tHhDr5TIhkbA8+U5dincKUJsV/ZJRk3QlCwnvE2FZ 756w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8Nk88C9e1YSHytuy/urYiBm6Fh5qFSuemVpCrvWv3wM=; b=rNKj4aX5E6osTC+AJKceX0mQOD3bVIvzFF5nlCAardJ7QjlnP13CVBq79rh9qyQrqt xOiO+8b/wNBhHRu3ocbJSHs5gTZJMsudIl9WGZswfnJGPwDpDmbEp+ybEDgsjEzo6T// Ihnlv9q/QYD4Q0e+oY7aVJ2rZd55PSKoNlOcqLrteSU05CC2f6yLaE6tdImUTzjGub9k 6omw8/UCaAnaHk+bpw5DXyd37Awkg3ZWrfpCExNmYSX4OrOpxJLBuQA5MzvJab5eiiJl FGsL3Q7k8ILO0clabHitc1FRyIILELeSq0QqOYU3U7PewH0vi/DjGbfyGaCjvSFgTCki lOwA== X-Gm-Message-State: AOAM532Ccm3HiqRt9OIi7kOpwr7SG3g3RFylvngvjb34EXwhPRWNUpwG oxKE+bEcokzXGXSJ1tHo06A= X-Google-Smtp-Source: ABdhPJyHLaXpMtlT/ZlRFeVCoAbs+0dWwpmbMrqQ/Sjs8GeDF6jIbsyeb/ptNZwnBj40znc0QMZmfA== X-Received: by 2002:a63:fc0a:: with SMTP id j10mr8717240pgi.136.1630084884095; Fri, 27 Aug 2021 10:21:24 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, catalin.marinas@arm.com, will@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, gregkh@linuxfoundation.org, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, brijesh.singh@amd.com, thomas.lendacky@amd.com, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, hannes@cmpxchg.org, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, linux-arm-kernel@lists.infradead.org, xen-devel@lists.xenproject.org, rientjes@google.com, ardb@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V4 01/13] x86/hyperv: Initialize GHCB page in Isolation VM Date: Fri, 27 Aug 2021 13:20:59 -0400 Message-Id: <20210827172114.414281-2-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210827172114.414281-1-ltykernel@gmail.com> References: <20210827172114.414281-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1630084911228100001 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan Hyperv exposes GHCB page via SEV ES GHCB MSR for SNP guest to communicate with hypervisor. Map GHCB page for all cpus to read/write MSR register and submit hvcall request via ghcb page. Signed-off-by: Tianyu Lan --- Chagne since v3: * Rename ghcb_base to hv_ghcb_pg and move it out of struct ms_hyperv_info. * Allocate hv_ghcb_pg before cpuhp_setup_state() and leverage hv_cpu_init() to initialize ghcb page. --- arch/x86/hyperv/hv_init.c | 68 +++++++++++++++++++++++++++++---- arch/x86/include/asm/mshyperv.h | 4 ++ arch/x86/kernel/cpu/mshyperv.c | 3 ++ include/asm-generic/mshyperv.h | 1 + 4 files changed, 69 insertions(+), 7 deletions(-) diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c index 708a2712a516..eba10ed4f73e 100644 --- a/arch/x86/hyperv/hv_init.c +++ b/arch/x86/hyperv/hv_init.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -36,12 +37,42 @@ EXPORT_SYMBOL_GPL(hv_current_partition_id); void *hv_hypercall_pg; EXPORT_SYMBOL_GPL(hv_hypercall_pg); =20 +void __percpu **hv_ghcb_pg; + /* Storage to save the hypercall page temporarily for hibernation */ static void *hv_hypercall_pg_saved; =20 struct hv_vp_assist_page **hv_vp_assist_page; EXPORT_SYMBOL_GPL(hv_vp_assist_page); =20 +static int hyperv_init_ghcb(void) +{ + u64 ghcb_gpa; + void *ghcb_va; + void **ghcb_base; + + if (!hv_isolation_type_snp()) + return 0; + + if (!hv_ghcb_pg) + return -EINVAL; + + /* + * GHCB page is allocated by paravisor. The address + * returned by MSR_AMD64_SEV_ES_GHCB is above shared + * ghcb boundary and map it here. + */ + rdmsrl(MSR_AMD64_SEV_ES_GHCB, ghcb_gpa); + ghcb_va =3D memremap(ghcb_gpa, HV_HYP_PAGE_SIZE, MEMREMAP_WB); + if (!ghcb_va) + return -ENOMEM; + + ghcb_base =3D (void **)this_cpu_ptr(hv_ghcb_pg); + *ghcb_base =3D ghcb_va; + + return 0; +} + static int hv_cpu_init(unsigned int cpu) { union hv_vp_assist_msr_contents msr =3D { 0 }; @@ -85,7 +116,7 @@ static int hv_cpu_init(unsigned int cpu) } } =20 - return 0; + return hyperv_init_ghcb(); } =20 static void (*hv_reenlightenment_cb)(void); @@ -177,6 +208,14 @@ static int hv_cpu_die(unsigned int cpu) { struct hv_reenlightenment_control re_ctrl; unsigned int new_cpu; + void **ghcb_va; + + if (hv_ghcb_pg) { + ghcb_va =3D (void **)this_cpu_ptr(hv_ghcb_pg); + if (*ghcb_va) + memunmap(*ghcb_va); + *ghcb_va =3D NULL; + } =20 hv_common_cpu_die(cpu); =20 @@ -366,10 +405,16 @@ void __init hyperv_init(void) goto common_free; } =20 + if (hv_isolation_type_snp()) { + hv_ghcb_pg =3D alloc_percpu(void *); + if (!hv_ghcb_pg) + goto free_vp_assist_page; + } + cpuhp =3D cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online", hv_cpu_init, hv_cpu_die); if (cpuhp < 0) - goto free_vp_assist_page; + goto free_ghcb_page; =20 /* * Setup the hypercall page and enable hypercalls. @@ -383,10 +428,8 @@ void __init hyperv_init(void) VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX, VM_FLUSH_RESET_PERMS, NUMA_NO_NODE, __builtin_return_address(0)); - if (hv_hypercall_pg =3D=3D NULL) { - wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); - goto remove_cpuhp_state; - } + if (hv_hypercall_pg =3D=3D NULL) + goto clean_guest_os_id; =20 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); hypercall_msr.enable =3D 1; @@ -456,8 +499,11 @@ void __init hyperv_init(void) hv_query_ext_cap(0); return; =20 -remove_cpuhp_state: +clean_guest_os_id: + wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); cpuhp_remove_state(cpuhp); +free_ghcb_page: + free_percpu(hv_ghcb_pg); free_vp_assist_page: kfree(hv_vp_assist_page); hv_vp_assist_page =3D NULL; @@ -559,3 +605,11 @@ bool hv_is_isolation_supported(void) { return hv_get_isolation_type() !=3D HV_ISOLATION_TYPE_NONE; } + +DEFINE_STATIC_KEY_FALSE(isolation_type_snp); + +bool hv_isolation_type_snp(void) +{ + return static_branch_unlikely(&isolation_type_snp); +} +EXPORT_SYMBOL_GPL(hv_isolation_type_snp); diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyper= v.h index adccbc209169..37739a277ac6 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -11,6 +11,8 @@ #include #include =20 +DECLARE_STATIC_KEY_FALSE(isolation_type_snp); + typedef int (*hyperv_fill_flush_list_func)( struct hv_guest_mapping_flush_list *flush, void *data); @@ -39,6 +41,8 @@ extern void *hv_hypercall_pg; =20 extern u64 hv_current_partition_id; =20 +extern void __percpu **hv_ghcb_pg; + int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages); int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id); int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags); diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c index 6b5835a087a3..20557a9d6e25 100644 --- a/arch/x86/kernel/cpu/mshyperv.c +++ b/arch/x86/kernel/cpu/mshyperv.c @@ -316,6 +316,9 @@ static void __init ms_hyperv_init_platform(void) =20 pr_info("Hyper-V: Isolation Config: Group A 0x%x, Group B 0x%x\n", ms_hyperv.isolation_config_a, ms_hyperv.isolation_config_b); + + if (hv_get_isolation_type() =3D=3D HV_ISOLATION_TYPE_SNP) + static_branch_enable(&isolation_type_snp); } =20 if (hv_max_functions_eax >=3D HYPERV_CPUID_NESTED_FEATURES) { diff --git a/include/asm-generic/mshyperv.h b/include/asm-generic/mshyperv.h index c1ab6a6e72b5..0924bbd8458e 100644 --- a/include/asm-generic/mshyperv.h +++ b/include/asm-generic/mshyperv.h @@ -237,6 +237,7 @@ bool hv_is_hyperv_initialized(void); bool hv_is_hibernation_supported(void); enum hv_isolation_type hv_get_isolation_type(void); bool hv_is_isolation_supported(void); +bool hv_isolation_type_snp(void); void hyperv_cleanup(void); bool hv_query_ext_cap(u64 cap_query); #else /* CONFIG_HYPERV */ --=20 2.25.1 From nobody Sun May 5 00:53:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1630084906; cv=none; d=zohomail.com; s=zohoarc; b=mDWBsMltBgG+JfgplwjL/M4KNPYei7usLttmvhe7dmT01s6aozdVPrvXJ3aVcQBea5MegkguDc2wzKyp8K8gWh7oQeb2ImBTkUKoCCAjDP+WhY94UpyoXpUQPI4pHYybfE/oaOGvh/ZqtPH2I0H3MtSmf06Cxx676DQGqe3Zius= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630084906; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=QeAoBJVxLuEqjhstpPPHRrsV4aYneiblS47oqHVJx+4=; b=MvGjK4A/Nwdly8bCi5c1eUkLcsn0oEXCvGO09fLvFQDfql6UTZgzVt7RJdveOJflRHDnAJHFlIRN/lB3PCUKHrzFVdiD9A8t1aCZ1YJiiEeJ2tQu8mi8+paqRy8/NaGBMxmcGD405illwdnFr04WmYiaKAwXdLC/ApTsFGccC1A= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1630084906939192.91154883745673; Fri, 27 Aug 2021 10:21:46 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.173802.317111 (Exim 4.92) (envelope-from ) id 1mJfXp-0001hM-QT; Fri, 27 Aug 2021 17:21:33 +0000 Received: by outflank-mailman (output) from mailman id 173802.317111; Fri, 27 Aug 2021 17:21:33 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfXp-0001hF-NM; Fri, 27 Aug 2021 17:21:33 +0000 Received: by outflank-mailman (input) for mailman id 173802; Fri, 27 Aug 2021 17:21:32 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfXo-00016Q-Mo for xen-devel@lists.xenproject.org; Fri, 27 Aug 2021 17:21:32 +0000 Received: from mail-pl1-x62f.google.com (unknown [2607:f8b0:4864:20::62f]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 259bc3f0-0fb0-4ef3-b341-af0ee0a5e24d; Fri, 27 Aug 2021 17:21:27 +0000 (UTC) Received: by mail-pl1-x62f.google.com with SMTP id q21so4358658plq.3 for ; Fri, 27 Aug 2021 10:21:27 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:36:ef50:8fcd:44d1:eb17]) by smtp.gmail.com with ESMTPSA id f5sm7155015pjo.23.2021.08.27.10.21.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Aug 2021 10:21:26 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 259bc3f0-0fb0-4ef3-b341-af0ee0a5e24d DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QeAoBJVxLuEqjhstpPPHRrsV4aYneiblS47oqHVJx+4=; b=I7NUV++ybJ+PeLzq7nvmvC/fUa1MzSPBVMWympCWggjJfdGIagT64paurWTr4ufYmh iyifKSagK7g0dfWSGenf+VOn1hz4bUmTah6emDjZ7Foz5yleN2+WsDzCIuDF9ckq2p8q hFjrwh845yHhH4sTpGEnOYuYp+n6e3PBkeMtpDD2gYyYmLb/pNE98ycigf1ahWeak5Ds 119NB5Pug3TLpnQ/Feznsq48O//rxAT/a4sTQ52sT7LMmBNKIHmPmUv4Wo2l0yZEYlky QLNWAslMK29qAA7pi1dHujT6sJotapiAiz0QzHw0Xdhri/P7a9oV3kQamXq3XVGl03mc qYfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QeAoBJVxLuEqjhstpPPHRrsV4aYneiblS47oqHVJx+4=; b=m61ZOP4K6wqCNbgtxpdvvEKSmHMjfxzb4wrATo/zeL96Bq3hOMdcQ4DTE/AqF6Ll4n 6KmzqiMKdjlNzLlUaSC64p6cZEVzv7HbST4u+3BlHI4K8prKFcf/vqgXJelfxcqkP5lq YMcAiMw8STZsQWqyrvNA/xEGuXQemAiBiIOcP1k12UUWqYeBuH7falfB9IW1ovskMVlU hk+2OaMZLsJRjV59gGc/y5EwrmjUAJxbe8JQ07bo/pJJfyJsHeL1bS01EYlsWKpznO8t tFeaa1Hji50+pwjLl1gVZxxZMm4Q1vs5KykFhX5IiXShkHl0rbn7rlQawuITtu1YzVop mcrQ== X-Gm-Message-State: AOAM531jZz3Bby98U6HbUZUFehNlIHwwd/LSvg1JF0sXdKtyEEUo5XH8 b7KfMx7Ymv4JMTcbv/ovAKI= X-Google-Smtp-Source: ABdhPJygO/uJNLRxEGKep6uevI66uTYM1Cjltj7n7K2J5FM1lDlOoUIpLG2da+nKpVHYkSugQbym8Q== X-Received: by 2002:a17:902:70cb:b0:132:6a68:af90 with SMTP id l11-20020a17090270cb00b001326a68af90mr9716504plt.56.1630084886558; Fri, 27 Aug 2021 10:21:26 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, catalin.marinas@arm.com, will@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, gregkh@linuxfoundation.org, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, brijesh.singh@amd.com, thomas.lendacky@amd.com, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, hannes@cmpxchg.org, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, linux-arm-kernel@lists.infradead.org, xen-devel@lists.xenproject.org, rientjes@google.com, ardb@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V4 02/13] x86/hyperv: Initialize shared memory boundary in the Isolation VM. Date: Fri, 27 Aug 2021 13:21:00 -0400 Message-Id: <20210827172114.414281-3-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210827172114.414281-1-ltykernel@gmail.com> References: <20210827172114.414281-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1630084908845100007 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan Hyper-V exposes shared memory boundary via cpuid HYPERV_CPUID_ISOLATION_CONFIG and store it in the shared_gpa_boundary of ms_hyperv struct. This prepares to share memory with host for SNP guest. Signed-off-by: Tianyu Lan --- Change since v3: * user BIT_ULL to get shared_gpa_boundary * Rename field Reserved* to reserved --- arch/x86/kernel/cpu/mshyperv.c | 2 ++ include/asm-generic/mshyperv.h | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c index 20557a9d6e25..8bb001198316 100644 --- a/arch/x86/kernel/cpu/mshyperv.c +++ b/arch/x86/kernel/cpu/mshyperv.c @@ -313,6 +313,8 @@ static void __init ms_hyperv_init_platform(void) if (ms_hyperv.priv_high & HV_ISOLATION) { ms_hyperv.isolation_config_a =3D cpuid_eax(HYPERV_CPUID_ISOLATION_CONFIG= ); ms_hyperv.isolation_config_b =3D cpuid_ebx(HYPERV_CPUID_ISOLATION_CONFIG= ); + ms_hyperv.shared_gpa_boundary =3D + BIT_ULL(ms_hyperv.shared_gpa_boundary_bits); =20 pr_info("Hyper-V: Isolation Config: Group A 0x%x, Group B 0x%x\n", ms_hyperv.isolation_config_a, ms_hyperv.isolation_config_b); diff --git a/include/asm-generic/mshyperv.h b/include/asm-generic/mshyperv.h index 0924bbd8458e..7537ae1db828 100644 --- a/include/asm-generic/mshyperv.h +++ b/include/asm-generic/mshyperv.h @@ -35,7 +35,17 @@ struct ms_hyperv_info { u32 max_vp_index; u32 max_lp_index; u32 isolation_config_a; - u32 isolation_config_b; + union { + u32 isolation_config_b; + struct { + u32 cvm_type : 4; + u32 reserved11 : 1; + u32 shared_gpa_boundary_active : 1; + u32 shared_gpa_boundary_bits : 6; + u32 reserved12 : 20; + }; + }; + u64 shared_gpa_boundary; }; extern struct ms_hyperv_info ms_hyperv; =20 --=20 2.25.1 From nobody Sun May 5 00:53:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1630084913; cv=none; d=zohomail.com; s=zohoarc; b=bZVuH5uMxluYKR76Jfu7lS7npUV4n+Lbm5klWDK0cmE52ubWT5bArPIOIwF71nEh/L+E88ccGKQDhwieb1wHtvEuLK9re7tDD2KmOW6BLSQ2AjUdCidSE40iUNRfqvV8GkS8X2NDi2m5FQi5TqfGQoksxp+UE8u+VcMY2O9jVvc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630084913; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=97TSoNEFUH/dcOtN9KWTkyqxlJ5Mdi7oz6hJ0eE1ORs=; b=WhaGjz7xafRm2bv2AbobQizSK9NtOhunHIOYsgMjh58jbFnu46PIDeyUiGP9/Hqwvi4Iij3Et/xyQK5FwY0SP68Rj7Kxo/XRGwu2cV5hlivMV7AyuTtTyNsGO8hfNCFY1lE89qewFYMw9reZriX44+mNs6iQjpLqXH3rHsx6cNw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1630084913545825.583023460218; Fri, 27 Aug 2021 10:21:53 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.173803.317123 (Exim 4.92) (envelope-from ) id 1mJfXv-00023o-3w; Fri, 27 Aug 2021 17:21:39 +0000 Received: by outflank-mailman (output) from mailman id 173803.317123; Fri, 27 Aug 2021 17:21:39 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfXu-00023d-VC; Fri, 27 Aug 2021 17:21:38 +0000 Received: by outflank-mailman (input) for mailman id 173803; Fri, 27 Aug 2021 17:21:37 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfXt-00016Q-N1 for xen-devel@lists.xenproject.org; Fri, 27 Aug 2021 17:21:37 +0000 Received: from mail-pg1-x52a.google.com (unknown [2607:f8b0:4864:20::52a]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 913f8b25-f630-4fc2-844b-04781bc92c41; Fri, 27 Aug 2021 17:21:29 +0000 (UTC) Received: by mail-pg1-x52a.google.com with SMTP id s11so6467420pgr.11 for ; Fri, 27 Aug 2021 10:21:29 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:36:ef50:8fcd:44d1:eb17]) by smtp.gmail.com with ESMTPSA id f5sm7155015pjo.23.2021.08.27.10.21.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Aug 2021 10:21:28 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 913f8b25-f630-4fc2-844b-04781bc92c41 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=97TSoNEFUH/dcOtN9KWTkyqxlJ5Mdi7oz6hJ0eE1ORs=; b=YRAstm5ofU6lymcaSHffQzFjHf7CrC9ui/b162RG6xUbr7quns6FgP0SE8tVEHansz 5qsaUt4VatKFIiI+VSvPF6adLk2ksLkJOPCCkx+qyeUI/ghRSDb9Y+rS6cfBhS3ZAnm3 TM5FVIlEaiQKQjGrpI7U/S0HdVOi8yfWaJu4svbpOmvYP0OMHtyMxWJBGyPR7libNktp yF1QX8uIOENq25zYmUsHtyd3uyZnFFbumt9pqtI15qmj9L0GHnaUyYzAMst5t1wKshud 1EPkxovDW0R+57wgcXgiwnxnRtr3Ud1HIU9mh+mhC2Don7t5kZJ+sLdVjQW8B3+2e4jG YP0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=97TSoNEFUH/dcOtN9KWTkyqxlJ5Mdi7oz6hJ0eE1ORs=; b=rvU9bqNAfHxfVOk/cbdlnzBONWpOJoHNzctTu3H4tLlWwhjRBhowIsB25HpdBonoNg e+mLY84UJovPQ8BuujKs8Ce5cgK0uNMaZ86N7rvzYhdx6+CqS/RzxXgkU80qXMg1udK0 TNzLvc8QkBuzCTFK5UUCBpaLoeh5x1r2vqVAeFl4nght+OluIdKZrbWgh5N3RgSK3/J8 jHNg3c/5QqSuPCNdOpWK3Aw8eup2lhEoJzQtoeBQsg0R7ie6vfxO/5xam4+CGBDr9rqs c5tBk4n6qSnRel/N64lD7G2FF1pHqUOTXJ+MjJxMw+d6XbhNyQ7eJ4Zqd5g54vUleVSu EM2g== X-Gm-Message-State: AOAM530MAHq6b5AmMOHNOTUSFX1qCi/lZQkkeIHiBhhsVCUWo3wVBbl1 /y0B1hkC+2Ch630AKEFwJuw= X-Google-Smtp-Source: ABdhPJwXkzmhKuM8IMAJzHz5jyUb4XiEbWyJ9t/qB/+cDCy1kYl16jf539JCUKzcxANI+FnYjNOQjQ== X-Received: by 2002:aa7:96e7:0:b0:3f1:fbd4:68f2 with SMTP id i7-20020aa796e7000000b003f1fbd468f2mr9781666pfq.77.1630084888856; Fri, 27 Aug 2021 10:21:28 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, catalin.marinas@arm.com, will@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, gregkh@linuxfoundation.org, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, brijesh.singh@amd.com, thomas.lendacky@amd.com, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, hannes@cmpxchg.org, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, linux-arm-kernel@lists.infradead.org, xen-devel@lists.xenproject.org, rientjes@google.com, ardb@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V4 03/13] x86/hyperv: Add new hvcall guest address host visibility support Date: Fri, 27 Aug 2021 13:21:01 -0400 Message-Id: <20210827172114.414281-4-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210827172114.414281-1-ltykernel@gmail.com> References: <20210827172114.414281-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1630084915795100003 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan Add new hvcall guest address host visibility support to mark memory visible to host. Call it inside set_memory_decrypted /encrypted(). Add HYPERVISOR feature check in the hv_is_isolation_supported() to optimize in non-virtualization environment. Acked-by: Dave Hansen Signed-off-by: Tianyu Lan --- Change since v3: * Fix error code handle in the __hv_set_mem_host_visibility(). * Move HvCallModifySparseGpaPageHostVisibility near to enum hv_mem_host_visibility. Change since v2: * Rework __set_memory_enc_dec() and call Hyper-V and AMD function according to platform check. Change since v1: * Use new staic call x86_set_memory_enc to avoid add Hyper-V specific check in the set_memory code. --- arch/x86/hyperv/Makefile | 2 +- arch/x86/hyperv/hv_init.c | 6 ++ arch/x86/hyperv/ivm.c | 113 +++++++++++++++++++++++++++++ arch/x86/include/asm/hyperv-tlfs.h | 17 +++++ arch/x86/include/asm/mshyperv.h | 4 +- arch/x86/mm/pat/set_memory.c | 19 +++-- include/asm-generic/hyperv-tlfs.h | 1 + include/asm-generic/mshyperv.h | 1 + 8 files changed, 156 insertions(+), 7 deletions(-) create mode 100644 arch/x86/hyperv/ivm.c diff --git a/arch/x86/hyperv/Makefile b/arch/x86/hyperv/Makefile index 48e2c51464e8..5d2de10809ae 100644 --- a/arch/x86/hyperv/Makefile +++ b/arch/x86/hyperv/Makefile @@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-y :=3D hv_init.o mmu.o nested.o irqdomain.o +obj-y :=3D hv_init.o mmu.o nested.o irqdomain.o ivm.o obj-$(CONFIG_X86_64) +=3D hv_apic.o hv_proc.o =20 ifdef CONFIG_X86_64 diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c index eba10ed4f73e..b1aa42f60faa 100644 --- a/arch/x86/hyperv/hv_init.c +++ b/arch/x86/hyperv/hv_init.c @@ -603,6 +603,12 @@ EXPORT_SYMBOL_GPL(hv_get_isolation_type); =20 bool hv_is_isolation_supported(void) { + if (!cpu_feature_enabled(X86_FEATURE_HYPERVISOR)) + return 0; + + if (!hypervisor_is_type(X86_HYPER_MS_HYPERV)) + return 0; + return hv_get_isolation_type() !=3D HV_ISOLATION_TYPE_NONE; } =20 diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c new file mode 100644 index 000000000000..a069c788ce3c --- /dev/null +++ b/arch/x86/hyperv/ivm.c @@ -0,0 +1,113 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Hyper-V Isolation VM interface with paravisor and hypervisor + * + * Author: + * Tianyu Lan + */ + +#include +#include +#include +#include +#include +#include + +/* + * hv_mark_gpa_visibility - Set pages visible to host via hvcall. + * + * In Isolation VM, all guest memory is encripted from host and guest + * needs to set memory visible to host via hvcall before sharing memory + * with host. + */ +int hv_mark_gpa_visibility(u16 count, const u64 pfn[], + enum hv_mem_host_visibility visibility) +{ + struct hv_gpa_range_for_visibility **input_pcpu, *input; + u16 pages_processed; + u64 hv_status; + unsigned long flags; + + /* no-op if partition isolation is not enabled */ + if (!hv_is_isolation_supported()) + return 0; + + if (count > HV_MAX_MODIFY_GPA_REP_COUNT) { + pr_err("Hyper-V: GPA count:%d exceeds supported:%lu\n", count, + HV_MAX_MODIFY_GPA_REP_COUNT); + return -EINVAL; + } + + local_irq_save(flags); + input_pcpu =3D (struct hv_gpa_range_for_visibility **) + this_cpu_ptr(hyperv_pcpu_input_arg); + input =3D *input_pcpu; + if (unlikely(!input)) { + local_irq_restore(flags); + return -EINVAL; + } + + input->partition_id =3D HV_PARTITION_ID_SELF; + input->host_visibility =3D visibility; + input->reserved0 =3D 0; + input->reserved1 =3D 0; + memcpy((void *)input->gpa_page_list, pfn, count * sizeof(*pfn)); + hv_status =3D hv_do_rep_hypercall( + HVCALL_MODIFY_SPARSE_GPA_PAGE_HOST_VISIBILITY, count, + 0, input, &pages_processed); + local_irq_restore(flags); + + if (hv_result_success(hv_status)) + return 0; + else + return -EFAULT; +} +EXPORT_SYMBOL(hv_mark_gpa_visibility); + +static int __hv_set_mem_host_visibility(void *kbuffer, int pagecount, + enum hv_mem_host_visibility visibility) +{ + u64 *pfn_array; + int ret =3D 0; + int i, pfn; + + if (!hv_is_isolation_supported() || !hv_hypercall_pg) + return 0; + + pfn_array =3D kmalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL); + if (!pfn_array) + return -ENOMEM; + + for (i =3D 0, pfn =3D 0; i < pagecount; i++) { + pfn_array[pfn] =3D virt_to_hvpfn(kbuffer + i * HV_HYP_PAGE_SIZE); + pfn++; + + if (pfn =3D=3D HV_MAX_MODIFY_GPA_REP_COUNT || i =3D=3D pagecount - 1) { + ret =3D hv_mark_gpa_visibility(pfn, pfn_array, + visibility); + if (ret) + goto err_free_pfn_array; + pfn =3D 0; + } + } + + err_free_pfn_array: + kfree(pfn_array); + return ret; +} + +/* + * hv_set_mem_host_visibility - Set specified memory visible to host. + * + * In Isolation VM, all guest memory is encrypted from host and guest + * needs to set memory visible to host via hvcall before sharing memory + * with host. This function works as wrap of hv_mark_gpa_visibility() + * with memory base and size. + */ +int hv_set_mem_host_visibility(unsigned long addr, int numpages, bool visi= ble) +{ + enum hv_mem_host_visibility visibility =3D visible ? + VMBUS_PAGE_VISIBLE_READ_WRITE : VMBUS_PAGE_NOT_VISIBLE; + + return __hv_set_mem_host_visibility((void *)addr, numpages, visibility); +} diff --git a/arch/x86/include/asm/hyperv-tlfs.h b/arch/x86/include/asm/hype= rv-tlfs.h index 2322d6bd5883..381e88122a5f 100644 --- a/arch/x86/include/asm/hyperv-tlfs.h +++ b/arch/x86/include/asm/hyperv-tlfs.h @@ -276,6 +276,23 @@ enum hv_isolation_type { #define HV_X64_MSR_TIME_REF_COUNT HV_REGISTER_TIME_REF_COUNT #define HV_X64_MSR_REFERENCE_TSC HV_REGISTER_REFERENCE_TSC =20 +/* Hyper-V memory host visibility */ +enum hv_mem_host_visibility { + VMBUS_PAGE_NOT_VISIBLE =3D 0, + VMBUS_PAGE_VISIBLE_READ_ONLY =3D 1, + VMBUS_PAGE_VISIBLE_READ_WRITE =3D 3 +}; + +/* HvCallModifySparseGpaPageHostVisibility hypercall */ +#define HV_MAX_MODIFY_GPA_REP_COUNT ((PAGE_SIZE / sizeof(u64)) - 2) +struct hv_gpa_range_for_visibility { + u64 partition_id; + u32 host_visibility:2; + u32 reserved0:30; + u32 reserved1; + u64 gpa_page_list[HV_MAX_MODIFY_GPA_REP_COUNT]; +} __packed; + /* * Declare the MSR used to setup pages used to communicate with the hyperv= isor. */ diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyper= v.h index 37739a277ac6..ffb2af079c6b 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -192,7 +192,9 @@ struct irq_domain *hv_create_pci_msi_domain(void); int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vecto= r, struct hv_interrupt_entry *entry); int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *en= try); - +int hv_mark_gpa_visibility(u16 count, const u64 pfn[], + enum hv_mem_host_visibility visibility); +int hv_set_mem_host_visibility(unsigned long addr, int numpages, bool visi= ble); #else /* CONFIG_HYPERV */ static inline void hyperv_init(void) {} static inline void hyperv_setup_mmu_ops(void) {} diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index ad8a5c586a35..1e4a0882820a 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -29,6 +29,8 @@ #include #include #include +#include +#include =20 #include "../mm_internal.h" =20 @@ -1980,15 +1982,11 @@ int set_memory_global(unsigned long addr, int numpa= ges) __pgprot(_PAGE_GLOBAL), 0); } =20 -static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc) +static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool= enc) { struct cpa_data cpa; int ret; =20 - /* Nothing to do if memory encryption is not active */ - if (!mem_encrypt_active()) - return 0; - /* Should not be working on unaligned addresses */ if (WARN_ONCE(addr & ~PAGE_MASK, "misaligned address: %#lx\n", addr)) addr &=3D PAGE_MASK; @@ -2023,6 +2021,17 @@ static int __set_memory_enc_dec(unsigned long addr, = int numpages, bool enc) return ret; } =20 +static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc) +{ + if (hv_is_isolation_supported()) + return hv_set_mem_host_visibility(addr, numpages, !enc); + + if (mem_encrypt_active()) + return __set_memory_enc_pgtable(addr, numpages, enc); + + return 0; +} + int set_memory_encrypted(unsigned long addr, int numpages) { return __set_memory_enc_dec(addr, numpages, true); diff --git a/include/asm-generic/hyperv-tlfs.h b/include/asm-generic/hyperv= -tlfs.h index 56348a541c50..8ed6733d5146 100644 --- a/include/asm-generic/hyperv-tlfs.h +++ b/include/asm-generic/hyperv-tlfs.h @@ -158,6 +158,7 @@ struct ms_hyperv_tsc_page { #define HVCALL_RETARGET_INTERRUPT 0x007e #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE 0x00af #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_LIST 0x00b0 +#define HVCALL_MODIFY_SPARSE_GPA_PAGE_HOST_VISIBILITY 0x00db =20 /* Extended hypercalls */ #define HV_EXT_CALL_QUERY_CAPABILITIES 0x8001 diff --git a/include/asm-generic/mshyperv.h b/include/asm-generic/mshyperv.h index 7537ae1db828..aa55447b9700 100644 --- a/include/asm-generic/mshyperv.h +++ b/include/asm-generic/mshyperv.h @@ -254,6 +254,7 @@ bool hv_query_ext_cap(u64 cap_query); static inline bool hv_is_hyperv_initialized(void) { return false; } static inline bool hv_is_hibernation_supported(void) { return false; } static inline void hyperv_cleanup(void) {} +static inline hv_is_isolation_supported(void); #endif /* CONFIG_HYPERV */ =20 #endif --=20 2.25.1 From nobody Sun May 5 00:53:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1630084917; cv=none; d=zohomail.com; s=zohoarc; b=ZHOXqv/81VfeUEbOp478sdI02lZgqP5+OeCtKoMpP4E3ozGXR2bJd5UHb+xMgfpwImUDIqj3Y3rgHdmp4b+F01HkIvxuWqmRykqMMLaVkQ6WcQj7mhaWmXuurJDeFQXXbp6Z6haBCjfB6L0Kw7WHmV0tICpNTWITz1JLLpZZhOY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630084917; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=UK8KBR+AM7nXV01E8dM+g65JtfRzps5r9CU0KnjqW2k=; b=R9Kb2NRASxCLld4DRitq7GJ/D+EVjlT+B62OUc1zxwu65Vtsk1EbVaREPAp18k+BshbtbsZlBAoV5kupBxaZ9AqWMBNgimK3Oe0fhkZiQeCcCn9iMWZFZFekPCn2o4A9COt/dkEuBMVSu9i20Mhjoc8CA7vYUxvTIv/ewvylIp8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1630084917600408.79704564849044; Fri, 27 Aug 2021 10:21:57 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.173804.317134 (Exim 4.92) (envelope-from ) id 1mJfXz-0002Sr-Jv; Fri, 27 Aug 2021 17:21:43 +0000 Received: by outflank-mailman (output) from mailman id 173804.317134; Fri, 27 Aug 2021 17:21:43 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfXz-0002Sg-Ft; Fri, 27 Aug 2021 17:21:43 +0000 Received: by outflank-mailman (input) for mailman id 173804; Fri, 27 Aug 2021 17:21:42 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfXy-00016Q-N6 for xen-devel@lists.xenproject.org; Fri, 27 Aug 2021 17:21:42 +0000 Received: from mail-pl1-x633.google.com (unknown [2607:f8b0:4864:20::633]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 349db860-40c1-4fc0-b88f-5c56e9faed7e; Fri, 27 Aug 2021 17:21:31 +0000 (UTC) Received: by mail-pl1-x633.google.com with SMTP id j2so4364514pll.1 for ; Fri, 27 Aug 2021 10:21:31 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:36:ef50:8fcd:44d1:eb17]) by smtp.gmail.com with ESMTPSA id f5sm7155015pjo.23.2021.08.27.10.21.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Aug 2021 10:21:30 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 349db860-40c1-4fc0-b88f-5c56e9faed7e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UK8KBR+AM7nXV01E8dM+g65JtfRzps5r9CU0KnjqW2k=; b=ve2IfOkuhZeXLPaSsOefBWJQVXO7Fomi79JA3NKf6cjuWsN5c+xuKhGp6k5cwyvODG MgKfbmKZTA0KXcVCK5yPL/O5HsV8TQL/bb6H5AsGJb13MU7dBNds2kT/0U9rREbkrtQn Py1dhGunkXUOG21ZS9K3POXFabiwDwy6lJAsxchzkqMtBFVJxMOMTSre4zEjVuV+DP1p 9rTsiYsX7bf/ct8pFPCUhXJ5wCAIf0/eA+pUwDLZtnWvqmgWm+/ho47HQDBIPynrlYPC 9hwMu2l/qm/RurhAlO11gAom0hxKB/X0pLIdC+3F2rnpmEksmoCPjzV6lngObwAxNYwz OzJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UK8KBR+AM7nXV01E8dM+g65JtfRzps5r9CU0KnjqW2k=; b=TTAJtvZDapRMBjLEbYzGF1n4bajTWySqIjtWZDAynXGFdnCKuJ82QusZEJbRO7Djws 5z4p4dwBxe5vTSwyoZb0nZVgH0h8tDmYVmj/cbYXJ6+vk/OiAdVtYrzPE0TU8LfSwBTQ piCp4viZE5MqoY162AFDIFnW/+A2/jfje7iIAIxKh7v054rHY3zAKg15Nv1+DBs4C+Tz t9cBJkJG4Hv9PfQWvDVOubyXKiS5UGsv1ZZllRdIGrdKfA7uzEm5gdXDEdw3jF4vHwMS 24AgdBZlR3PtIpMYohQvpU4CSu+aASFDn3T4VjnDn5B1wdAfZG2IO6hGhUk0pf+MMB0L aZxQ== X-Gm-Message-State: AOAM533gCc5EjgjSCtxbVpbkZ+M1XAPQOO/iBSn2SOt1DUgFANRuUk2+ HmkrQ6dbqXOj3OBUSpuKE84= X-Google-Smtp-Source: ABdhPJyqfQQ5fRrI7GmAJTDqX9bqPYgY3SObBdBo5YLycH2A2XdhXK90T0PqgMjyjAgWsPbPHwEsOQ== X-Received: by 2002:a17:902:82c1:b0:135:b97d:8e84 with SMTP id u1-20020a17090282c100b00135b97d8e84mr9559224plz.85.1630084891091; Fri, 27 Aug 2021 10:21:31 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, catalin.marinas@arm.com, will@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, gregkh@linuxfoundation.org, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, brijesh.singh@amd.com, thomas.lendacky@amd.com, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, hannes@cmpxchg.org, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, linux-arm-kernel@lists.infradead.org, xen-devel@lists.xenproject.org, rientjes@google.com, ardb@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V4 04/13] hyperv: Mark vmbus ring buffer visible to host in Isolation VM Date: Fri, 27 Aug 2021 13:21:02 -0400 Message-Id: <20210827172114.414281-5-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210827172114.414281-1-ltykernel@gmail.com> References: <20210827172114.414281-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1630084918196100001 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan Mark vmbus ring buffer visible with set_memory_decrypted() when establish gpadl handle. Signed-off-by: Tianyu Lan --- Change since v3: * Change vmbus_teardown_gpadl() parameter and put gpadl handle, buffer and buffer size in the struct vmbus_gpadl. --- drivers/hv/channel.c | 36 ++++++++++++++++++++++++++++----- drivers/net/hyperv/hyperv_net.h | 1 + drivers/net/hyperv/netvsc.c | 16 +++++++++++---- drivers/uio/uio_hv_generic.c | 14 +++++++++++-- include/linux/hyperv.h | 8 +++++++- 5 files changed, 63 insertions(+), 12 deletions(-) diff --git a/drivers/hv/channel.c b/drivers/hv/channel.c index f3761c73b074..82650beb3af0 100644 --- a/drivers/hv/channel.c +++ b/drivers/hv/channel.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include =20 @@ -474,6 +475,13 @@ static int __vmbus_establish_gpadl(struct vmbus_channe= l *channel, if (ret) return ret; =20 + ret =3D set_memory_decrypted((unsigned long)kbuffer, + HVPFN_UP(size)); + if (ret) { + pr_warn("Failed to set host visibility for new GPADL %d.\n", ret); + return ret; + } + init_completion(&msginfo->waitevent); msginfo->waiting_channel =3D channel; =20 @@ -549,6 +557,11 @@ static int __vmbus_establish_gpadl(struct vmbus_channe= l *channel, } =20 kfree(msginfo); + + if (ret) + set_memory_encrypted((unsigned long)kbuffer, + HVPFN_UP(size)); + return ret; } =20 @@ -639,6 +652,7 @@ static int __vmbus_open(struct vmbus_channel *newchanne= l, struct vmbus_channel_open_channel *open_msg; struct vmbus_channel_msginfo *open_info =3D NULL; struct page *page =3D newchannel->ringbuffer_page; + struct vmbus_gpadl gpadl; u32 send_pages, recv_pages; unsigned long flags; int err; @@ -759,7 +773,10 @@ static int __vmbus_open(struct vmbus_channel *newchann= el, error_free_info: kfree(open_info); error_free_gpadl: - vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle); + gpadl.gpadl_handle =3D newchannel->ringbuffer_gpadlhandle; + gpadl.buffer =3D page_address(newchannel->ringbuffer_page); + gpadl.size =3D (send_pages + recv_pages) << PAGE_SHIFT; + vmbus_teardown_gpadl(newchannel, &gpadl); newchannel->ringbuffer_gpadlhandle =3D 0; error_clean_ring: hv_ringbuffer_cleanup(&newchannel->outbound); @@ -806,7 +823,7 @@ EXPORT_SYMBOL_GPL(vmbus_open); /* * vmbus_teardown_gpadl -Teardown the specified GPADL handle */ -int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle) +int vmbus_teardown_gpadl(struct vmbus_channel *channel, struct vmbus_gpadl= *gpadl) { struct vmbus_channel_gpadl_teardown *msg; struct vmbus_channel_msginfo *info; @@ -825,7 +842,7 @@ int vmbus_teardown_gpadl(struct vmbus_channel *channel,= u32 gpadl_handle) =20 msg->header.msgtype =3D CHANNELMSG_GPADL_TEARDOWN; msg->child_relid =3D channel->offermsg.child_relid; - msg->gpadl =3D gpadl_handle; + msg->gpadl =3D gpadl->gpadl_handle; =20 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); list_add_tail(&info->msglistentry, @@ -859,6 +876,12 @@ int vmbus_teardown_gpadl(struct vmbus_channel *channel= , u32 gpadl_handle) spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); =20 kfree(info); + + ret =3D set_memory_encrypted((unsigned long)gpadl->buffer, + HVPFN_UP(gpadl->size)); + if (ret) + pr_warn("Fail to set mem host visibility in GPADL teardown %d.\n", ret); + return ret; } EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl); @@ -896,6 +919,7 @@ void vmbus_reset_channel_cb(struct vmbus_channel *chann= el) static int vmbus_close_internal(struct vmbus_channel *channel) { struct vmbus_channel_close_channel *msg; + struct vmbus_gpadl gpadl; int ret; =20 vmbus_reset_channel_cb(channel); @@ -934,8 +958,10 @@ static int vmbus_close_internal(struct vmbus_channel *= channel) =20 /* Tear down the gpadl for the channel's ring buffer */ else if (channel->ringbuffer_gpadlhandle) { - ret =3D vmbus_teardown_gpadl(channel, - channel->ringbuffer_gpadlhandle); + gpadl.gpadl_handle =3D channel->ringbuffer_gpadlhandle; + gpadl.buffer =3D page_address(channel->ringbuffer_page); + gpadl.size =3D channel->ringbuffer_pagecount; + ret =3D vmbus_teardown_gpadl(channel, &gpadl); if (ret) { pr_err("Close failed: teardown gpadl return %d\n", ret); /* diff --git a/drivers/net/hyperv/hyperv_net.h b/drivers/net/hyperv/hyperv_ne= t.h index bc48855dff10..aa7c9962dbd8 100644 --- a/drivers/net/hyperv/hyperv_net.h +++ b/drivers/net/hyperv/hyperv_net.h @@ -1082,6 +1082,7 @@ struct netvsc_device { =20 /* Send buffer allocated by us */ void *send_buf; + u32 send_buf_size; u32 send_buf_gpadl_handle; u32 send_section_cnt; u32 send_section_size; diff --git a/drivers/net/hyperv/netvsc.c b/drivers/net/hyperv/netvsc.c index 7bd935412853..f19bffff6a63 100644 --- a/drivers/net/hyperv/netvsc.c +++ b/drivers/net/hyperv/netvsc.c @@ -276,11 +276,14 @@ static void netvsc_teardown_recv_gpadl(struct hv_devi= ce *device, struct netvsc_device *net_device, struct net_device *ndev) { + struct vmbus_gpadl gpadl; int ret; =20 if (net_device->recv_buf_gpadl_handle) { - ret =3D vmbus_teardown_gpadl(device->channel, - net_device->recv_buf_gpadl_handle); + gpadl.gpadl_handle =3D net_device->recv_buf_gpadl_handle; + gpadl.buffer =3D net_device->recv_buf; + gpadl.size =3D net_device->recv_buf_size; + ret =3D vmbus_teardown_gpadl(device->channel, &gpadl); =20 /* If we failed here, we might as well return and have a leak * rather than continue and a bugchk @@ -298,11 +301,15 @@ static void netvsc_teardown_send_gpadl(struct hv_devi= ce *device, struct netvsc_device *net_device, struct net_device *ndev) { + struct vmbus_gpadl gpadl; int ret; =20 if (net_device->send_buf_gpadl_handle) { - ret =3D vmbus_teardown_gpadl(device->channel, - net_device->send_buf_gpadl_handle); + gpadl.gpadl_handle =3D net_device->send_buf_gpadl_handle; + gpadl.buffer =3D net_device->send_buf; + gpadl.size =3D net_device->send_buf_size; + + ret =3D vmbus_teardown_gpadl(device->channel, &gpadl); =20 /* If we failed here, we might as well return and have a leak * rather than continue and a bugchk @@ -463,6 +470,7 @@ static int netvsc_init_buf(struct hv_device *device, ret =3D -ENOMEM; goto cleanup; } + net_device->send_buf_size =3D buf_size; =20 /* Establish the gpadl handle for this buffer on this * channel. Note: This call uses the vmbus connection rather diff --git a/drivers/uio/uio_hv_generic.c b/drivers/uio/uio_hv_generic.c index 652fe2547587..13c5df8dd11d 100644 --- a/drivers/uio/uio_hv_generic.c +++ b/drivers/uio/uio_hv_generic.c @@ -179,14 +179,24 @@ hv_uio_new_channel(struct vmbus_channel *new_sc) static void hv_uio_cleanup(struct hv_device *dev, struct hv_uio_private_data *pdata) { + struct vmbus_gpadl gpadl; + if (pdata->send_gpadl) { - vmbus_teardown_gpadl(dev->channel, pdata->send_gpadl); + gpadl.gpadl_handle =3D pdata->send_gpadl; + gpadl.buffer =3D pdata->send_buf; + gpadl.size =3D SEND_BUFFER_SIZE; + + vmbus_teardown_gpadl(dev->channel, &gpadl); pdata->send_gpadl =3D 0; vfree(pdata->send_buf); } =20 if (pdata->recv_gpadl) { - vmbus_teardown_gpadl(dev->channel, pdata->recv_gpadl); + gpadl.gpadl_handle =3D pdata->recv_gpadl; + gpadl.buffer =3D pdata->recv_buf; + gpadl.size =3D RECV_BUFFER_SIZE; + + vmbus_teardown_gpadl(dev->channel, &gpadl); pdata->recv_gpadl =3D 0; vfree(pdata->recv_buf); } diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h index ddc8713ce57b..757e09606fd3 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -803,6 +803,12 @@ struct vmbus_device { =20 #define VMBUS_DEFAULT_MAX_PKT_SIZE 4096 =20 +struct vmbus_gpadl { + u32 gpadl_handle; + u32 size; + void *buffer; +}; + struct vmbus_channel { struct list_head listentry; =20 @@ -1195,7 +1201,7 @@ extern int vmbus_establish_gpadl(struct vmbus_channel= *channel, u32 *gpadl_handle); =20 extern int vmbus_teardown_gpadl(struct vmbus_channel *channel, - u32 gpadl_handle); + struct vmbus_gpadl *gpadl); =20 void vmbus_reset_channel_cb(struct vmbus_channel *channel); =20 --=20 2.25.1 From nobody Sun May 5 00:53:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1630084923; cv=none; d=zohomail.com; s=zohoarc; b=S/SYw+tHO/K/wqZklktOLycsDK7O4iAEPNmMoqWtOHthJ/X4s5FhARJi2uN/XVhWCGnHtQ09ItiJ4JRa77+wBU4o35en/kqV2VmegVVknt5P70QeHpGNNyX/uUj0nWE9St6/AoFJy0GeQyfTG5VPmnw/9RzrSeSlv+wCYVEmRBY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630084923; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=NMiinD0nrj/EF3wgWQrIIkzlaXeB4tTcwG2tHfq+6xA=; b=VO8TISLaT0ozXtBAro4BwVv+bAtN4Ufc9QkNejm+Ims+bCsyBw7p7/YN/8irZgcSjScyldW6ECWlo1ESK3MQi/vh8r2P+chQQMSKcVKskiSLGbL/mei6wroFT5YFVQ1lPX4OyJbnnOPZjWgq3ZtwKfm/dKkmw446dmVxVF1BEPw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1630084923480847.4083656117407; Fri, 27 Aug 2021 10:22:03 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.173805.317145 (Exim 4.92) (envelope-from ) id 1mJfY4-00032c-U1; Fri, 27 Aug 2021 17:21:48 +0000 Received: by outflank-mailman (output) from mailman id 173805.317145; Fri, 27 Aug 2021 17:21:48 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfY4-00032L-Oj; Fri, 27 Aug 2021 17:21:48 +0000 Received: by outflank-mailman (input) for mailman id 173805; Fri, 27 Aug 2021 17:21:47 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfY3-00016Q-NE for xen-devel@lists.xenproject.org; Fri, 27 Aug 2021 17:21:47 +0000 Received: from mail-pj1-x1035.google.com (unknown [2607:f8b0:4864:20::1035]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id ed1e1053-6fdb-4ff1-942a-7987278bd6e2; Fri, 27 Aug 2021 17:21:34 +0000 (UTC) Received: by mail-pj1-x1035.google.com with SMTP id 28-20020a17090a031cb0290178dcd8a4d1so8345555pje.0 for ; Fri, 27 Aug 2021 10:21:34 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:36:ef50:8fcd:44d1:eb17]) by smtp.gmail.com with ESMTPSA id f5sm7155015pjo.23.2021.08.27.10.21.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Aug 2021 10:21:32 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: ed1e1053-6fdb-4ff1-942a-7987278bd6e2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NMiinD0nrj/EF3wgWQrIIkzlaXeB4tTcwG2tHfq+6xA=; b=HEMUb+m3PpeyyOUVvXxqrhCKc8yNo0qk6PhsQs+IJBPiTP6RP0arkruBZb43WdICFI Opt0KQO05G/P+dvT3/I2EtVoIj2z/t0Onlp8WMG8/SDfpZ4HsuWD8pLq+FRSm5hnmv98 kTr99DM48/h8mox3SbNbKGQV848ahDDQX6aLPPLgsA0r2CqGr2e7jjWJPb9IsNx0+bZi pCQNofp5g9EwyyoP0FRQwSKLyr5wwS9+nYS1BQCL0La8qA4WkZNeUBaczLjfBKK63WO0 /Z8ImFpLV2BFTmLWM0JExERaQT0cf0JM8895jzqTvQFHwSV30HV6bxSVbnMng9gfq7ev sPRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NMiinD0nrj/EF3wgWQrIIkzlaXeB4tTcwG2tHfq+6xA=; b=BfezuenDXJHA9XDo9C3dWg5w67UlNMklS41b3QNb9da5kvCP7mTXUbEZjLv/ggsMeF cuV03LMNTckwd40FxccFJblmlgqlnLNrT86k2hSONm3dZU2gHiT12sznaPrBZ9MJrr7k csS96FQ+HBGicRCQcsTiL/oVJEzWrVgT45y7L/959vJtRmKWYzxyI+9qf4WwoI9oAJSe jZVlE9dN4LNaD3QvadU7flOAFFpuVSoYZDCsiCfS3qPzdFR/CDAjvW9L2LurQsrkmLOy JAmkp+VYAP+uhcc8Ii6ilqNLorU6RXqEoPGec5xfIh6qXuy0w/sNWyxBhJqOAaGCsEha hQ2A== X-Gm-Message-State: AOAM531jr+u4ADV+BE7GYzrK2q2XcPapXB/O+ZCsfCjq3F3w8AqJ0Jcc F/SbWmu7tBvNErDbeVr+unY= X-Google-Smtp-Source: ABdhPJzcsGWVF3pnUTc6cPdQZbAUQkeXvTpHWigo9Q+d4201ZV50lMmSLv171RQVlz5hiqe27/lyMw== X-Received: by 2002:a17:90a:ad7:: with SMTP id r23mr9357066pje.184.1630084893483; Fri, 27 Aug 2021 10:21:33 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, catalin.marinas@arm.com, will@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, gregkh@linuxfoundation.org, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, brijesh.singh@amd.com, thomas.lendacky@amd.com, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, hannes@cmpxchg.org, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, linux-arm-kernel@lists.infradead.org, xen-devel@lists.xenproject.org, rientjes@google.com, ardb@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V4 05/13] hyperv: Add Write/Read MSR registers via ghcb page Date: Fri, 27 Aug 2021 13:21:03 -0400 Message-Id: <20210827172114.414281-6-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210827172114.414281-1-ltykernel@gmail.com> References: <20210827172114.414281-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1630084925246100001 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan Hyperv provides GHCB protocol to write Synthetic Interrupt Controller MSR registers in Isolation VM with AMD SEV SNP and these registers are emulated by hypervisor directly. Hyperv requires to write SINTx MSR registers twice. First writes MSR via GHCB page to communicate with hypervisor and then writes wrmsr instruction to talk with paravisor which runs in VMPL0. Guest OS ID MSR also needs to be set via GHCB page. Signed-off-by: Tianyu Lan --- Change since v1: * Introduce sev_es_ghcb_hv_call_simple() and share code between SEV and Hyper-V code. Change since v3: * Pass old_msg_type to hv_signal_eom() as parameter. * Use HV_REGISTER_* marcro instead of HV_X64_MSR_* * Add hv_isolation_type_snp() weak function. * Add maros to set syinc register in ARM code. --- arch/arm64/include/asm/mshyperv.h | 23 ++++++ arch/x86/hyperv/hv_init.c | 36 ++-------- arch/x86/hyperv/ivm.c | 112 ++++++++++++++++++++++++++++++ arch/x86/include/asm/mshyperv.h | 80 ++++++++++++++++++++- arch/x86/include/asm/sev.h | 3 + arch/x86/kernel/sev-shared.c | 63 ++++++++++------- drivers/hv/hv.c | 112 ++++++++++++++++++++---------- drivers/hv/hv_common.c | 6 ++ include/asm-generic/mshyperv.h | 4 +- 9 files changed, 345 insertions(+), 94 deletions(-) diff --git a/arch/arm64/include/asm/mshyperv.h b/arch/arm64/include/asm/msh= yperv.h index 20070a847304..ced83297e009 100644 --- a/arch/arm64/include/asm/mshyperv.h +++ b/arch/arm64/include/asm/mshyperv.h @@ -41,6 +41,29 @@ static inline u64 hv_get_register(unsigned int reg) return hv_get_vpreg(reg); } =20 +#define hv_get_simp(val) { val =3D hv_get_register(HV_REGISTER_SIMP); } +#define hv_set_simp(val) hv_set_register(HV_REGISTER_SIMP, val) + +#define hv_get_siefp(val) { val =3D hv_get_register(HV_REGISTER_SIEFP); } +#define hv_set_siefp(val) hv_set_register(HV_REGISTER_SIEFP, val) + +#define hv_get_synint_state(int_num, val) { \ + val =3D hv_get_register(HV_REGISTER_SINT0 + int_num); \ + } + +#define hv_set_synint_state(int_num, val) \ + hv_set_register(HV_REGISTER_SINT0 + int_num, val) + +#define hv_get_synic_state(val) { \ + val =3D hv_get_register(HV_REGISTER_SCONTROL); \ + } + +#define hv_set_synic_state(val) \ + hv_set_register(HV_REGISTER_SCONTROL, val) + +#define hv_signal_eom(old_msg_type) \ + hv_set_register(HV_REGISTER_EOM, 0) + /* SMCCC hypercall parameters */ #define HV_SMCCC_FUNC_NUMBER 1 #define HV_FUNC_ID ARM_SMCCC_CALL_VAL( \ diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c index b1aa42f60faa..be6210a3fd2f 100644 --- a/arch/x86/hyperv/hv_init.c +++ b/arch/x86/hyperv/hv_init.c @@ -37,7 +37,7 @@ EXPORT_SYMBOL_GPL(hv_current_partition_id); void *hv_hypercall_pg; EXPORT_SYMBOL_GPL(hv_hypercall_pg); =20 -void __percpu **hv_ghcb_pg; +union hv_ghcb __percpu **hv_ghcb_pg; =20 /* Storage to save the hypercall page temporarily for hibernation */ static void *hv_hypercall_pg_saved; @@ -406,7 +406,7 @@ void __init hyperv_init(void) } =20 if (hv_isolation_type_snp()) { - hv_ghcb_pg =3D alloc_percpu(void *); + hv_ghcb_pg =3D alloc_percpu(union hv_ghcb *); if (!hv_ghcb_pg) goto free_vp_assist_page; } @@ -424,6 +424,9 @@ void __init hyperv_init(void) guest_id =3D generate_guest_id(0, LINUX_VERSION_CODE, 0); wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id); =20 + /* Hyper-V requires to write guest os id via ghcb in SNP IVM. */ + hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id); + hv_hypercall_pg =3D __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX, VM_FLUSH_RESET_PERMS, NUMA_NO_NODE, @@ -501,6 +504,7 @@ void __init hyperv_init(void) =20 clean_guest_os_id: wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); + hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0); cpuhp_remove_state(cpuhp); free_ghcb_page: free_percpu(hv_ghcb_pg); @@ -522,6 +526,7 @@ void hyperv_cleanup(void) =20 /* Reset our OS id */ wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); + hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0); =20 /* * Reset hypercall page reference before reset the page, @@ -592,30 +597,3 @@ bool hv_is_hyperv_initialized(void) return hypercall_msr.enable; } EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized); - -enum hv_isolation_type hv_get_isolation_type(void) -{ - if (!(ms_hyperv.priv_high & HV_ISOLATION)) - return HV_ISOLATION_TYPE_NONE; - return FIELD_GET(HV_ISOLATION_TYPE, ms_hyperv.isolation_config_b); -} -EXPORT_SYMBOL_GPL(hv_get_isolation_type); - -bool hv_is_isolation_supported(void) -{ - if (!cpu_feature_enabled(X86_FEATURE_HYPERVISOR)) - return 0; - - if (!hypervisor_is_type(X86_HYPER_MS_HYPERV)) - return 0; - - return hv_get_isolation_type() !=3D HV_ISOLATION_TYPE_NONE; -} - -DEFINE_STATIC_KEY_FALSE(isolation_type_snp); - -bool hv_isolation_type_snp(void) -{ - return static_branch_unlikely(&isolation_type_snp); -} -EXPORT_SYMBOL_GPL(hv_isolation_type_snp); diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index a069c788ce3c..f56fe4f73000 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -6,13 +6,125 @@ * Tianyu Lan */ =20 +#include +#include #include #include #include #include +#include +#include #include #include =20 +union hv_ghcb { + struct ghcb ghcb; +} __packed __aligned(HV_HYP_PAGE_SIZE); + +void hv_ghcb_msr_write(u64 msr, u64 value) +{ + union hv_ghcb *hv_ghcb; + void **ghcb_base; + unsigned long flags; + + if (!hv_ghcb_pg) + return; + + WARN_ON(in_nmi()); + + local_irq_save(flags); + ghcb_base =3D (void **)this_cpu_ptr(hv_ghcb_pg); + hv_ghcb =3D (union hv_ghcb *)*ghcb_base; + if (!hv_ghcb) { + local_irq_restore(flags); + return; + } + + ghcb_set_rcx(&hv_ghcb->ghcb, msr); + ghcb_set_rax(&hv_ghcb->ghcb, lower_32_bits(value)); + ghcb_set_rdx(&hv_ghcb->ghcb, upper_32_bits(value)); + + if (sev_es_ghcb_hv_call_simple(&hv_ghcb->ghcb, SVM_EXIT_MSR, 1, 0)) + pr_warn("Fail to write msr via ghcb %llx.\n", msr); + + local_irq_restore(flags); +} + +void hv_ghcb_msr_read(u64 msr, u64 *value) +{ + union hv_ghcb *hv_ghcb; + void **ghcb_base; + unsigned long flags; + + /* Check size of union hv_ghcb here. */ + BUILD_BUG_ON(sizeof(union hv_ghcb) !=3D HV_HYP_PAGE_SIZE); + + if (!hv_ghcb_pg) + return; + + WARN_ON(in_nmi()); + + local_irq_save(flags); + ghcb_base =3D (void **)this_cpu_ptr(hv_ghcb_pg); + hv_ghcb =3D (union hv_ghcb *)*ghcb_base; + if (!hv_ghcb) { + local_irq_restore(flags); + return; + } + + ghcb_set_rcx(&hv_ghcb->ghcb, msr); + if (sev_es_ghcb_hv_call_simple(&hv_ghcb->ghcb, SVM_EXIT_MSR, 0, 0)) + pr_warn("Fail to read msr via ghcb %llx.\n", msr); + else + *value =3D (u64)lower_32_bits(hv_ghcb->ghcb.save.rax) + | ((u64)lower_32_bits(hv_ghcb->ghcb.save.rdx) << 32); + local_irq_restore(flags); +} + +void hv_sint_rdmsrl_ghcb(u64 msr, u64 *value) +{ + hv_ghcb_msr_read(msr, value); +} +EXPORT_SYMBOL_GPL(hv_sint_rdmsrl_ghcb); + +void hv_sint_wrmsrl_ghcb(u64 msr, u64 value) +{ + hv_ghcb_msr_write(msr, value); + + /* Write proxy bit vua wrmsrl instruction. */ + if (msr >=3D HV_X64_MSR_SINT0 && msr <=3D HV_X64_MSR_SINT15) + wrmsrl(msr, value | 1 << 20); +} +EXPORT_SYMBOL_GPL(hv_sint_wrmsrl_ghcb); + +enum hv_isolation_type hv_get_isolation_type(void) +{ + if (!(ms_hyperv.priv_high & HV_ISOLATION)) + return HV_ISOLATION_TYPE_NONE; + return FIELD_GET(HV_ISOLATION_TYPE, ms_hyperv.isolation_config_b); +} +EXPORT_SYMBOL_GPL(hv_get_isolation_type); + +/* + * hv_is_isolation_supported - Check system runs in the Hyper-V + * isolation VM. + */ +bool hv_is_isolation_supported(void) +{ + return hv_get_isolation_type() !=3D HV_ISOLATION_TYPE_NONE; +} + +DEFINE_STATIC_KEY_FALSE(isolation_type_snp); + +/* + * hv_isolation_type_snp - Check system runs in the AMD SEV-SNP based + * isolation VM. + */ +bool hv_isolation_type_snp(void) +{ + return static_branch_unlikely(&isolation_type_snp); +} + /* * hv_mark_gpa_visibility - Set pages visible to host via hvcall. * diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyper= v.h index ffb2af079c6b..b77f4caee3ee 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -11,6 +11,8 @@ #include #include =20 +union hv_ghcb; + DECLARE_STATIC_KEY_FALSE(isolation_type_snp); =20 typedef int (*hyperv_fill_flush_list_func)( @@ -30,6 +32,61 @@ static inline u64 hv_get_register(unsigned int reg) return value; } =20 +#define hv_get_sint_reg(val, reg) { \ + if (hv_isolation_type_snp()) \ + hv_get_##reg##_ghcb(&val); \ + else \ + rdmsrl(HV_REGISTER_##reg, val); \ + } + +#define hv_set_sint_reg(val, reg) { \ + if (hv_isolation_type_snp()) \ + hv_set_##reg##_ghcb(val); \ + else \ + wrmsrl(HV_REGISTER_##reg, val); \ + } + + +#define hv_get_simp(val) hv_get_sint_reg(val, SIMP) +#define hv_get_siefp(val) hv_get_sint_reg(val, SIEFP) + +#define hv_set_simp(val) hv_set_sint_reg(val, SIMP) +#define hv_set_siefp(val) hv_set_sint_reg(val, SIEFP) + +#define hv_get_synic_state(val) { \ + if (hv_isolation_type_snp()) \ + hv_get_synic_state_ghcb(&val); \ + else \ + rdmsrl(HV_REGISTER_SCONTROL, val); \ + } +#define hv_set_synic_state(val) { \ + if (hv_isolation_type_snp()) \ + hv_set_synic_state_ghcb(val); \ + else \ + wrmsrl(HV_REGISTER_SCONTROL, val); \ + } + +#define hv_signal_eom(old_msg_type) { \ + if (hv_isolation_type_snp() && \ + old_msg_type !=3D HVMSG_TIMER_EXPIRED) \ + hv_sint_wrmsrl_ghcb(HV_REGISTER_EOM, 0); \ + else \ + wrmsrl(HV_REGISTER_EOM, 0); \ + } + +#define hv_get_synint_state(int_num, val) { \ + if (hv_isolation_type_snp()) \ + hv_get_synint_state_ghcb(int_num, &val);\ + else \ + rdmsrl(HV_REGISTER_SINT0 + int_num, val);\ + } +#define hv_set_synint_state(int_num, val) { \ + if (hv_isolation_type_snp()) \ + hv_set_synint_state_ghcb(int_num, val); \ + else \ + wrmsrl(HV_REGISTER_SINT0 + int_num, val);\ + } + #define hv_get_raw_timer() rdtsc_ordered() =20 void hyperv_vector_handler(struct pt_regs *regs); @@ -41,7 +98,7 @@ extern void *hv_hypercall_pg; =20 extern u64 hv_current_partition_id; =20 -extern void __percpu **hv_ghcb_pg; +extern union hv_ghcb __percpu **hv_ghcb_pg; =20 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages); int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id); @@ -195,6 +252,25 @@ int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv= _interrupt_entry *entry); int hv_mark_gpa_visibility(u16 count, const u64 pfn[], enum hv_mem_host_visibility visibility); int hv_set_mem_host_visibility(unsigned long addr, int numpages, bool visi= ble); +void hv_sint_wrmsrl_ghcb(u64 msr, u64 value); +void hv_sint_rdmsrl_ghcb(u64 msr, u64 *value); +void hv_signal_eom_ghcb(void); +void hv_ghcb_msr_write(u64 msr, u64 value); +void hv_ghcb_msr_read(u64 msr, u64 *value); + +#define hv_get_synint_state_ghcb(int_num, val) \ + hv_sint_rdmsrl_ghcb(HV_X64_MSR_SINT0 + int_num, val) +#define hv_set_synint_state_ghcb(int_num, val) \ + hv_sint_wrmsrl_ghcb(HV_X64_MSR_SINT0 + int_num, val) + +#define hv_get_SIMP_ghcb(val) hv_sint_rdmsrl_ghcb(HV_X64_MSR_SIMP, val) +#define hv_set_SIMP_ghcb(val) hv_sint_wrmsrl_ghcb(HV_X64_MSR_SIMP, val) + +#define hv_get_SIEFP_ghcb(val) hv_sint_rdmsrl_ghcb(HV_X64_MSR_SIEFP, val) +#define hv_set_SIEFP_ghcb(val) hv_sint_wrmsrl_ghcb(HV_X64_MSR_SIEFP, val) + +#define hv_get_synic_state_ghcb(val) hv_sint_rdmsrl_ghcb(HV_X64_MSR_SCONTR= OL, val) +#define hv_set_synic_state_ghcb(val) hv_sint_wrmsrl_ghcb(HV_X64_MSR_SCONTR= OL, val) #else /* CONFIG_HYPERV */ static inline void hyperv_init(void) {} static inline void hyperv_setup_mmu_ops(void) {} @@ -211,9 +287,9 @@ static inline int hyperv_flush_guest_mapping_range(u64 = as, { return -1; } +static inline void hv_signal_eom_ghcb(void) { }; #endif /* CONFIG_HYPERV */ =20 - #include =20 #endif diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index fa5cd05d3b5b..81beb2a8031b 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -81,6 +81,9 @@ static __always_inline void sev_es_nmi_complete(void) __sev_es_nmi_complete(); } extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd); +extern enum es_result sev_es_ghcb_hv_call_simple(struct ghcb *ghcb, + u64 exit_code, u64 exit_info_1, + u64 exit_info_2); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index 9f90f460a28c..dd7f37de640b 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -94,10 +94,9 @@ static void vc_finish_insn(struct es_em_ctxt *ctxt) ctxt->regs->ip +=3D ctxt->insn.length; } =20 -static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, - struct es_em_ctxt *ctxt, - u64 exit_code, u64 exit_info_1, - u64 exit_info_2) +enum es_result sev_es_ghcb_hv_call_simple(struct ghcb *ghcb, + u64 exit_code, u64 exit_info_1, + u64 exit_info_2) { enum es_result ret; =20 @@ -109,29 +108,45 @@ static enum es_result sev_es_ghcb_hv_call(struct ghcb= *ghcb, ghcb_set_sw_exit_info_1(ghcb, exit_info_1); ghcb_set_sw_exit_info_2(ghcb, exit_info_2); =20 - sev_es_wr_ghcb_msr(__pa(ghcb)); VMGEXIT(); =20 - if ((ghcb->save.sw_exit_info_1 & 0xffffffff) =3D=3D 1) { - u64 info =3D ghcb->save.sw_exit_info_2; - unsigned long v; - - info =3D ghcb->save.sw_exit_info_2; - v =3D info & SVM_EVTINJ_VEC_MASK; - - /* Check if exception information from hypervisor is sane. */ - if ((info & SVM_EVTINJ_VALID) && - ((v =3D=3D X86_TRAP_GP) || (v =3D=3D X86_TRAP_UD)) && - ((info & SVM_EVTINJ_TYPE_MASK) =3D=3D SVM_EVTINJ_TYPE_EXEPT)) { - ctxt->fi.vector =3D v; - if (info & SVM_EVTINJ_VALID_ERR) - ctxt->fi.error_code =3D info >> 32; - ret =3D ES_EXCEPTION; - } else { - ret =3D ES_VMM_ERROR; - } - } else { + if ((ghcb->save.sw_exit_info_1 & 0xffffffff) =3D=3D 1) + ret =3D ES_VMM_ERROR; + else ret =3D ES_OK; + + return ret; +} + +static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, + struct es_em_ctxt *ctxt, + u64 exit_code, u64 exit_info_1, + u64 exit_info_2) +{ + unsigned long v; + enum es_result ret; + u64 info; + + sev_es_wr_ghcb_msr(__pa(ghcb)); + + ret =3D sev_es_ghcb_hv_call_simple(ghcb, exit_code, exit_info_1, + exit_info_2); + if (ret =3D=3D ES_OK) + return ret; + + info =3D ghcb->save.sw_exit_info_2; + v =3D info & SVM_EVTINJ_VEC_MASK; + + /* Check if exception information from hypervisor is sane. */ + if ((info & SVM_EVTINJ_VALID) && + ((v =3D=3D X86_TRAP_GP) || (v =3D=3D X86_TRAP_UD)) && + ((info & SVM_EVTINJ_TYPE_MASK) =3D=3D SVM_EVTINJ_TYPE_EXEPT)) { + ctxt->fi.vector =3D v; + if (info & SVM_EVTINJ_VALID_ERR) + ctxt->fi.error_code =3D info >> 32; + ret =3D ES_EXCEPTION; + } else { + ret =3D ES_VMM_ERROR; } =20 return ret; diff --git a/drivers/hv/hv.c b/drivers/hv/hv.c index e83507f49676..97b21256a9db 100644 --- a/drivers/hv/hv.c +++ b/drivers/hv/hv.c @@ -8,6 +8,7 @@ */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 +#include #include #include #include @@ -136,17 +137,24 @@ int hv_synic_alloc(void) tasklet_init(&hv_cpu->msg_dpc, vmbus_on_msg_dpc, (unsigned long) hv_cpu); =20 - hv_cpu->synic_message_page =3D - (void *)get_zeroed_page(GFP_ATOMIC); - if (hv_cpu->synic_message_page =3D=3D NULL) { - pr_err("Unable to allocate SYNIC message page\n"); - goto err; - } + /* + * Synic message and event pages are allocated by paravisor. + * Skip these pages allocation here. + */ + if (!hv_isolation_type_snp()) { + hv_cpu->synic_message_page =3D + (void *)get_zeroed_page(GFP_ATOMIC); + if (hv_cpu->synic_message_page =3D=3D NULL) { + pr_err("Unable to allocate SYNIC message page\n"); + goto err; + } =20 - hv_cpu->synic_event_page =3D (void *)get_zeroed_page(GFP_ATOMIC); - if (hv_cpu->synic_event_page =3D=3D NULL) { - pr_err("Unable to allocate SYNIC event page\n"); - goto err; + hv_cpu->synic_event_page =3D + (void *)get_zeroed_page(GFP_ATOMIC); + if (hv_cpu->synic_event_page =3D=3D NULL) { + pr_err("Unable to allocate SYNIC event page\n"); + goto err; + } } =20 hv_cpu->post_msg_page =3D (void *)get_zeroed_page(GFP_ATOMIC); @@ -199,26 +207,43 @@ void hv_synic_enable_regs(unsigned int cpu) union hv_synic_scontrol sctrl; =20 /* Setup the Synic's message page */ - simp.as_uint64 =3D hv_get_register(HV_REGISTER_SIMP); + hv_get_simp(simp.as_uint64); simp.simp_enabled =3D 1; - simp.base_simp_gpa =3D virt_to_phys(hv_cpu->synic_message_page) - >> HV_HYP_PAGE_SHIFT; =20 - hv_set_register(HV_REGISTER_SIMP, simp.as_uint64); + if (hv_isolation_type_snp()) { + hv_cpu->synic_message_page + =3D memremap(simp.base_simp_gpa << HV_HYP_PAGE_SHIFT, + HV_HYP_PAGE_SIZE, MEMREMAP_WB); + if (!hv_cpu->synic_message_page) + pr_err("Fail to map syinc message page.\n"); + } else { + simp.base_simp_gpa =3D virt_to_phys(hv_cpu->synic_message_page) + >> HV_HYP_PAGE_SHIFT; + } + + hv_set_simp(simp.as_uint64); =20 /* Setup the Synic's event page */ - siefp.as_uint64 =3D hv_get_register(HV_REGISTER_SIEFP); + hv_get_siefp(siefp.as_uint64); siefp.siefp_enabled =3D 1; - siefp.base_siefp_gpa =3D virt_to_phys(hv_cpu->synic_event_page) - >> HV_HYP_PAGE_SHIFT; =20 - hv_set_register(HV_REGISTER_SIEFP, siefp.as_uint64); + if (hv_isolation_type_snp()) { + hv_cpu->synic_event_page =3D + memremap(siefp.base_siefp_gpa << HV_HYP_PAGE_SHIFT, + HV_HYP_PAGE_SIZE, MEMREMAP_WB); + + if (!hv_cpu->synic_event_page) + pr_err("Fail to map syinc event page.\n"); + } else { + siefp.base_siefp_gpa =3D virt_to_phys(hv_cpu->synic_event_page) + >> HV_HYP_PAGE_SHIFT; + } + hv_set_siefp(siefp.as_uint64); =20 /* Setup the shared SINT. */ if (vmbus_irq !=3D -1) enable_percpu_irq(vmbus_irq, 0); - shared_sint.as_uint64 =3D hv_get_register(HV_REGISTER_SINT0 + - VMBUS_MESSAGE_SINT); + hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64); =20 shared_sint.vector =3D vmbus_interrupt; shared_sint.masked =3D false; @@ -233,14 +258,12 @@ void hv_synic_enable_regs(unsigned int cpu) #else shared_sint.auto_eoi =3D 0; #endif - hv_set_register(HV_REGISTER_SINT0 + VMBUS_MESSAGE_SINT, - shared_sint.as_uint64); + hv_set_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64); =20 /* Enable the global synic bit */ - sctrl.as_uint64 =3D hv_get_register(HV_REGISTER_SCONTROL); + hv_get_synic_state(sctrl.as_uint64); sctrl.enable =3D 1; - - hv_set_register(HV_REGISTER_SCONTROL, sctrl.as_uint64); + hv_set_synic_state(sctrl.as_uint64); } =20 int hv_synic_init(unsigned int cpu) @@ -257,37 +280,50 @@ int hv_synic_init(unsigned int cpu) */ void hv_synic_disable_regs(unsigned int cpu) { + struct hv_per_cpu_context *hv_cpu + =3D per_cpu_ptr(hv_context.cpu_context, cpu); union hv_synic_sint shared_sint; union hv_synic_simp simp; union hv_synic_siefp siefp; union hv_synic_scontrol sctrl; =20 - shared_sint.as_uint64 =3D hv_get_register(HV_REGISTER_SINT0 + - VMBUS_MESSAGE_SINT); - + hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64); shared_sint.masked =3D 1; + hv_set_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64); + =20 /* Need to correctly cleanup in the case of SMP!!! */ /* Disable the interrupt */ - hv_set_register(HV_REGISTER_SINT0 + VMBUS_MESSAGE_SINT, - shared_sint.as_uint64); + hv_get_simp(simp.as_uint64); =20 - simp.as_uint64 =3D hv_get_register(HV_REGISTER_SIMP); + /* + * In Isolation VM, sim and sief pages are allocated by + * paravisor. These pages also will be used by kdump + * kernel. So just reset enable bit here and keep page + * addresses. + */ simp.simp_enabled =3D 0; - simp.base_simp_gpa =3D 0; + if (hv_isolation_type_snp()) + memunmap(hv_cpu->synic_message_page); + else + simp.base_simp_gpa =3D 0; =20 - hv_set_register(HV_REGISTER_SIMP, simp.as_uint64); + hv_set_simp(simp.as_uint64); =20 - siefp.as_uint64 =3D hv_get_register(HV_REGISTER_SIEFP); + hv_get_siefp(siefp.as_uint64); siefp.siefp_enabled =3D 0; - siefp.base_siefp_gpa =3D 0; =20 - hv_set_register(HV_REGISTER_SIEFP, siefp.as_uint64); + if (hv_isolation_type_snp()) + memunmap(hv_cpu->synic_event_page); + else + siefp.base_siefp_gpa =3D 0; + + hv_set_siefp(siefp.as_uint64); =20 /* Disable the global synic bit */ - sctrl.as_uint64 =3D hv_get_register(HV_REGISTER_SCONTROL); + hv_get_synic_state(sctrl.as_uint64); sctrl.enable =3D 0; - hv_set_register(HV_REGISTER_SCONTROL, sctrl.as_uint64); + hv_set_synic_state(sctrl.as_uint64); =20 if (vmbus_irq !=3D -1) disable_percpu_irq(vmbus_irq); diff --git a/drivers/hv/hv_common.c b/drivers/hv/hv_common.c index c0d9048a4112..1fc82d237161 100644 --- a/drivers/hv/hv_common.c +++ b/drivers/hv/hv_common.c @@ -249,6 +249,12 @@ bool __weak hv_is_isolation_supported(void) } EXPORT_SYMBOL_GPL(hv_is_isolation_supported); =20 +bool __weak hv_isolation_type_snp(void) +{ + return false; +} +EXPORT_SYMBOL_GPL(hv_isolation_type_snp); + void __weak hv_setup_vmbus_handler(void (*handler)(void)) { } diff --git a/include/asm-generic/mshyperv.h b/include/asm-generic/mshyperv.h index aa55447b9700..04a687d95eac 100644 --- a/include/asm-generic/mshyperv.h +++ b/include/asm-generic/mshyperv.h @@ -24,6 +24,7 @@ #include #include #include +#include #include =20 struct ms_hyperv_info { @@ -54,6 +55,7 @@ extern void __percpu **hyperv_pcpu_output_arg; =20 extern u64 hv_do_hypercall(u64 control, void *inputaddr, void *outputaddr); extern u64 hv_do_fast_hypercall8(u16 control, u64 input8); +extern bool hv_isolation_type_snp(void); =20 /* Helper functions that provide a consistent pattern for checking Hyper-V= hypercall status. */ static inline int hv_result(u64 status) @@ -148,7 +150,7 @@ static inline void vmbus_signal_eom(struct hv_message *= msg, u32 old_msg_type) * possibly deliver another msg from the * hypervisor */ - hv_set_register(HV_REGISTER_EOM, 0); + hv_signal_eom(old_msg_type); } } =20 --=20 2.25.1 From nobody Sun May 5 00:53:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1630084926; cv=none; d=zohomail.com; s=zohoarc; b=Do8MkJb5Y4xjBBlN/1EWV3OdrJZxCVQI2/gPtJjz8R5+7PUirk2KW8ICFR4gZvWZZQn95tBgUC1PzV27LXNFA9zLFVVKXMmq/IRV4EQ7uiLu/vpCGH+ze2oJf3M3vueVZT2EPcm/HYDtJIPS6wPgjdvQhXjR3SoG44InM7tk1JQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630084926; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ob17ye6fnB7930R2FL8arcDHdt3yZk08j/bIRN4xoKw=; b=HmpHrGS/t9JLvl0QuhIi2DdcNBTyKkUSQlPpUoRUqY3FMJsegn6Cw9GqJNbYbj2gEj4R5kelLmS07ckh5q+pCSCkHbulTfOtmHm2FPTDlXiGnmcf8wEtL9W4XUFYWDspXkKjPggv76/ZqMPI/wBD7a9peG3ffPOWgOwTxCNg0TE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1630084926896461.74136608940796; Fri, 27 Aug 2021 10:22:06 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.173808.317156 (Exim 4.92) (envelope-from ) id 1mJfYA-0003in-H1; Fri, 27 Aug 2021 17:21:54 +0000 Received: by outflank-mailman (output) from mailman id 173808.317156; Fri, 27 Aug 2021 17:21:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfYA-0003iW-Bz; Fri, 27 Aug 2021 17:21:54 +0000 Received: by outflank-mailman (input) for mailman id 173808; Fri, 27 Aug 2021 17:21:52 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfY8-00016Q-NH for xen-devel@lists.xenproject.org; Fri, 27 Aug 2021 17:21:52 +0000 Received: from mail-pj1-x1036.google.com (unknown [2607:f8b0:4864:20::1036]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 73887b7c-8fbc-410a-bad8-3c15feebec73; Fri, 27 Aug 2021 17:21:36 +0000 (UTC) Received: by mail-pj1-x1036.google.com with SMTP id u13-20020a17090abb0db0290177e1d9b3f7so9339307pjr.1 for ; Fri, 27 Aug 2021 10:21:36 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:36:ef50:8fcd:44d1:eb17]) by smtp.gmail.com with ESMTPSA id f5sm7155015pjo.23.2021.08.27.10.21.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Aug 2021 10:21:35 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 73887b7c-8fbc-410a-bad8-3c15feebec73 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ob17ye6fnB7930R2FL8arcDHdt3yZk08j/bIRN4xoKw=; b=Acq1bVUvmaPyZlZjmDtywazFIpInMJ5l50PEA2Nt3bFOezzfV0cJXj0UCstXCzAMBj KfGQXE6kdqUIrk3FKEI4LK3MOaU/qt2lKq1m9PTltB806b7/cSj9E7RSKQ2auAqcybd+ 9wAlDTj81JPxOqaCrr18me0IQCBb6qT7513xzCPfvIdPqnk2WNtVKQlv/AudOf4dMoYS U3cOSF+ygSE19w5mBvsH5pTBBooic+JayU5NnWQ9mvd5PKbJuY22vk3nxxSot8AvIOSg udI302S3dgA4YRY4l76sBeRQT1mqw0wR1intt/aaKxosHTOVgz1TrwlP2iTngJ8FsGBk FBoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ob17ye6fnB7930R2FL8arcDHdt3yZk08j/bIRN4xoKw=; b=N/6v2gy5bMzZTdaR3jN6PY6HkbZ8+AE1BzqDquPoqJc5VSDF+pUpBm7XXv1ImC6h6R ZTUioXcDPG3fUI8eblz9OP2/txKksKlkOkTvHW1i9SZK+La3mlPXUM8CCDPq1QVETyU4 FlOdB4IbkEnQfOdpHHZNc8p/szqT5+c+04Gd+sdlIPWf1cc4cVLU9WCCI/ZZOiBVJENU +fs+lYiVj+eK9cvY1FbIaViQqrsKR3ypyYdPsWL3QtLXI4T0IXyah1nD7kfFLBIR3Dts 7viwFZbs81ATw5qWBk409nQlYzVk0HBd4CA7AXZNE5vRRKoH7LYCZAsnDkba4jfnS36g OFUw== X-Gm-Message-State: AOAM533gCuFI4sJkpS9dtz3DJVIBok9AtUH5DMxjAFi7RxoUzUFcXDlH kkty8XS6FzuXK3LefrJPk+U= X-Google-Smtp-Source: ABdhPJwGMkg3bSRTf7M6vFTQbDZzjMqErLrtp6BhB2Bjhure2g65PdQZullrEVECsMos6PPHsE5QZg== X-Received: by 2002:a17:902:7203:b0:133:ec6b:6c16 with SMTP id ba3-20020a170902720300b00133ec6b6c16mr9550118plb.67.1630084895682; Fri, 27 Aug 2021 10:21:35 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, catalin.marinas@arm.com, will@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, gregkh@linuxfoundation.org, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, brijesh.singh@amd.com, thomas.lendacky@amd.com, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, hannes@cmpxchg.org, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, linux-arm-kernel@lists.infradead.org, xen-devel@lists.xenproject.org, rientjes@google.com, ardb@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V4 06/13] hyperv: Add ghcb hvcall support for SNP VM Date: Fri, 27 Aug 2021 13:21:04 -0400 Message-Id: <20210827172114.414281-7-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210827172114.414281-1-ltykernel@gmail.com> References: <20210827172114.414281-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1630084927753100005 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan hyperv provides ghcb hvcall to handle VMBus HVCALL_SIGNAL_EVENT and HVCALL_POST_MESSAGE msg in SNP Isolation VM. Add such support. Signed-off-by: Tianyu Lan --- Change since v3: * Add hv_ghcb_hypercall() stub function to avoid compile error for ARM. --- arch/x86/hyperv/ivm.c | 71 ++++++++++++++++++++++++++++++++++ drivers/hv/connection.c | 6 ++- drivers/hv/hv.c | 8 +++- drivers/hv/hv_common.c | 6 +++ include/asm-generic/mshyperv.h | 1 + 5 files changed, 90 insertions(+), 2 deletions(-) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index f56fe4f73000..e761c67e2218 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -17,10 +17,81 @@ #include #include =20 +#define GHCB_USAGE_HYPERV_CALL 1 + union hv_ghcb { struct ghcb ghcb; + struct { + u64 hypercalldata[509]; + u64 outputgpa; + union { + union { + struct { + u32 callcode : 16; + u32 isfast : 1; + u32 reserved1 : 14; + u32 isnested : 1; + u32 countofelements : 12; + u32 reserved2 : 4; + u32 repstartindex : 12; + u32 reserved3 : 4; + }; + u64 asuint64; + } hypercallinput; + union { + struct { + u16 callstatus; + u16 reserved1; + u32 elementsprocessed : 12; + u32 reserved2 : 20; + }; + u64 asunit64; + } hypercalloutput; + }; + u64 reserved2; + } hypercall; } __packed __aligned(HV_HYP_PAGE_SIZE); =20 +u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_si= ze) +{ + union hv_ghcb *hv_ghcb; + void **ghcb_base; + unsigned long flags; + + if (!hv_ghcb_pg) + return -EFAULT; + + WARN_ON(in_nmi()); + + local_irq_save(flags); + ghcb_base =3D (void **)this_cpu_ptr(hv_ghcb_pg); + hv_ghcb =3D (union hv_ghcb *)*ghcb_base; + if (!hv_ghcb) { + local_irq_restore(flags); + return -EFAULT; + } + + hv_ghcb->ghcb.protocol_version =3D GHCB_PROTOCOL_MAX; + hv_ghcb->ghcb.ghcb_usage =3D GHCB_USAGE_HYPERV_CALL; + + hv_ghcb->hypercall.outputgpa =3D (u64)output; + hv_ghcb->hypercall.hypercallinput.asuint64 =3D 0; + hv_ghcb->hypercall.hypercallinput.callcode =3D control; + + if (input_size) + memcpy(hv_ghcb->hypercall.hypercalldata, input, input_size); + + VMGEXIT(); + + hv_ghcb->ghcb.ghcb_usage =3D 0xffffffff; + memset(hv_ghcb->ghcb.save.valid_bitmap, 0, + sizeof(hv_ghcb->ghcb.save.valid_bitmap)); + + local_irq_restore(flags); + + return hv_ghcb->hypercall.hypercalloutput.callstatus; +} + void hv_ghcb_msr_write(u64 msr, u64 value) { union hv_ghcb *hv_ghcb; diff --git a/drivers/hv/connection.c b/drivers/hv/connection.c index 5e479d54918c..6d315c1465e0 100644 --- a/drivers/hv/connection.c +++ b/drivers/hv/connection.c @@ -447,6 +447,10 @@ void vmbus_set_event(struct vmbus_channel *channel) =20 ++channel->sig_events; =20 - hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event); + if (hv_isolation_type_snp()) + hv_ghcb_hypercall(HVCALL_SIGNAL_EVENT, &channel->sig_event, + NULL, sizeof(u64)); + else + hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event); } EXPORT_SYMBOL_GPL(vmbus_set_event); diff --git a/drivers/hv/hv.c b/drivers/hv/hv.c index 97b21256a9db..d4531c64d9d3 100644 --- a/drivers/hv/hv.c +++ b/drivers/hv/hv.c @@ -98,7 +98,13 @@ int hv_post_message(union hv_connection_id connection_id, aligned_msg->payload_size =3D payload_size; memcpy((void *)aligned_msg->payload, payload, payload_size); =20 - status =3D hv_do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL); + if (hv_isolation_type_snp()) + status =3D hv_ghcb_hypercall(HVCALL_POST_MESSAGE, + (void *)aligned_msg, NULL, + sizeof(struct hv_input_post_message)); + else + status =3D hv_do_hypercall(HVCALL_POST_MESSAGE, + aligned_msg, NULL); =20 /* Preemption must remain disabled until after the hypercall * so some other thread can't get scheduled onto this cpu and diff --git a/drivers/hv/hv_common.c b/drivers/hv/hv_common.c index 1fc82d237161..7be173a99f27 100644 --- a/drivers/hv/hv_common.c +++ b/drivers/hv/hv_common.c @@ -289,3 +289,9 @@ void __weak hyperv_cleanup(void) { } EXPORT_SYMBOL_GPL(hyperv_cleanup); + +u64 __weak hv_ghcb_hypercall(u64 control, void *input, void *output, u32 i= nput_size) +{ + return HV_STATUS_INVALID_PARAMETER; +} +EXPORT_SYMBOL_GPL(hv_ghcb_hypercall); diff --git a/include/asm-generic/mshyperv.h b/include/asm-generic/mshyperv.h index 04a687d95eac..0da45807c36a 100644 --- a/include/asm-generic/mshyperv.h +++ b/include/asm-generic/mshyperv.h @@ -250,6 +250,7 @@ bool hv_is_hibernation_supported(void); enum hv_isolation_type hv_get_isolation_type(void); bool hv_is_isolation_supported(void); bool hv_isolation_type_snp(void); +u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_si= ze); void hyperv_cleanup(void); bool hv_query_ext_cap(u64 cap_query); #else /* CONFIG_HYPERV */ --=20 2.25.1 From nobody Sun May 5 00:53:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1630084933; cv=none; d=zohomail.com; s=zohoarc; b=dixaCtY6NKaOtenplq83zNUXjtRoa7IFVwHQJCLRYD/wjRX7bYgwXfzAt2DpWHSydB27DSXZTaJpoQEAoQdWPI4cpY5nynQ8iv924H4MXZKCYXSs3Jp07rPtaRezFrJOhm09ueTU4L96fISknGokX1upEle1JL/6f7mOO8B3OHY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630084933; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=IDv52uhOHnM52YSplOEizgvrMcaCG52xTZqX8PjRtjY=; b=notohldxKq6fnRMuCBBV/qz7pnu0/jy+OKXmkEJZ1XtfiykstfrgAPlhBEaYRWxKx/LBQeFQlm3UZRvgBzLXEHY+eylknGEHVKxEa0spWKiIJZfKf0wFIZgEPOq/YUaDJsofWiG8DDk11FmMJs6QBXrmstM+I0H4d4zITLWS9Rg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1630084933222449.56523931199956; Fri, 27 Aug 2021 10:22:13 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.173813.317167 (Exim 4.92) (envelope-from ) id 1mJfYE-0004Ff-Tt; Fri, 27 Aug 2021 17:21:58 +0000 Received: by outflank-mailman (output) from mailman id 173813.317167; Fri, 27 Aug 2021 17:21:58 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfYE-0004FR-Nr; Fri, 27 Aug 2021 17:21:58 +0000 Received: by outflank-mailman (input) for mailman id 173813; Fri, 27 Aug 2021 17:21:57 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfYD-00016Q-Ne for xen-devel@lists.xenproject.org; Fri, 27 Aug 2021 17:21:57 +0000 Received: from mail-pf1-x42b.google.com (unknown [2607:f8b0:4864:20::42b]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id a97efa3d-a27b-4e67-adc2-57c68b97bb78; Fri, 27 Aug 2021 17:21:39 +0000 (UTC) Received: by mail-pf1-x42b.google.com with SMTP id 2so6225960pfo.8 for ; Fri, 27 Aug 2021 10:21:39 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:36:ef50:8fcd:44d1:eb17]) by smtp.gmail.com with ESMTPSA id f5sm7155015pjo.23.2021.08.27.10.21.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Aug 2021 10:21:38 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: a97efa3d-a27b-4e67-adc2-57c68b97bb78 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IDv52uhOHnM52YSplOEizgvrMcaCG52xTZqX8PjRtjY=; b=W3A72vwZ3Ghwpp2o31CalwDckdSlHrw8StpK5AyjpmRfd7CoTWRdKx9qXGzOjb4pj3 NCSk+7IZAD3LedbDJg025YQV6OI40vLBcSKr0wrsGn6eizyE2OTRY7/X40qbI132Q8la icbgRcIxFmG914HjGbujHrewWgk176VEDV+x/QdzOvheP0TwfLTweIzL9uBVd3yVDUn2 vSmaSU6KUJ257VBVrC46n3gE5fJnAmuxOXfrkO29VPSz5UDjxNj5n6hNF3QnSI8ZIMvx LPiBV+UQ4WTAkCIT/6Qzy9PhNFbXgoU18387LZmAfrNkr5IiukCeXLXFomvnet7ilIBD 6Tkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IDv52uhOHnM52YSplOEizgvrMcaCG52xTZqX8PjRtjY=; b=abXwapaIMsFocZpucVCUcVJN4SObh7BKMVTFQk76yiBPA3WyzKDsaioTjzCbaQtqNt Yn+JwAn9KkQJGL8cTiyFEHT2IMP1x1hgumY9yD5qDCtIfjM99jf57PIB9CE+8MouUM/B fQWFw5M97/Gq5huBspiTdF3LvDH7JS9wg722nwzGZAHUg0QM0FU5oBMfzp2/5Wl4Zr12 6+x3D9o/n2jsdYw0xQulOvBNnhSfSCl8FppEIlFc7qXysQv/NvGEVLTNVPSxDQX3WfrU 9oz191eYd1/xCiZwgYrFADgdHFm0dJW2wNFOOMmxzquXiE7wEBx07RygxfdHg2x8eaUm ZbCA== X-Gm-Message-State: AOAM531y6f67YuDbYoh1mJ+uDKEI/QEab7D+5j++sjoXFzj9/3dLr+fq PQ0FlWLTTpWdYjLO2gzmaBk= X-Google-Smtp-Source: ABdhPJzz68J2ef2NxKZvaNs5SyHPyYKCGSvy1fUWoS56/s8IKaqxW9MfFpxUlMb01fo8vSumjFMHoQ== X-Received: by 2002:aa7:864a:0:b0:3ee:a4f6:af02 with SMTP id a10-20020aa7864a000000b003eea4f6af02mr10062820pfo.23.1630084898734; Fri, 27 Aug 2021 10:21:38 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, catalin.marinas@arm.com, will@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, gregkh@linuxfoundation.org, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, brijesh.singh@amd.com, thomas.lendacky@amd.com, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, hannes@cmpxchg.org, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, linux-arm-kernel@lists.infradead.org, xen-devel@lists.xenproject.org, rientjes@google.com, ardb@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V4 07/13] hyperv/Vmbus: Add SNP support for VMbus channel initiate message Date: Fri, 27 Aug 2021 13:21:05 -0400 Message-Id: <20210827172114.414281-8-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210827172114.414281-1-ltykernel@gmail.com> References: <20210827172114.414281-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1630084934124100003 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan The monitor pages in the CHANNELMSG_INITIATE_CONTACT msg are shared with host in Isolation VM and so it's necessary to use hvcall to set them visible to host. In Isolation VM with AMD SEV SNP, the access address should be in the extra space which is above shared gpa boundary. So remap these pages into the extra address(pa + shared_gpa_boundary). Introduce monitor_pages_original[] in the struct vmbus_connection to store monitor page virtual address returned by hv_alloc_hyperv_ zeroed_page() and free monitor page via monitor_pages_original in the vmbus_disconnect(). The monitor_pages[] is to used to access monitor page and it is initialized to be equal with monitor_pages_ original. The monitor_pages[] will be overridden in the isolation VM with va of extra address. Signed-off-by: Tianyu Lan --- Change since v3: * Rename monitor_pages_va with monitor_pages_original * free monitor page via monitor_pages_original and monitor_pages is used to access monitor page. Change since v1: * Not remap monitor pages in the non-SNP isolation VM. --- drivers/hv/connection.c | 75 ++++++++++++++++++++++++++++++++++++--- drivers/hv/hyperv_vmbus.h | 1 + 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/drivers/hv/connection.c b/drivers/hv/connection.c index 6d315c1465e0..9a48d8115c87 100644 --- a/drivers/hv/connection.c +++ b/drivers/hv/connection.c @@ -19,6 +19,7 @@ #include #include #include +#include #include =20 #include "hyperv_vmbus.h" @@ -104,6 +105,12 @@ int vmbus_negotiate_version(struct vmbus_channel_msgin= fo *msginfo, u32 version) =20 msg->monitor_page1 =3D virt_to_phys(vmbus_connection.monitor_pages[0]); msg->monitor_page2 =3D virt_to_phys(vmbus_connection.monitor_pages[1]); + + if (hv_isolation_type_snp()) { + msg->monitor_page1 +=3D ms_hyperv.shared_gpa_boundary; + msg->monitor_page2 +=3D ms_hyperv.shared_gpa_boundary; + } + msg->target_vcpu =3D hv_cpu_number_to_vp_number(VMBUS_CONNECT_CPU); =20 /* @@ -148,6 +155,35 @@ int vmbus_negotiate_version(struct vmbus_channel_msgin= fo *msginfo, u32 version) return -ECONNREFUSED; } =20 + + if (hv_is_isolation_supported()) { + if (hv_isolation_type_snp()) { + vmbus_connection.monitor_pages[0] + =3D memremap(msg->monitor_page1, HV_HYP_PAGE_SIZE, + MEMREMAP_WB); + if (!vmbus_connection.monitor_pages[0]) + return -ENOMEM; + + vmbus_connection.monitor_pages[1] + =3D memremap(msg->monitor_page2, HV_HYP_PAGE_SIZE, + MEMREMAP_WB); + if (!vmbus_connection.monitor_pages[1]) { + memunmap(vmbus_connection.monitor_pages[0]); + return -ENOMEM; + } + } + + /* + * Set memory host visibility hvcall smears memory + * and so zero monitor pages here. + */ + memset(vmbus_connection.monitor_pages[0], 0x00, + HV_HYP_PAGE_SIZE); + memset(vmbus_connection.monitor_pages[1], 0x00, + HV_HYP_PAGE_SIZE); + + } + return ret; } =20 @@ -159,6 +195,7 @@ int vmbus_connect(void) struct vmbus_channel_msginfo *msginfo =3D NULL; int i, ret =3D 0; __u32 version; + u64 pfn[2]; =20 /* Initialize the vmbus connection */ vmbus_connection.conn_state =3D CONNECTING; @@ -216,6 +253,21 @@ int vmbus_connect(void) goto cleanup; } =20 + vmbus_connection.monitor_pages_original[0] + =3D vmbus_connection.monitor_pages[0]; + vmbus_connection.monitor_pages_original[1] + =3D vmbus_connection.monitor_pages[1]; + + if (hv_is_isolation_supported()) { + pfn[0] =3D virt_to_hvpfn(vmbus_connection.monitor_pages[0]); + pfn[1] =3D virt_to_hvpfn(vmbus_connection.monitor_pages[1]); + if (hv_mark_gpa_visibility(2, pfn, + VMBUS_PAGE_VISIBLE_READ_WRITE)) { + ret =3D -EFAULT; + goto cleanup; + } + } + msginfo =3D kzalloc(sizeof(*msginfo) + sizeof(struct vmbus_channel_initiate_contact), GFP_KERNEL); @@ -284,6 +336,8 @@ int vmbus_connect(void) =20 void vmbus_disconnect(void) { + u64 pfn[2]; + /* * First send the unload request to the host. */ @@ -303,10 +357,23 @@ void vmbus_disconnect(void) vmbus_connection.int_page =3D NULL; } =20 - hv_free_hyperv_page((unsigned long)vmbus_connection.monitor_pages[0]); - hv_free_hyperv_page((unsigned long)vmbus_connection.monitor_pages[1]); - vmbus_connection.monitor_pages[0] =3D NULL; - vmbus_connection.monitor_pages[1] =3D NULL; + if (hv_is_isolation_supported()) { + memunmap(vmbus_connection.monitor_pages[0]); + memunmap(vmbus_connection.monitor_pages[1]); + + pfn[0] =3D virt_to_hvpfn(vmbus_connection.monitor_pages[0]); + pfn[1] =3D virt_to_hvpfn(vmbus_connection.monitor_pages[1]); + hv_mark_gpa_visibility(2, pfn, VMBUS_PAGE_NOT_VISIBLE); + } + + hv_free_hyperv_page((unsigned long) + vmbus_connection.monitor_pages_original[0]); + hv_free_hyperv_page((unsigned long) + vmbus_connection.monitor_pages_original[1]); + vmbus_connection.monitor_pages_original[0] =3D + vmbus_connection.monitor_pages[0] =3D NULL; + vmbus_connection.monitor_pages_original[1] =3D + vmbus_connection.monitor_pages[1] =3D NULL; } =20 /* diff --git a/drivers/hv/hyperv_vmbus.h b/drivers/hv/hyperv_vmbus.h index 42f3d9d123a1..7cb11ef694da 100644 --- a/drivers/hv/hyperv_vmbus.h +++ b/drivers/hv/hyperv_vmbus.h @@ -240,6 +240,7 @@ struct vmbus_connection { * is child->parent notification */ struct hv_monitor_page *monitor_pages[2]; + void *monitor_pages_original[2]; struct list_head chn_msg_list; spinlock_t channelmsg_lock; =20 --=20 2.25.1 From nobody Sun May 5 00:53:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1630084937; cv=none; d=zohomail.com; s=zohoarc; b=krVjxDyyV8bK/xmrg+I/bXMp09zY9aNP8xmEeVAqUHRo7tQZVsElDnTsQYiqAsU6xdI+NvUrvpwzLvfTRNeD2NuYuhkWyZeLPgMp9aZVdxaQtHPsNHdQlByDNZAD3upNPnEGfvMa+d4/WFI5s1VYXA9MCe1ZvWzNBrnfTLx0RgE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630084937; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=4dx9A/B3m5ysOD2+VzBoH5w81i27I/BMJvu/dVa7Ytg=; b=X8xBwrFpc9sk+a96i0nm2peGyzhWqXgt5j57aoS0GXsDun8D67vytwBsixfcOs4N7fxAvU2WrOC8kLYpC/m6As+nUcU0f3T1xmX1OixPlksIZ8XE2aIuTPOO95iNnh7sQQujEV3K6Z3yeo5yfEyiVanrvv0YA/8eaAnu8tvn8Wo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1630084937228323.54157597649; Fri, 27 Aug 2021 10:22:17 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.173818.317177 (Exim 4.92) (envelope-from ) id 1mJfYK-0004oU-6B; Fri, 27 Aug 2021 17:22:04 +0000 Received: by outflank-mailman (output) from mailman id 173818.317177; Fri, 27 Aug 2021 17:22:04 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfYK-0004oD-1o; Fri, 27 Aug 2021 17:22:04 +0000 Received: by outflank-mailman (input) for mailman id 173818; Fri, 27 Aug 2021 17:22:02 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfYI-00016Q-Ny for xen-devel@lists.xenproject.org; Fri, 27 Aug 2021 17:22:02 +0000 Received: from mail-pl1-x631.google.com (unknown [2607:f8b0:4864:20::631]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 971e1a30-4c0c-42f9-90ac-9e3670196996; Fri, 27 Aug 2021 17:21:42 +0000 (UTC) Received: by mail-pl1-x631.google.com with SMTP id m17so4350645plc.6 for ; Fri, 27 Aug 2021 10:21:42 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:36:ef50:8fcd:44d1:eb17]) by smtp.gmail.com with ESMTPSA id f5sm7155015pjo.23.2021.08.27.10.21.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Aug 2021 10:21:41 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 971e1a30-4c0c-42f9-90ac-9e3670196996 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4dx9A/B3m5ysOD2+VzBoH5w81i27I/BMJvu/dVa7Ytg=; b=d6svu92zxjPI6sOGybaew8LGEzZQzVDWU73Js82UMmjRVPkircRHjJuTwevbiccRtw ZOHi8nITISKt1ONd0/XM2BKSMvAJmurxS00i1gBOaGuR027+Kyk7ZjDXFSFBUcP4bTCZ +mxTQlEetvL8su0/2uTAqa3IHgMTEZ7T+82SBtlXhSmpM7sNg5mVuophr86XcTcFPq2/ w+EZ2tpFCYMRtm821G13jWV8IhGq8uTX0NcoMVXtQ+wE1Ha9RrqJUhReEosqrotUQCoQ IGgYsbYCnWTzEe07DlxvZ+deZdgduAL0Zs/sUPNCxPdbBV8fooS21ucdofpUlseeIhpm zgng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4dx9A/B3m5ysOD2+VzBoH5w81i27I/BMJvu/dVa7Ytg=; b=ss7ueBwSmeI4DA1+QkMG5zmSNQ7jPBUuUhs1X4ob2mSLDHjeF+/CEOw6FYpkj6goBb zMEb96Hw+hDwwVPNIXli3t8JQ6h3/IbXns0/RrFY7mESHFsbCwxr6IRM4Z88DYD5D3HW TwT1QiXbX158LEcmW6q2pkh+0nN9vNHxXHh4+cSJ1y3l+dr85H3z6ZMWQAO106kZi3Lb M8KZSHhxAVCn2P5S6GrgEDRgA7VP3g0Id5bYxsklPkMKBd6g90ceKsAN3pVU0We4tKBa XI9QJnCQgJyI56pOt9+7/Yx5v9ZghxN7Cx3xl2X4JAKUpCIfldgc/wwt5SA+MoeHArq1 +PFA== X-Gm-Message-State: AOAM5308dSezwFi9IyLwWN5iHCAuX6yrFb+Z7g/Ywe/fEOnmdiQ3NMw0 MF89gRYqqrKhxoMLwBtujFo= X-Google-Smtp-Source: ABdhPJwIY8QSecpkOYquBiJzV0Q22b9scRKmqtTl7kB/KD182fzIRPnUqvHG1miAckAMpUlV+5Pz6g== X-Received: by 2002:a17:902:f704:b029:11a:cdee:490 with SMTP id h4-20020a170902f704b029011acdee0490mr9529029plo.37.1630084901556; Fri, 27 Aug 2021 10:21:41 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, catalin.marinas@arm.com, will@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, gregkh@linuxfoundation.org, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, brijesh.singh@amd.com, thomas.lendacky@amd.com, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, hannes@cmpxchg.org, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, linux-arm-kernel@lists.infradead.org, xen-devel@lists.xenproject.org, rientjes@google.com, ardb@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V4 08/13] hyperv/vmbus: Initialize VMbus ring buffer for Isolation VM Date: Fri, 27 Aug 2021 13:21:06 -0400 Message-Id: <20210827172114.414281-9-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210827172114.414281-1-ltykernel@gmail.com> References: <20210827172114.414281-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1630084938533100001 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan VMbus ring buffer are shared with host and it's need to be accessed via extra address space of Isolation VM with AMD SNP support. This patch is to map the ring buffer address in extra address space via vmap_pfn(). Hyperv set memory host visibility hvcall smears data in the ring buffer and so reset the ring buffer memory to zero after mapping. Signed-off-by: Tianyu Lan --- Change since v3: * Remove hv_ringbuffer_post_init(), merge map operation for Isolation VM into hv_ringbuffer_init() * Call hv_ringbuffer_init() after __vmbus_establish_gpadl(). --- drivers/hv/Kconfig | 1 + drivers/hv/channel.c | 19 +++++++------- drivers/hv/ring_buffer.c | 56 ++++++++++++++++++++++++++++++---------- 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/drivers/hv/Kconfig b/drivers/hv/Kconfig index d1123ceb38f3..dd12af20e467 100644 --- a/drivers/hv/Kconfig +++ b/drivers/hv/Kconfig @@ -8,6 +8,7 @@ config HYPERV || (ARM64 && !CPU_BIG_ENDIAN)) select PARAVIRT select X86_HV_CALLBACK_VECTOR if X86 + select VMAP_PFN help Select this option to run Linux as a Hyper-V client operating system. diff --git a/drivers/hv/channel.c b/drivers/hv/channel.c index 82650beb3af0..81f8629e4491 100644 --- a/drivers/hv/channel.c +++ b/drivers/hv/channel.c @@ -679,15 +679,6 @@ static int __vmbus_open(struct vmbus_channel *newchann= el, if (!newchannel->max_pkt_size) newchannel->max_pkt_size =3D VMBUS_DEFAULT_MAX_PKT_SIZE; =20 - err =3D hv_ringbuffer_init(&newchannel->outbound, page, send_pages, 0); - if (err) - goto error_clean_ring; - - err =3D hv_ringbuffer_init(&newchannel->inbound, &page[send_pages], - recv_pages, newchannel->max_pkt_size); - if (err) - goto error_clean_ring; - /* Establish the gpadl for the ring buffer */ newchannel->ringbuffer_gpadlhandle =3D 0; =20 @@ -699,6 +690,16 @@ static int __vmbus_open(struct vmbus_channel *newchann= el, if (err) goto error_clean_ring; =20 + err =3D hv_ringbuffer_init(&newchannel->outbound, + page, send_pages, 0); + if (err) + goto error_free_gpadl; + + err =3D hv_ringbuffer_init(&newchannel->inbound, &page[send_pages], + recv_pages, newchannel->max_pkt_size); + if (err) + goto error_free_gpadl; + /* Create and init the channel open message */ open_info =3D kzalloc(sizeof(*open_info) + sizeof(struct vmbus_channel_open_channel), diff --git a/drivers/hv/ring_buffer.c b/drivers/hv/ring_buffer.c index 2aee356840a2..24d64d18eb65 100644 --- a/drivers/hv/ring_buffer.c +++ b/drivers/hv/ring_buffer.c @@ -17,6 +17,8 @@ #include #include #include +#include +#include =20 #include "hyperv_vmbus.h" =20 @@ -183,8 +185,10 @@ void hv_ringbuffer_pre_init(struct vmbus_channel *chan= nel) int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, struct page *pages, u32 page_cnt, u32 max_pkt_size) { - int i; struct page **pages_wraparound; + unsigned long *pfns_wraparound; + u64 pfn; + int i; =20 BUILD_BUG_ON((sizeof(struct hv_ring_buffer) !=3D PAGE_SIZE)); =20 @@ -192,23 +196,49 @@ int hv_ringbuffer_init(struct hv_ring_buffer_info *ri= ng_info, * First page holds struct hv_ring_buffer, do wraparound mapping for * the rest. */ - pages_wraparound =3D kcalloc(page_cnt * 2 - 1, sizeof(struct page *), - GFP_KERNEL); - if (!pages_wraparound) - return -ENOMEM; + if (hv_isolation_type_snp()) { + pfn =3D page_to_pfn(pages) + + HVPFN_DOWN(ms_hyperv.shared_gpa_boundary); =20 - pages_wraparound[0] =3D pages; - for (i =3D 0; i < 2 * (page_cnt - 1); i++) - pages_wraparound[i + 1] =3D &pages[i % (page_cnt - 1) + 1]; + pfns_wraparound =3D kcalloc(page_cnt * 2 - 1, + sizeof(unsigned long), GFP_KERNEL); + if (!pfns_wraparound) + return -ENOMEM; =20 - ring_info->ring_buffer =3D (struct hv_ring_buffer *) - vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, PAGE_KERNEL); + pfns_wraparound[0] =3D pfn; + for (i =3D 0; i < 2 * (page_cnt - 1); i++) + pfns_wraparound[i + 1] =3D pfn + i % (page_cnt - 1) + 1; =20 - kfree(pages_wraparound); + ring_info->ring_buffer =3D (struct hv_ring_buffer *) + vmap_pfn(pfns_wraparound, page_cnt * 2 - 1, + PAGE_KERNEL); + kfree(pfns_wraparound); =20 + if (!ring_info->ring_buffer) + return -ENOMEM; + + /* Zero ring buffer after setting memory host visibility. */ + memset(ring_info->ring_buffer, 0x00, + HV_HYP_PAGE_SIZE * page_cnt); + } else { + pages_wraparound =3D kcalloc(page_cnt * 2 - 1, + sizeof(struct page *), + GFP_KERNEL); + + pages_wraparound[0] =3D pages; + for (i =3D 0; i < 2 * (page_cnt - 1); i++) + pages_wraparound[i + 1] =3D + &pages[i % (page_cnt - 1) + 1]; + + ring_info->ring_buffer =3D (struct hv_ring_buffer *) + vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, + PAGE_KERNEL); + + kfree(pages_wraparound); + if (!ring_info->ring_buffer) + return -ENOMEM; + } =20 - if (!ring_info->ring_buffer) - return -ENOMEM; =20 ring_info->ring_buffer->read_index =3D ring_info->ring_buffer->write_index =3D 0; --=20 2.25.1 From nobody Sun May 5 00:53:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1630084942; cv=none; d=zohomail.com; s=zohoarc; b=fvMBtVPFw2Jg4xGVN/DLcaI86ZFuoROQRleNBWnjJAqRGrv2dLf6hu1buSvTV5L50rE7sAMDJ6uQEHo4LWePDGbWnQxWb/XKKNf46plEvyJO+vDYcKHNwhaDrceQcj+17M1ZToPriWfcjPthEa2Tp0tA30J/q5WkTwS2mFY3wPQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630084942; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=QzDpud8qjO7DGAwSDqVcLhrCIaPCA+FlLB1tYtC1F1E=; b=fOw5YMsCKDDpA6MpQq5K0Dw3vVliW6kPhC4tYlqGm9G7zxtgtnhS62//Bmu0E16LO8dHFGtOowhQvhzimj95n+ju44sS/XIjJUyLZHF7HZDP/9o8rN+VvRJWbHWYXa14khv/goY7x0l/28NBEwmblQ66d3lp/NSxAYF5DN4Jpk4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1630084942556244.1413765733497; Fri, 27 Aug 2021 10:22:22 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.173823.317188 (Exim 4.92) (envelope-from ) id 1mJfYP-0005Rz-Ft; Fri, 27 Aug 2021 17:22:09 +0000 Received: by outflank-mailman (output) from mailman id 173823.317188; Fri, 27 Aug 2021 17:22:09 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfYP-0005Rq-Bi; Fri, 27 Aug 2021 17:22:09 +0000 Received: by outflank-mailman (input) for mailman id 173823; Fri, 27 Aug 2021 17:22:07 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfYN-00016Q-Nw for xen-devel@lists.xenproject.org; Fri, 27 Aug 2021 17:22:07 +0000 Received: from mail-pl1-x634.google.com (unknown [2607:f8b0:4864:20::634]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id bb8ffdf7-506e-44c6-affc-63f9c055b5d3; Fri, 27 Aug 2021 17:21:44 +0000 (UTC) Received: by mail-pl1-x634.google.com with SMTP id j2so4364855pll.1 for ; Fri, 27 Aug 2021 10:21:44 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:36:ef50:8fcd:44d1:eb17]) by smtp.gmail.com with ESMTPSA id f5sm7155015pjo.23.2021.08.27.10.21.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Aug 2021 10:21:43 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: bb8ffdf7-506e-44c6-affc-63f9c055b5d3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QzDpud8qjO7DGAwSDqVcLhrCIaPCA+FlLB1tYtC1F1E=; b=lzOMpy7gjTg0QsI9X1B0zDEO5w8A3tTHDaA3Newvm7Dl3CUxrum/hXz2a9YiW/zbl9 DWBNfsfAuWLVXW+6052ehJsSra7WuCaqDcfvuVRm/H1XSNDcmZV21gls+k03o8B15EvO WllzL/XxkMvOhuAI1ingj47d+lIZ1gqKkC9D8tlfwvTv21qcSq6y1ob7HKk5txM1NLLs ZHs1WkgWMZEeRaubjxDUO3RgjimMTGd3vjBVskF/18NptrHuYNJudZP4Ttd3US16Hi3t /3kG52lIvLDBHlCYO8E/XcfFGifWx7bIZTkC4gv5hSXuX9FWgCaJwm8f8Mjy2h7PQgiL HXmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QzDpud8qjO7DGAwSDqVcLhrCIaPCA+FlLB1tYtC1F1E=; b=DSRQMfk3XapHI27ROmtXvxOmpI5JbCA8ZsM+wZbafCMB0oHjUKQbIdxnhxQr6QqVFb tQvB/tftBKAL847a4CjiC+MVgmS6DzlUCz05LmEzm6Tvbk5ONsk0FYIteHh+EQ2ZlOwU jSzcmyxLOCh7jQJL7i7yQa5lyWtWrLogGl0RrvRADgnVl/wWOhzC8RBVPZA6x69Brvj/ VfM3ou8eQdtsfG2neAr5NV97/Hg1Cw8G5YggXd21iaw+i648eAoceI9Mycsf4zUnNE36 rIRoCRCcGganK7SWIfv1C7q7KdtSaEdpWHlVd/ut6XHDq2o1tCO7utROTPclkkLPiZZp qBfQ== X-Gm-Message-State: AOAM531u9sjO6Z4ItkQI/2edLIE2DkkswNk6X+rZ35tWNwCIKyjMIf3/ h1aQfk/KwTsxLjD8lpTRhJI= X-Google-Smtp-Source: ABdhPJyGYNb/dCSEWaVblCRuQOF/Z6LafmEQvufqDOpzfIgc7L3h2vFfPqAmiAB3tW+rcK6DfA1aiQ== X-Received: by 2002:a17:902:bf49:b0:136:7033:8963 with SMTP id u9-20020a170902bf4900b0013670338963mr9726914pls.75.1630084904216; Fri, 27 Aug 2021 10:21:44 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, catalin.marinas@arm.com, will@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, gregkh@linuxfoundation.org, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, brijesh.singh@amd.com, thomas.lendacky@amd.com, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, hannes@cmpxchg.org, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, linux-arm-kernel@lists.infradead.org, xen-devel@lists.xenproject.org, rientjes@google.com, ardb@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V4 09/13] DMA: Add dma_map_decrypted/dma_unmap_encrypted() function Date: Fri, 27 Aug 2021 13:21:07 -0400 Message-Id: <20210827172114.414281-10-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210827172114.414281-1-ltykernel@gmail.com> References: <20210827172114.414281-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1630084943037100001 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan In Hyper-V Isolation VM with AMD SEV, swiotlb boucne buffer needs to be mapped into address space above vTOM and so introduce dma_map_decrypted/dma_unmap_encrypted() to map/unmap bounce buffer memory. The platform can populate man/unmap callback in the dma memory decrypted ops. The swiotlb bounce buffer PA will be returned to driver and used for DMA address. The new mapped virtual address is just to acess bounce buffer in the swiotlb code. PAs passed to DMA API still have backing struct page. Signed-off-by: Tianyu Lan --- include/linux/dma-map-ops.h | 9 +++++++++ kernel/dma/mapping.c | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index 0d53a96a3d64..01d60a024e45 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -71,6 +71,11 @@ struct dma_map_ops { unsigned long (*get_merge_boundary)(struct device *dev); }; =20 +struct dma_memory_decrypted_ops { + void *(*map)(void *addr, unsigned long size); + void (*unmap)(void *addr); +}; + #ifdef CONFIG_DMA_OPS #include =20 @@ -374,6 +379,10 @@ static inline void debug_dma_dump_mappings(struct devi= ce *dev) } #endif /* CONFIG_DMA_API_DEBUG */ =20 +void *dma_map_decrypted(void *addr, unsigned long size); +int dma_unmap_decrypted(void *addr, unsigned long size); + extern const struct dma_map_ops dma_dummy_ops; +extern struct dma_memory_decrypted_ops dma_memory_generic_decrypted_ops; =20 #endif /* _LINUX_DMA_MAP_OPS_H */ diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c index 2b06a809d0b9..6fb150dc1750 100644 --- a/kernel/dma/mapping.c +++ b/kernel/dma/mapping.c @@ -13,11 +13,13 @@ #include #include #include +#include #include "debug.h" #include "direct.h" =20 bool dma_default_coherent; =20 +struct dma_memory_decrypted_ops dma_memory_generic_decrypted_ops; /* * Managed DMA API */ @@ -736,3 +738,23 @@ unsigned long dma_get_merge_boundary(struct device *de= v) return ops->get_merge_boundary(dev); } EXPORT_SYMBOL_GPL(dma_get_merge_boundary); + +void *dma_map_decrypted(void *addr, unsigned long size) +{ + if (set_memory_decrypted((unsigned long)addr, + size / PAGE_SIZE)) + return NULL; + + if (dma_memory_generic_decrypted_ops.map) + return dma_memory_generic_decrypted_ops.map(addr, size); + else + return addr; +} + +int dma_unmap_encrypted(void *addr, unsigned long size) +{ + if (dma_memory_generic_decrypted_ops.unmap) + dma_memory_generic_decrypted_ops.unmap(addr); + + return set_memory_encrypted((unsigned long)addr, size / PAGE_SIZE); +} --=20 2.25.1 From nobody Sun May 5 00:53:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1630085581; cv=none; d=zohomail.com; s=zohoarc; b=bW90vdkKEhuw5LccWUCikNniY6MYAW8m5b+iZZ1jqRf+KPlr+okm8UVJhNBLqYTJ9c1F0l7t4q3DqvAmfhjmesritaLoy9e+MOl6TQJ2x4HQj9bqbj0iywBo3pZtZM1sGbEFSqeHiMBvXIn9e2gw9s9Ck02QmtdRXYEoWDDOx+E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630085581; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=6MHi30R96t1tpcfv0VvlaJ3ckWN83uvRHnkjJOo1l30=; b=mrp+nMwuCfynYTaP0NGAimaSR9YdNseM+kaI1shyyOUNQesqBVLuJnjUSoOVLxJs9ZctMi0NrtBozsbOz2Cku0BMP7Ueti0FKPF1REwPwZDVIt/Wa/IczkwrN6BWqA/e6wD/kZ8qh7lT6IjCowKWk1JWHZQNsfIJ+bNShhq6VS0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 16300855817171006.5930670448543; Fri, 27 Aug 2021 10:33:01 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.173881.317258 (Exim 4.92) (envelope-from ) id 1mJfij-0002wp-BO; Fri, 27 Aug 2021 17:32:49 +0000 Received: by outflank-mailman (output) from mailman id 173881.317258; Fri, 27 Aug 2021 17:32:49 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfij-0002wc-5H; Fri, 27 Aug 2021 17:32:49 +0000 Received: by outflank-mailman (input) for mailman id 173881; Fri, 27 Aug 2021 17:32:48 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfYS-00016Q-O9 for xen-devel@lists.xenproject.org; Fri, 27 Aug 2021 17:22:12 +0000 Received: from mail-pf1-x433.google.com (unknown [2607:f8b0:4864:20::433]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id edb059b1-e70d-4bf6-86cd-f17be81a1a9b; Fri, 27 Aug 2021 17:21:47 +0000 (UTC) Received: by mail-pf1-x433.google.com with SMTP id 2so6226282pfo.8 for ; Fri, 27 Aug 2021 10:21:47 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:36:ef50:8fcd:44d1:eb17]) by smtp.gmail.com with ESMTPSA id f5sm7155015pjo.23.2021.08.27.10.21.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Aug 2021 10:21:46 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: edb059b1-e70d-4bf6-86cd-f17be81a1a9b DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6MHi30R96t1tpcfv0VvlaJ3ckWN83uvRHnkjJOo1l30=; b=MOcte0DoDafRZO8ZtX2XcPp/LXoS4Y8Uc+1YFSueU47Od7ILMltiJ+jU/PD2Bem9A6 egV3QwhmmbWOrrE4pRxAPThWOWuRWBYL/V8suAXt/TOwDWJD5Txw1Ztj7UcKPNgn7b3S 64Dl/WlPUEwgmW/EQJOhf0kinfHFaEga3IVGbbzchLUVdnE4z7oulTK6s2gwUajS0jMO XCFXLk3TKwWljw55q7xE/CphXl0QiV/ldYVD2VzyXCnfh4Wlnba38D0RScL2guLBekg5 uuxnrzN988dSfELcvZBQj1oVLuif1sO8N9ozD7WDA31KjxT6WFD2rY7fqC29VP5ApsxF E/Lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6MHi30R96t1tpcfv0VvlaJ3ckWN83uvRHnkjJOo1l30=; b=AO141Q/+eow6HhHivpoaNi/tjy5RJ0S6gC7BacdPsJbqdPExI0KUr+fX7PIxq4YegO TSJiUMYjpK8k6oNxrXZ3oqF4IwnF9APN6X9LG/U+2fwd1l4jcw8inlsz8fOb8dFNv/Ck VFcT3SbxCsrnHDJ1UZNrX7Ew0xy7cG13oIX+QOhJ39QLNwTbocEBenxn3iT7M6S7Zh7r MN3cpjCirY6SpzZ0pwaPkJjZ2bLA4W7N+1lsQNh6ISPKnuq3OFCOOgWRIpdTGWRIz3Rq D0UY8ncKJi/YWAqCumPO4TP7TcmRNlrGHUQM8/OXStD34zs61cACkfiPBOFBwxfhmKEw AxsA== X-Gm-Message-State: AOAM530J8J89diZQi5L6MH/ywcEZhbW91FFlMtEcHdYHy3VdBdFV1ksD Tc3uAlTXuqCc0hoJsneN110= X-Google-Smtp-Source: ABdhPJzQcZQUCwo3EVIginZLbkS0j6tpa+TKdpTpirJZ9GqPVodAIhLCJiwv8MK3x0NUITnBw1JqCw== X-Received: by 2002:a63:534f:: with SMTP id t15mr8821683pgl.392.1630084906877; Fri, 27 Aug 2021 10:21:46 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, catalin.marinas@arm.com, will@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, gregkh@linuxfoundation.org, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, brijesh.singh@amd.com, thomas.lendacky@amd.com, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, hannes@cmpxchg.org, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, linux-arm-kernel@lists.infradead.org, xen-devel@lists.xenproject.org, rientjes@google.com, ardb@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V4 10/13] x86/Swiotlb: Add Swiotlb bounce buffer remap function for HV IVM Date: Fri, 27 Aug 2021 13:21:08 -0400 Message-Id: <20210827172114.414281-11-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210827172114.414281-1-ltykernel@gmail.com> References: <20210827172114.414281-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1630085582670100001 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan In Isolation VM with AMD SEV, bounce buffer needs to be accessed via extra address space which is above shared_gpa_boundary (E.G 39 bit address line) reported by Hyper-V CPUID ISOLATION_CONFIG. The access physical address will be original physical address + shared_gpa_boundary. The shared_gpa_boundary in the AMD SEV SNP spec is called virtual top of memory(vTOM). Memory addresses below vTOM are automatically treated as private while memory above vTOM is treated as shared. Use dma_map_decrypted() in the swiotlb code, store remap address returned and use the remap address to copy data from/to swiotlb bounce buffer. Signed-off-by: Tianyu Lan --- Change since v1: * Make swiotlb_init_io_tlb_mem() return error code and return error when dma_map_decrypted() fails. --- include/linux/swiotlb.h | 4 ++++ kernel/dma/swiotlb.c | 32 ++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index f507e3eacbea..584560ecaa8e 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -72,6 +72,9 @@ extern enum swiotlb_force swiotlb_force; * @end: The end address of the swiotlb memory pool. Used to do a quick * range check to see if the memory was in fact allocated by this * API. + * @vaddr: The vaddr of the swiotlb memory pool. The swiotlb + * memory pool may be remapped in the memory encrypted case and store + * virtual address for bounce buffer operation. * @nslabs: The number of IO TLB blocks (in groups of 64) between @start a= nd * @end. For default swiotlb, this is command line adjustable via * setup_io_tlb_npages. @@ -89,6 +92,7 @@ extern enum swiotlb_force swiotlb_force; struct io_tlb_mem { phys_addr_t start; phys_addr_t end; + void *vaddr; unsigned long nslabs; unsigned long used; unsigned int index; diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 1fa81c096c1d..29b6d888ef3b 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -176,7 +176,7 @@ void __init swiotlb_update_mem_attributes(void) memset(vaddr, 0, bytes); } =20 -static void swiotlb_init_io_tlb_mem(struct io_tlb_mem *mem, phys_addr_t st= art, +static int swiotlb_init_io_tlb_mem(struct io_tlb_mem *mem, phys_addr_t sta= rt, unsigned long nslabs, bool late_alloc) { void *vaddr =3D phys_to_virt(start); @@ -194,14 +194,21 @@ static void swiotlb_init_io_tlb_mem(struct io_tlb_mem= *mem, phys_addr_t start, mem->slots[i].alloc_size =3D 0; } =20 - set_memory_decrypted((unsigned long)vaddr, bytes >> PAGE_SHIFT); - memset(vaddr, 0, bytes); + mem->vaddr =3D dma_map_decrypted(vaddr, bytes); + if (!mem->vaddr) { + pr_err("Failed to decrypt memory.\n"); + return -ENOMEM; + } + + memset(mem->vaddr, 0, bytes); + return 0; } =20 int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verb= ose) { struct io_tlb_mem *mem; size_t alloc_size; + int ret; =20 if (swiotlb_force =3D=3D SWIOTLB_NO_FORCE) return 0; @@ -216,7 +223,11 @@ int __init swiotlb_init_with_tbl(char *tlb, unsigned l= ong nslabs, int verbose) panic("%s: Failed to allocate %zu bytes align=3D0x%lx\n", __func__, alloc_size, PAGE_SIZE); =20 - swiotlb_init_io_tlb_mem(mem, __pa(tlb), nslabs, false); + ret =3D swiotlb_init_io_tlb_mem(mem, __pa(tlb), nslabs, false); + if (ret) { + memblock_free(__pa(mem), alloc_size); + return ret; + } =20 io_tlb_default_mem =3D mem; if (verbose) @@ -304,6 +315,8 @@ int swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs) { struct io_tlb_mem *mem; + int size =3D get_order(struct_size(mem, slots, nslabs)); + int ret; =20 if (swiotlb_force =3D=3D SWIOTLB_NO_FORCE) return 0; @@ -312,12 +325,15 @@ swiotlb_late_init_with_tbl(char *tlb, unsigned long n= slabs) if (WARN_ON_ONCE(io_tlb_default_mem)) return -ENOMEM; =20 - mem =3D (void *)__get_free_pages(GFP_KERNEL, - get_order(struct_size(mem, slots, nslabs))); + mem =3D (void *)__get_free_pages(GFP_KERNEL, size); if (!mem) return -ENOMEM; =20 - swiotlb_init_io_tlb_mem(mem, virt_to_phys(tlb), nslabs, true); + ret =3D swiotlb_init_io_tlb_mem(mem, virt_to_phys(tlb), nslabs, true); + if (ret) { + free_pages((unsigned long)mem, size); + return ret; + } =20 io_tlb_default_mem =3D mem; swiotlb_print_info(); @@ -360,7 +376,7 @@ static void swiotlb_bounce(struct device *dev, phys_add= r_t tlb_addr, size_t size phys_addr_t orig_addr =3D mem->slots[index].orig_addr; size_t alloc_size =3D mem->slots[index].alloc_size; unsigned long pfn =3D PFN_DOWN(orig_addr); - unsigned char *vaddr =3D phys_to_virt(tlb_addr); + unsigned char *vaddr =3D mem->vaddr + tlb_addr - mem->start; unsigned int tlb_offset; =20 if (orig_addr =3D=3D INVALID_PHYS_ADDR) --=20 2.25.1 From nobody Sun May 5 00:53:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1630085490; cv=none; d=zohomail.com; s=zohoarc; b=JgjEYFQapnlOxLO3Ec84pF3QbODEmiXfA0KZnY77kKcfpc7mypbXO5EL1M9uewqQb0xVW2C+XWdJinXdd+DL2I44fgT2XLcI+IjmJrj5jWTI1wug/uFSAW+eQkykSZRgrae61lSDL4huBL4QxcdUyVrQzNNvjgmG8eyBXYqW6zg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630085490; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=f/xiBxoq9p910rq4iCCIXN3piXXicO8x7yFYnpejeEk=; b=W+S7DIqQtovOKj8P5C6xFPtzR0JAnUX2lfafdWWHht18hrYf7K1uMTGEDFqsU/eI8WG0oGZo8dhl/VUFn7r2atbzpJDOOq27rVcU4UpvG4xioprNm+EERjF8OH2GUblijHX4PyZhId1eosOoQowBnklUbnfitqEf8OcLifvghpE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1630085490921134.13396608766095; Fri, 27 Aug 2021 10:31:30 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.173852.317213 (Exim 4.92) (envelope-from ) id 1mJfh9-0000Pk-V7; Fri, 27 Aug 2021 17:31:11 +0000 Received: by outflank-mailman (output) from mailman id 173852.317213; Fri, 27 Aug 2021 17:31:11 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfh9-0000Pd-S0; Fri, 27 Aug 2021 17:31:11 +0000 Received: by outflank-mailman (input) for mailman id 173852; Fri, 27 Aug 2021 17:31:10 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfYX-00016Q-OM for xen-devel@lists.xenproject.org; Fri, 27 Aug 2021 17:22:17 +0000 Received: from mail-pf1-x42f.google.com (unknown [2607:f8b0:4864:20::42f]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id c2bb90f7-b2bf-47ce-82a5-3477241e9920; Fri, 27 Aug 2021 17:21:50 +0000 (UTC) Received: by mail-pf1-x42f.google.com with SMTP id y11so6214929pfl.13 for ; Fri, 27 Aug 2021 10:21:50 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:36:ef50:8fcd:44d1:eb17]) by smtp.gmail.com with ESMTPSA id f5sm7155015pjo.23.2021.08.27.10.21.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Aug 2021 10:21:48 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: c2bb90f7-b2bf-47ce-82a5-3477241e9920 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=f/xiBxoq9p910rq4iCCIXN3piXXicO8x7yFYnpejeEk=; b=Ju+1YsPkg+UbMh1or5AIAI/ZvTETioYtgUuZZBnkEMEAY4wG/GpaOnM5lRpCuVEexe tumb++OLULd903N2hp7Qy2sLv493o0sqM1ciJZGGmOTkFHwpc0J/Yi0A4EFKybLEXp6A jHk4b+zO0G8lnOcidl94Qugbz4vzXgvdD20Gf0+Za3mE4U4VRUylL95yNp387frjeho3 Tydc+sAk2HjHyRM2/aBSNofuAZ7hYsTRhM8t4bBXBsNP8Ghfc2Eobpq+STxZiiZ5Bjmz KaaohXKSjVg3XUlnBIKCvvdh/pNN14L9P6XuRbik+Tc9GG6lDQBqK96lrzNtAflEBlew xsmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=f/xiBxoq9p910rq4iCCIXN3piXXicO8x7yFYnpejeEk=; b=PEU8loJZo9gmj2l0C3demIuTaPatfCreiYoyyk/Rpn5SN/9bgeVD1OA4gbK649C8VH bjgkVm7ybM8t684zPecAEo0T6AHTygaGTpK3o2FhCQQBQXlNEtf0JxPWMvso7OIhGh5j 1h4V/dvrcRP+YUq5XolOds6AZwqUpSQXDCzkWaWpP3Rmhmx/L227BwiEj0gZkQ41snPn NEJpYprJV5lAWZgZJGMCS4xw0NEv+LpVlZ4KyoMRWBDBrqn96wZOiusMSb8NZtu7ZdEA Zp1uSmYWOm56Z7Q53QZonhPOMUFU66WcEwZkuKZ1l7zO4fqbHwFtTMoiOhZDqXXS6ccH iNHw== X-Gm-Message-State: AOAM532H178asoJW+Mf5ICYGJg/+70Q0lrdlZjCBeorMo94pEwlYr9xQ +6Z/wvetQ/5KwSmyCVjtwig= X-Google-Smtp-Source: ABdhPJzpavlzdHZS1F6vdwwwOqwaFBPaG/YJGq0r0zJwRIjYidAu7mB3ClbmW59sMbVlOn28t15kbQ== X-Received: by 2002:a63:4f51:: with SMTP id p17mr8806904pgl.29.1630084909372; Fri, 27 Aug 2021 10:21:49 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, catalin.marinas@arm.com, will@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, gregkh@linuxfoundation.org, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, brijesh.singh@amd.com, thomas.lendacky@amd.com, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, hannes@cmpxchg.org, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, linux-arm-kernel@lists.infradead.org, xen-devel@lists.xenproject.org, rientjes@google.com, ardb@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V4 11/13] hyperv/IOMMU: Enable swiotlb bounce buffer for Isolation VM Date: Fri, 27 Aug 2021 13:21:09 -0400 Message-Id: <20210827172114.414281-12-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210827172114.414281-1-ltykernel@gmail.com> References: <20210827172114.414281-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1630085492486100001 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan hyperv Isolation VM requires bounce buffer support to copy data from/to encrypted memory and so enable swiotlb force mode to use swiotlb bounce buffer for DMA transaction. In Isolation VM with AMD SEV, the bounce buffer needs to be accessed via extra address space which is above shared_gpa_boundary (E.G 39 bit address line) reported by Hyper-V CPUID ISOLATION_CONFIG. The access physical address will be original physical address + shared_gpa_boundary. The shared_gpa_boundary in the AMD SEV SNP spec is called virtual top of memory(vTOM). Memory addresses below vTOM are automatically treated as private while memory above vTOM is treated as shared. Swiotlb bounce buffer code calls dma_map_decrypted() to mark bounce buffer visible to host and map it in extra address space. Populate dma memory decrypted ops with hv map/unmap function. Hyper-V initalizes swiotlb bounce buffer and default swiotlb needs to be disabled. pci_swiotlb_detect_override() and pci_swiotlb_detect_4gb() enable the default one. To override the setting, hyperv_swiotlb_detect() needs to run before these detect functions which depends on the pci_xen_swiotlb_ init(). Make pci_xen_swiotlb_init() depends on the hyperv_swiotlb _detect() to keep the order. The map function vmap_pfn() can't work in the early place hyperv_iommu_swiotlb_init() and so initialize swiotlb bounce buffer in the hyperv_iommu_swiotlb_later_init(). Signed-off-by: Tianyu Lan --- Change since v3: * Get hyperv bounce bufffer size via default swiotlb bounce buffer size function and keep default size as same as the one in the AMD SEV VM. --- arch/x86/hyperv/ivm.c | 28 +++++++++++++++ arch/x86/include/asm/mshyperv.h | 2 ++ arch/x86/mm/mem_encrypt.c | 3 +- arch/x86/xen/pci-swiotlb-xen.c | 3 +- drivers/hv/vmbus_drv.c | 3 ++ drivers/iommu/hyperv-iommu.c | 61 +++++++++++++++++++++++++++++++++ include/linux/hyperv.h | 1 + 7 files changed, 99 insertions(+), 2 deletions(-) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index e761c67e2218..84563b3c9f3a 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -294,3 +294,31 @@ int hv_set_mem_host_visibility(unsigned long addr, int= numpages, bool visible) =20 return __hv_set_mem_host_visibility((void *)addr, numpages, visibility); } + +/* + * hv_map_memory - map memory to extra space in the AMD SEV-SNP Isolation = VM. + */ +void *hv_map_memory(void *addr, unsigned long size) +{ + unsigned long *pfns =3D kcalloc(size / HV_HYP_PAGE_SIZE, + sizeof(unsigned long), GFP_KERNEL); + void *vaddr; + int i; + + if (!pfns) + return NULL; + + for (i =3D 0; i < size / PAGE_SIZE; i++) + pfns[i] =3D virt_to_hvpfn(addr + i * PAGE_SIZE) + + (ms_hyperv.shared_gpa_boundary >> PAGE_SHIFT); + + vaddr =3D vmap_pfn(pfns, size / PAGE_SIZE, PAGE_KERNEL_IO); + kfree(pfns); + + return vaddr; +} + +void hv_unmap_memory(void *addr) +{ + vunmap(addr); +} diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyper= v.h index b77f4caee3ee..627fcf8d443c 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -252,6 +252,8 @@ int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_= interrupt_entry *entry); int hv_mark_gpa_visibility(u16 count, const u64 pfn[], enum hv_mem_host_visibility visibility); int hv_set_mem_host_visibility(unsigned long addr, int numpages, bool visi= ble); +void *hv_map_memory(void *addr, unsigned long size); +void hv_unmap_memory(void *addr); void hv_sint_wrmsrl_ghcb(u64 msr, u64 value); void hv_sint_rdmsrl_ghcb(u64 msr, u64 *value); void hv_signal_eom_ghcb(void); diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index ff08dc463634..e2db0b8ed938 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -30,6 +30,7 @@ #include #include #include +#include =20 #include "mm_internal.h" =20 @@ -202,7 +203,7 @@ void __init sev_setup_arch(void) phys_addr_t total_mem =3D memblock_phys_mem_size(); unsigned long size; =20 - if (!sev_active()) + if (!sev_active() && !hv_is_isolation_supported()) return; =20 /* diff --git a/arch/x86/xen/pci-swiotlb-xen.c b/arch/x86/xen/pci-swiotlb-xen.c index 54f9aa7e8457..43bd031aa332 100644 --- a/arch/x86/xen/pci-swiotlb-xen.c +++ b/arch/x86/xen/pci-swiotlb-xen.c @@ -4,6 +4,7 @@ =20 #include #include +#include #include =20 #include @@ -91,6 +92,6 @@ int pci_xen_swiotlb_init_late(void) EXPORT_SYMBOL_GPL(pci_xen_swiotlb_init_late); =20 IOMMU_INIT_FINISH(pci_xen_swiotlb_detect, - NULL, + hyperv_swiotlb_detect, pci_xen_swiotlb_init, NULL); diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c index 57bbbaa4e8f7..f068e22a5636 100644 --- a/drivers/hv/vmbus_drv.c +++ b/drivers/hv/vmbus_drv.c @@ -23,6 +23,7 @@ #include #include =20 +#include #include #include #include @@ -2081,6 +2082,7 @@ struct hv_device *vmbus_device_create(const guid_t *t= ype, return child_device_obj; } =20 +static u64 vmbus_dma_mask =3D DMA_BIT_MASK(64); /* * vmbus_device_register - Register the child device */ @@ -2121,6 +2123,7 @@ int vmbus_device_register(struct hv_device *child_dev= ice_obj) } hv_debug_add_dev_dir(child_device_obj); =20 + child_device_obj->device.dma_mask =3D &vmbus_dma_mask; return 0; =20 err_kset_unregister: diff --git a/drivers/iommu/hyperv-iommu.c b/drivers/iommu/hyperv-iommu.c index e285a220c913..899563551574 100644 --- a/drivers/iommu/hyperv-iommu.c +++ b/drivers/iommu/hyperv-iommu.c @@ -13,14 +13,22 @@ #include #include #include +#include +#include =20 #include #include #include #include +#include +#include #include #include #include +#include +#include +#include +#include =20 #include "irq_remapping.h" =20 @@ -36,6 +44,9 @@ static cpumask_t ioapic_max_cpumask =3D { CPU_BITS_NONE }; static struct irq_domain *ioapic_ir_domain; =20 +static unsigned long hyperv_io_tlb_size; +static void *hyperv_io_tlb_start; + static int hyperv_ir_set_affinity(struct irq_data *data, const struct cpumask *mask, bool force) { @@ -337,4 +348,54 @@ static const struct irq_domain_ops hyperv_root_ir_doma= in_ops =3D { .free =3D hyperv_root_irq_remapping_free, }; =20 +void __init hyperv_iommu_swiotlb_init(void) +{ + /* + * Allocate Hyper-V swiotlb bounce buffer at early place + * to reserve large contiguous memory. + */ + hyperv_io_tlb_size =3D swiotlb_size_or_default(); + hyperv_io_tlb_start =3D memblock_alloc( + hyperv_io_tlb_size, HV_HYP_PAGE_SIZE); + + if (!hyperv_io_tlb_start) { + pr_warn("Fail to allocate Hyper-V swiotlb buffer.\n"); + return; + } +} + +int __init hyperv_swiotlb_detect(void) +{ + if (hypervisor_is_type(X86_HYPER_MS_HYPERV) + && hv_is_isolation_supported()) { + /* + * Enable swiotlb force mode in Isolation VM to + * use swiotlb bounce buffer for dma transaction. + */ + swiotlb_force =3D SWIOTLB_FORCE; + + dma_memory_generic_decrypted_ops.map =3D hv_map_memory; + dma_memory_generic_decrypted_ops.unmap =3D hv_unmap_memory; + return 1; + } + + return 0; +} + +void __init hyperv_iommu_swiotlb_later_init(void) +{ + /* + * Swiotlb bounce buffer needs to be mapped in extra address + * space. Map function doesn't work in the early place and so + * call swiotlb_late_init_with_tbl() here. + */ + if (swiotlb_late_init_with_tbl(hyperv_io_tlb_start, + hyperv_io_tlb_size >> IO_TLB_SHIFT)) + panic("Fail to initialize hyperv swiotlb.\n"); +} + +IOMMU_INIT_FINISH(hyperv_swiotlb_detect, + NULL, hyperv_iommu_swiotlb_init, + hyperv_iommu_swiotlb_later_init); + #endif diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h index 757e09606fd3..724a735d722a 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -1739,6 +1739,7 @@ int hyperv_write_cfg_blk(struct pci_dev *dev, void *b= uf, unsigned int len, int hyperv_reg_block_invalidate(struct pci_dev *dev, void *context, void (*block_invalidate)(void *context, u64 block_mask)); +int __init hyperv_swiotlb_detect(void); =20 struct hyperv_pci_block_ops { int (*read_block)(struct pci_dev *dev, void *buf, unsigned int buf_len, --=20 2.25.1 From nobody Sun May 5 00:53:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1630085521; cv=none; d=zohomail.com; s=zohoarc; b=BJIxGceLww0eBvkQSs45EbDmQ+6EPx3gbEf+1x7UQ/6pwLYSD6Gi4dG7z9ffulhHgNiClFnGNueO++SIAj455hnjMfHhbhoFNDaCjYUg22tjC5NfqUxVuxaSwTbgvsQz64fTvIQx/tYWRZwyRmpI4th8T7rxOw/xAYMWu1/Rzko= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630085521; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=eP71HsUL5jIprgafHAa9nr+PcbJTMafwTb0JYXooIkc=; b=F7ENdjEmyW2sk2meshOlWtFl4XpDJA0xQE5HUxgDUvvCuZ4ibjGcUXO8arArtxJjE0UWzKLA6gnDFauYwD7wDk6TR6XlIS6zjDJTt7H3Uf3+//3emDl6OCwfc7tKObH/wXTEIdso/4Si5XdOj4Tb+25yFmTeyZ7X4WwqAdBtKVY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1630085521408516.8425260689863; Fri, 27 Aug 2021 10:32:01 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.173865.317236 (Exim 4.92) (envelope-from ) id 1mJfhi-0001Ug-HO; Fri, 27 Aug 2021 17:31:46 +0000 Received: by outflank-mailman (output) from mailman id 173865.317236; Fri, 27 Aug 2021 17:31:46 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfhi-0001UZ-Ch; Fri, 27 Aug 2021 17:31:46 +0000 Received: by outflank-mailman (input) for mailman id 173865; Fri, 27 Aug 2021 17:31:44 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfYc-00016Q-OO for xen-devel@lists.xenproject.org; Fri, 27 Aug 2021 17:22:22 +0000 Received: from mail-pl1-x635.google.com (unknown [2607:f8b0:4864:20::635]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id b380d51e-bf0f-4c24-a453-d3f10746b212; Fri, 27 Aug 2021 17:21:53 +0000 (UTC) Received: by mail-pl1-x635.google.com with SMTP id c4so4340925plh.7 for ; Fri, 27 Aug 2021 10:21:53 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:36:ef50:8fcd:44d1:eb17]) by smtp.gmail.com with ESMTPSA id f5sm7155015pjo.23.2021.08.27.10.21.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Aug 2021 10:21:51 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: b380d51e-bf0f-4c24-a453-d3f10746b212 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=eP71HsUL5jIprgafHAa9nr+PcbJTMafwTb0JYXooIkc=; b=XjA9RK0diCV/NGST3xrY+yd75EDMjsEGQH/MoBKRQsWG98prw900XXUkrlbVQCqUsQ MHaMnEN2Pe1TBwV5HSpMusMKHgOPjJJDZhE8XYHKthbV12ljewMWRv80ALVBlRldp490 2sekztXz8D83jxYZ1FxlIPO0hq2wjpQo9hhi6k7rnZ+U4VdreZrd65DZqEmNtQ4iA69z OfLzvtnocSEsnMg1+wRdnNjowgFs3Qzam2F6tgirRv6C6/YUzrjupd4EDR0bN422QOG7 CMtR7z4OxOmMiP1InmgQnMFT4IgQbStpJ2yslzTuqG3+eouVXEjyk7XY5T/Ono+Ukm7S ckdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=eP71HsUL5jIprgafHAa9nr+PcbJTMafwTb0JYXooIkc=; b=NfVyLxLcpLHPPMhxS9smUCEbtGo/ptH00f2ohAGhv0vXW0qK+sW9h2HjayDrLc1cS4 fW0YUymE0h1CU6fz70z3iFnOYq4Gg84eVbPKhqp3bRgbA2P08UuCqCnLX4pjRcXfTqMo BPKlYYbIABMLtH6m2CRKAceLALeO5FKvVlI6zMniiFCW8EOjB1mQ8Jcve8MNqxNgtQj7 X05IZ5XaphS0BRM3YwGwRRW3eN6hecJZRU/W1BqNY45vDp9XjOBbrK2+2wdwpjKZ2KrW MjATfYnz/1HIUWrodyCw8GGhd35uQqO3cPUg8RaPbXQQuneAPhLiqWfqJodMq+ntpM/T ISoA== X-Gm-Message-State: AOAM530zsq+pwZIQ0uGASW9Y+Ps1lp+1go8X+9uIgVJiI+sVu29zUjpt 5gU5ICB/dOKWTDSM9R+uxEM= X-Google-Smtp-Source: ABdhPJzzwuvbNkjkSkGeJFwgRJTLC75ZjTV696jyMEv0G8ZUsAhjChdDm2//FEE8VuhUyD1GKzVAhg== X-Received: by 2002:a17:90a:718c:: with SMTP id i12mr3199697pjk.182.1630084912377; Fri, 27 Aug 2021 10:21:52 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, catalin.marinas@arm.com, will@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, gregkh@linuxfoundation.org, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, brijesh.singh@amd.com, thomas.lendacky@amd.com, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, hannes@cmpxchg.org, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, linux-arm-kernel@lists.infradead.org, xen-devel@lists.xenproject.org, rientjes@google.com, ardb@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V4 12/13] hv_netvsc: Add Isolation VM support for netvsc driver Date: Fri, 27 Aug 2021 13:21:10 -0400 Message-Id: <20210827172114.414281-13-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210827172114.414281-1-ltykernel@gmail.com> References: <20210827172114.414281-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1630085523737100001 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan In Isolation VM, all shared memory with host needs to mark visible to host via hvcall. vmbus_establish_gpadl() has already done it for netvsc rx/tx ring buffer. The page buffer used by vmbus_sendpacket_ pagebuffer() stills need to be handled. Use DMA API to map/umap these memory during sending/receiving packet and Hyper-V swiotlb bounce buffer dma adress will be returned. The swiotlb bounce buffer has been masked to be visible to host during boot up. Signed-off-by: Tianyu Lan --- Change since v3: * Add comment to explain why not to use dma_map_sg() * Fix some error handle. --- arch/x86/hyperv/ivm.c | 1 + drivers/net/hyperv/hyperv_net.h | 5 ++ drivers/net/hyperv/netvsc.c | 135 +++++++++++++++++++++++++++++- drivers/net/hyperv/rndis_filter.c | 2 + include/linux/hyperv.h | 5 ++ 5 files changed, 145 insertions(+), 3 deletions(-) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index 84563b3c9f3a..08d8e01de017 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -317,6 +317,7 @@ void *hv_map_memory(void *addr, unsigned long size) =20 return vaddr; } +EXPORT_SYMBOL_GPL(hv_map_memory); =20 void hv_unmap_memory(void *addr) { diff --git a/drivers/net/hyperv/hyperv_net.h b/drivers/net/hyperv/hyperv_ne= t.h index aa7c9962dbd8..862419912bfb 100644 --- a/drivers/net/hyperv/hyperv_net.h +++ b/drivers/net/hyperv/hyperv_net.h @@ -164,6 +164,7 @@ struct hv_netvsc_packet { u32 total_bytes; u32 send_buf_index; u32 total_data_buflen; + struct hv_dma_range *dma_range; }; =20 #define NETVSC_HASH_KEYLEN 40 @@ -1074,6 +1075,7 @@ struct netvsc_device { =20 /* Receive buffer allocated by us but manages by NetVSP */ void *recv_buf; + void *recv_original_buf; u32 recv_buf_size; /* allocated bytes */ u32 recv_buf_gpadl_handle; u32 recv_section_cnt; @@ -1082,6 +1084,7 @@ struct netvsc_device { =20 /* Send buffer allocated by us */ void *send_buf; + void *send_original_buf; u32 send_buf_size; u32 send_buf_gpadl_handle; u32 send_section_cnt; @@ -1731,4 +1734,6 @@ struct rndis_message { #define RETRY_US_HI 10000 #define RETRY_MAX 2000 /* >10 sec */ =20 +void netvsc_dma_unmap(struct hv_device *hv_dev, + struct hv_netvsc_packet *packet); #endif /* _HYPERV_NET_H */ diff --git a/drivers/net/hyperv/netvsc.c b/drivers/net/hyperv/netvsc.c index f19bffff6a63..edd336b08c2c 100644 --- a/drivers/net/hyperv/netvsc.c +++ b/drivers/net/hyperv/netvsc.c @@ -153,8 +153,21 @@ static void free_netvsc_device(struct rcu_head *head) int i; =20 kfree(nvdev->extension); - vfree(nvdev->recv_buf); - vfree(nvdev->send_buf); + + if (nvdev->recv_original_buf) { + vunmap(nvdev->recv_buf); + vfree(nvdev->recv_original_buf); + } else { + vfree(nvdev->recv_buf); + } + + if (nvdev->send_original_buf) { + vunmap(nvdev->send_buf); + vfree(nvdev->send_original_buf); + } else { + vfree(nvdev->send_buf); + } + kfree(nvdev->send_section_map); =20 for (i =3D 0; i < VRSS_CHANNEL_MAX; i++) { @@ -347,6 +360,7 @@ static int netvsc_init_buf(struct hv_device *device, unsigned int buf_size; size_t map_words; int i, ret =3D 0; + void *vaddr; =20 /* Get receive buffer area. */ buf_size =3D device_info->recv_sections * device_info->recv_section_size; @@ -382,6 +396,17 @@ static int netvsc_init_buf(struct hv_device *device, goto cleanup; } =20 + if (hv_isolation_type_snp()) { + vaddr =3D hv_map_memory(net_device->recv_buf, buf_size); + if (!vaddr) { + ret =3D -ENOMEM; + goto cleanup; + } + + net_device->recv_original_buf =3D net_device->recv_buf; + net_device->recv_buf =3D vaddr; + } + /* Notify the NetVsp of the gpadl handle */ init_packet =3D &net_device->channel_init_pkt; memset(init_packet, 0, sizeof(struct nvsp_message)); @@ -485,6 +510,17 @@ static int netvsc_init_buf(struct hv_device *device, goto cleanup; } =20 + if (hv_isolation_type_snp()) { + vaddr =3D hv_map_memory(net_device->send_buf, buf_size); + if (!vaddr) { + ret =3D -ENOMEM; + goto cleanup; + } + + net_device->send_original_buf =3D net_device->send_buf; + net_device->send_buf =3D vaddr; + } + /* Notify the NetVsp of the gpadl handle */ init_packet =3D &net_device->channel_init_pkt; memset(init_packet, 0, sizeof(struct nvsp_message)); @@ -775,7 +811,7 @@ static void netvsc_send_tx_complete(struct net_device *= ndev, =20 /* Notify the layer above us */ if (likely(skb)) { - const struct hv_netvsc_packet *packet + struct hv_netvsc_packet *packet =3D (struct hv_netvsc_packet *)skb->cb; u32 send_index =3D packet->send_buf_index; struct netvsc_stats *tx_stats; @@ -791,6 +827,7 @@ static void netvsc_send_tx_complete(struct net_device *= ndev, tx_stats->bytes +=3D packet->total_bytes; u64_stats_update_end(&tx_stats->syncp); =20 + netvsc_dma_unmap(ndev_ctx->device_ctx, packet); napi_consume_skb(skb, budget); } =20 @@ -955,6 +992,87 @@ static void netvsc_copy_to_send_buf(struct netvsc_devi= ce *net_device, memset(dest, 0, padding); } =20 +void netvsc_dma_unmap(struct hv_device *hv_dev, + struct hv_netvsc_packet *packet) +{ + u32 page_count =3D packet->cp_partial ? + packet->page_buf_cnt - packet->rmsg_pgcnt : + packet->page_buf_cnt; + int i; + + if (!hv_is_isolation_supported()) + return; + + if (!packet->dma_range) + return; + + for (i =3D 0; i < page_count; i++) + dma_unmap_single(&hv_dev->device, packet->dma_range[i].dma, + packet->dma_range[i].mapping_size, + DMA_TO_DEVICE); + + kfree(packet->dma_range); +} + +/* netvsc_dma_map - Map swiotlb bounce buffer with data page of + * packet sent by vmbus_sendpacket_pagebuffer() in the Isolation + * VM. + * + * In isolation VM, netvsc send buffer has been marked visible to + * host and so the data copied to send buffer doesn't need to use + * bounce buffer. The data pages handled by vmbus_sendpacket_pagebuffer() + * may not be copied to send buffer and so these pages need to be + * mapped with swiotlb bounce buffer. netvsc_dma_map() is to do + * that. The pfns in the struct hv_page_buffer need to be converted + * to bounce buffer's pfn. The loop here is necessary becuase the + * entries in the page buffer array are not necessarily full + * pages of data. Each entry in the array has a separate offset and + * len that may be non-zero, even for entries in the middle of the + * array. And the entries are not physically contiguous. So each + * entry must be individually mapped rather than as a contiguous unit. + * So not use dma_map_sg() here. + */ +int netvsc_dma_map(struct hv_device *hv_dev, + struct hv_netvsc_packet *packet, + struct hv_page_buffer *pb) +{ + u32 page_count =3D packet->cp_partial ? + packet->page_buf_cnt - packet->rmsg_pgcnt : + packet->page_buf_cnt; + dma_addr_t dma; + int i; + + if (!hv_is_isolation_supported()) + return 0; + + packet->dma_range =3D kcalloc(page_count, + sizeof(*packet->dma_range), + GFP_KERNEL); + if (!packet->dma_range) + return -ENOMEM; + + for (i =3D 0; i < page_count; i++) { + char *src =3D phys_to_virt((pb[i].pfn << HV_HYP_PAGE_SHIFT) + + pb[i].offset); + u32 len =3D pb[i].len; + + dma =3D dma_map_single(&hv_dev->device, src, len, + DMA_TO_DEVICE); + if (dma_mapping_error(&hv_dev->device, dma)) { + kfree(packet->dma_range); + return -ENOMEM; + } + + packet->dma_range[i].dma =3D dma; + packet->dma_range[i].mapping_size =3D len; + pb[i].pfn =3D dma >> HV_HYP_PAGE_SHIFT; + pb[i].offset =3D offset_in_hvpage(dma); + pb[i].len =3D len; + } + + return 0; +} + static inline int netvsc_send_pkt( struct hv_device *device, struct hv_netvsc_packet *packet, @@ -995,14 +1113,24 @@ static inline int netvsc_send_pkt( =20 trace_nvsp_send_pkt(ndev, out_channel, rpkt); =20 + packet->dma_range =3D NULL; if (packet->page_buf_cnt) { if (packet->cp_partial) pb +=3D packet->rmsg_pgcnt; =20 + ret =3D netvsc_dma_map(ndev_ctx->device_ctx, packet, pb); + if (ret) { + ret =3D -EAGAIN; + goto exit;=20 + } + ret =3D vmbus_sendpacket_pagebuffer(out_channel, pb, packet->page_buf_cnt, &nvmsg, sizeof(nvmsg), req_id); + + if (ret) + netvsc_dma_unmap(ndev_ctx->device_ctx, packet); } else { ret =3D vmbus_sendpacket(out_channel, &nvmsg, sizeof(nvmsg), @@ -1010,6 +1138,7 @@ static inline int netvsc_send_pkt( VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); } =20 +exit: if (ret =3D=3D 0) { atomic_inc_return(&nvchan->queue_sends); =20 diff --git a/drivers/net/hyperv/rndis_filter.c b/drivers/net/hyperv/rndis_f= ilter.c index f6c9c2a670f9..448fcc325ed7 100644 --- a/drivers/net/hyperv/rndis_filter.c +++ b/drivers/net/hyperv/rndis_filter.c @@ -361,6 +361,8 @@ static void rndis_filter_receive_response(struct net_de= vice *ndev, } } =20 + netvsc_dma_unmap(((struct net_device_context *) + netdev_priv(ndev))->device_ctx, &request->pkt); complete(&request->wait_event); } else { netdev_err(ndev, diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h index 724a735d722a..139a43ad65a1 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -1596,6 +1596,11 @@ struct hyperv_service_callback { void (*callback)(void *context); }; =20 +struct hv_dma_range { + dma_addr_t dma; + u32 mapping_size; +}; + #define MAX_SRV_VER 0x7ffffff extern bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp, u8 *buf= , u32 buflen, const int *fw_version, int fw_vercnt, --=20 2.25.1 From nobody Sun May 5 00:53:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1630085568; cv=none; d=zohomail.com; s=zohoarc; b=D4XySremGWOkY5XmJukGTdO0YBbILLJlMk1iuH8uG9FrNImkZvvvZsmDNB2oNsv8YbSfrpbR5y/d5NNPjW2ttZpwTbBBpLxrG3DrLzhTGneT0DlSeGQTA3M2i4y6adRLgR00dGEqmCcI8+kdrgmjxcin0tgkYGlumVU39kLHJVk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630085568; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=gLQw+gOkOgBpF527SY7hnRVsopMwR8pUZ+6p/+sCMZE=; b=DWDbhjq6CvIsyZ5J2TN/7RB6Ve772c+FGby/UohJhzTNGO+PetThoN4uSsBi5HV/t1sq1jaqRLVSrfgJGkMoE8Dd0QTifNE4eqe6oJPRKQ976EUlfFIcwDfqj1sjv8oRx24mxMoA0VrNHN4L9zy/k+vBBwdtBGYLeuhV+QVU+WU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1630085568737461.579053078211; Fri, 27 Aug 2021 10:32:48 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.173880.317247 (Exim 4.92) (envelope-from ) id 1mJfiV-0002V7-0D; Fri, 27 Aug 2021 17:32:35 +0000 Received: by outflank-mailman (output) from mailman id 173880.317247; Fri, 27 Aug 2021 17:32:34 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfiU-0002Uy-Sl; Fri, 27 Aug 2021 17:32:34 +0000 Received: by outflank-mailman (input) for mailman id 173880; Fri, 27 Aug 2021 17:32:33 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mJfYh-00016Q-Od for xen-devel@lists.xenproject.org; Fri, 27 Aug 2021 17:22:27 +0000 Received: from mail-pg1-x534.google.com (unknown [2607:f8b0:4864:20::534]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id ef1f9e60-6774-4b61-b8e6-c390795a22b4; Fri, 27 Aug 2021 17:21:56 +0000 (UTC) Received: by mail-pg1-x534.google.com with SMTP id w8so6496962pgf.5 for ; Fri, 27 Aug 2021 10:21:56 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:36:ef50:8fcd:44d1:eb17]) by smtp.gmail.com with ESMTPSA id f5sm7155015pjo.23.2021.08.27.10.21.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Aug 2021 10:21:54 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: ef1f9e60-6774-4b61-b8e6-c390795a22b4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=gLQw+gOkOgBpF527SY7hnRVsopMwR8pUZ+6p/+sCMZE=; b=jqKW1cLVDQjLgxObfit7zrVrS1J/x3pPdbxN1asI4+M8zeuts6wiVmaF5gUPo9V2FS JXeBFTb/jNQsN8TVkdzk5dR/ZI/RL9bT7w6c7bid9edxLa0DUGokGuxk8lS91UGkaZj4 eqhtySo2OVg6q8KSYAGPGN96kFNqzBoVPg3X/PK5X2nNp/aialOuUPCIlZMUG0e+PnrF loz3O3FeTYg3uZwrb5yAi3OMvnSUXqfLmi7a7GhGDDQYfkSFFvgBDn2e4Y2I1ay7oW87 728MiFOG393bdP8GnvBwKHUYm2+UuifhwCPpfKTJJH5Hk/ZtBiCsSRaDaNOT026pRuBy Imzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gLQw+gOkOgBpF527SY7hnRVsopMwR8pUZ+6p/+sCMZE=; b=WIDKt7jkHMY0xA4V2iKIgdlfKogMC5CwSIB/KGDz3bvdv8PNyKlh2BWST67TYRZ9dq l99iRPyviDxA47J8vKFCp2n6nrsKG8yN9XJ7S/MWtXe5L3ZBykxHnYE15PiPG2A5dkPo f6ugt2sqbHbjUTcR83OKU46eCZ+SElDQqiZ/QZxriZXcEbCbXdera5idnAq1FuYrKeTb S0zZa2KtWlBddk46mRqoj0ecXWFpbJohgXYz4uQF8prlvTzGBCN+Glya6iLha+Vm+plY 7GtfQ/qzKVxYtuiFrYIdMWm6qKaDZczyN8uJgxElY9pctuDUEoYc9GEv6lyuVLPokVYX PWgA== X-Gm-Message-State: AOAM5328USqyJzWg0hC+Cdd94rSJAST00TyHMsiARlK9KPzkRSuBI5KH jj/ACSr49hcPmSNf7TFvDJA= X-Google-Smtp-Source: ABdhPJwuiMDXHrHAjE5Ue90v/UaZtCN02djvrlnnwi5m/AZBQrrgZl+iVNvLmQEttw3bmm/01KlJZg== X-Received: by 2002:a05:6a00:a94:b029:384:1dc6:7012 with SMTP id b20-20020a056a000a94b02903841dc67012mr10074419pfl.53.1630084915337; Fri, 27 Aug 2021 10:21:55 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, catalin.marinas@arm.com, will@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, gregkh@linuxfoundation.org, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, brijesh.singh@amd.com, thomas.lendacky@amd.com, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, hannes@cmpxchg.org, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, linux-arm-kernel@lists.infradead.org, xen-devel@lists.xenproject.org, rientjes@google.com, ardb@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V4 13/13] hv_storvsc: Add Isolation VM support for storvsc driver Date: Fri, 27 Aug 2021 13:21:11 -0400 Message-Id: <20210827172114.414281-14-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210827172114.414281-1-ltykernel@gmail.com> References: <20210827172114.414281-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1630085569462100001 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan In Isolation VM, all shared memory with host needs to mark visible to host via hvcall. vmbus_establish_gpadl() has already done it for storvsc rx/tx ring buffer. The page buffer used by vmbus_sendpacket_ mpb_desc() still needs to be handled. Use DMA API(dma_map_sg) to map these memory during sending/receiving packet and return swiotlb bounce buffer dma address. In Isolation VM, swiotlb bounce buffer is marked to be visible to host and the swiotlb force mode is enabled. Set device's dma min align mask to HV_HYP_PAGE_SIZE - 1 in order to keep the original data offset in the bounce buffer. Signed-off-by: Tianyu Lan --- Change since v3: * Rplace dma_map_page with dma_map_sg() * Use for_each_sg() to populate payload->range.pfn_array. * Remove storvsc_dma_map macro --- drivers/hv/vmbus_drv.c | 1 + drivers/scsi/storvsc_drv.c | 41 +++++++++++++++----------------------- include/linux/hyperv.h | 1 + 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c index f068e22a5636..270d526fd9de 100644 --- a/drivers/hv/vmbus_drv.c +++ b/drivers/hv/vmbus_drv.c @@ -2124,6 +2124,7 @@ int vmbus_device_register(struct hv_device *child_dev= ice_obj) hv_debug_add_dev_dir(child_device_obj); =20 child_device_obj->device.dma_mask =3D &vmbus_dma_mask; + child_device_obj->device.dma_parms =3D &child_device_obj->dma_parms; return 0; =20 err_kset_unregister: diff --git a/drivers/scsi/storvsc_drv.c b/drivers/scsi/storvsc_drv.c index 328bb961c281..4f1793be1fdc 100644 --- a/drivers/scsi/storvsc_drv.c +++ b/drivers/scsi/storvsc_drv.c @@ -21,6 +21,8 @@ #include #include #include +#include + #include #include #include @@ -1312,6 +1314,9 @@ static void storvsc_on_channel_callback(void *context) continue; } request =3D (struct storvsc_cmd_request *)scsi_cmd_priv(scmnd); + if (scsi_sg_count(scmnd)) + dma_unmap_sg(&device->device, scsi_sglist(scmnd), + scsi_sg_count(scmnd), scmnd->sc_data_direction); } =20 storvsc_on_receive(stor_device, packet, request); @@ -1725,7 +1730,6 @@ static int storvsc_queuecommand(struct Scsi_Host *hos= t, struct scsi_cmnd *scmnd) struct hv_host_device *host_dev =3D shost_priv(host); struct hv_device *dev =3D host_dev->dev; struct storvsc_cmd_request *cmd_request =3D scsi_cmd_priv(scmnd); - int i; struct scatterlist *sgl; unsigned int sg_count; struct vmscsi_request *vm_srb; @@ -1807,10 +1811,11 @@ static int storvsc_queuecommand(struct Scsi_Host *h= ost, struct scsi_cmnd *scmnd) payload_sz =3D sizeof(cmd_request->mpb); =20 if (sg_count) { - unsigned int hvpgoff, hvpfns_to_add; unsigned long offset_in_hvpg =3D offset_in_hvpage(sgl->offset); unsigned int hvpg_count =3D HVPFN_UP(offset_in_hvpg + length); - u64 hvpfn; + struct scatterlist *sg; + unsigned long hvpfn, hvpfns_to_add; + int j, i =3D 0; =20 if (hvpg_count > MAX_PAGE_BUFFER_COUNT) { =20 @@ -1824,31 +1829,16 @@ static int storvsc_queuecommand(struct Scsi_Host *h= ost, struct scsi_cmnd *scmnd) payload->range.len =3D length; payload->range.offset =3D offset_in_hvpg; =20 + if (dma_map_sg(&dev->device, sgl, sg_count, + scmnd->sc_data_direction) =3D=3D 0) + return SCSI_MLQUEUE_DEVICE_BUSY; =20 - for (i =3D 0; sgl !=3D NULL; sgl =3D sg_next(sgl)) { - /* - * Init values for the current sgl entry. hvpgoff - * and hvpfns_to_add are in units of Hyper-V size - * pages. Handling the PAGE_SIZE !=3D HV_HYP_PAGE_SIZE - * case also handles values of sgl->offset that are - * larger than PAGE_SIZE. Such offsets are handled - * even on other than the first sgl entry, provided - * they are a multiple of PAGE_SIZE. - */ - hvpgoff =3D HVPFN_DOWN(sgl->offset); - hvpfn =3D page_to_hvpfn(sg_page(sgl)) + hvpgoff; - hvpfns_to_add =3D HVPFN_UP(sgl->offset + sgl->length) - - hvpgoff; + for_each_sg(sgl, sg, sg_count, j) { + hvpfns_to_add =3D HVPFN_UP(sg_dma_len(sg)); + hvpfn =3D HVPFN_DOWN(sg_dma_address(sg)); =20 - /* - * Fill the next portion of the PFN array with - * sequential Hyper-V PFNs for the continguous physical - * memory described by the sgl entry. The end of the - * last sgl should be reached at the same time that - * the PFN array is filled. - */ while (hvpfns_to_add--) - payload->range.pfn_array[i++] =3D hvpfn++; + payload->range.pfn_array[i++] =3D hvpfn++; } } =20 @@ -1992,6 +1982,7 @@ static int storvsc_probe(struct hv_device *device, stor_device->vmscsi_size_delta =3D sizeof(struct vmscsi_win8_extension); spin_lock_init(&stor_device->lock); hv_set_drvdata(device, stor_device); + dma_set_min_align_mask(&device->device, HV_HYP_PAGE_SIZE - 1); =20 stor_device->port_number =3D host->host_no; ret =3D storvsc_connect_to_vsp(device, storvsc_ringbuffer_size, is_fc); diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h index 139a43ad65a1..8f39893f8ccf 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -1274,6 +1274,7 @@ struct hv_device { =20 struct vmbus_channel *channel; struct kset *channels_kset; + struct device_dma_parameters dma_parms; =20 /* place holder to keep track of the dir for hv device in debugfs */ struct dentry *debug_dir; --=20 2.25.1