From nobody Fri Apr 19 14:35:49 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=1631626786; cv=none; d=zohomail.com; s=zohoarc; b=OJ7lGCrhdN/Pl01Rk4p22dV15gNKp3YYZKDz1y/vtK2KamjM2YvziD2skvxrbj2xI9uqhb6w4kdckZeqdSl4wdmwoSS03H72rtzj2hD8Cd5Vl9qDU/g9hEN462bP4s847hn5GozzjjRVP1WXJVcv11FVc0hQyO+p3fxetPLz/a0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1631626786; 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=QrhS3LTuU3+XNI0p3h2fXGmudzlD8JRBXhiK5DozhlE=; b=RP0ODZWmZzOzO6NOh8TSAfH/JDZ6ewtS7tgXVEQSgVk0TdfhiI7wKbCWR6i9D/tARySrFFRXy9zJmeyJtzGraEC5Rg78YS+uWNRbU7NX+rVN7sGfXff9HqVNBadb8DLhIWACrf2V+otNJvrnopNhTLOLQTnpd7A5HMlA/EnFqBA= 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 163162678638598.33757226703449; Tue, 14 Sep 2021 06:39:46 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.186648.335386 (Exim 4.92) (envelope-from ) id 1mQ8eq-0002Fn-1F; Tue, 14 Sep 2021 13:39:32 +0000 Received: by outflank-mailman (output) from mailman id 186648.335386; Tue, 14 Sep 2021 13:39:32 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8ep-0002Fe-UM; Tue, 14 Sep 2021 13:39:31 +0000 Received: by outflank-mailman (input) for mailman id 186648; Tue, 14 Sep 2021 13:39:29 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8en-0001wz-Qo for xen-devel@lists.xenproject.org; Tue, 14 Sep 2021 13:39:29 +0000 Received: from mail-pj1-x1032.google.com (unknown [2607:f8b0:4864:20::1032]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 7a7994b4-e450-4c6e-96eb-880b16d83ea2; Tue, 14 Sep 2021 13:39:26 +0000 (UTC) Received: by mail-pj1-x1032.google.com with SMTP id lb1-20020a17090b4a4100b001993f863df2so2799411pjb.5 for ; Tue, 14 Sep 2021 06:39:26 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:7:6ea2:a529:4af3:5057]) by smtp.gmail.com with ESMTPSA id v13sm10461234pfm.16.2021.09.14.06.39.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:39:25 -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: 7a7994b4-e450-4c6e-96eb-880b16d83ea2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QrhS3LTuU3+XNI0p3h2fXGmudzlD8JRBXhiK5DozhlE=; b=o/165tggS7dVhrFX6GAOmcFAAgPlx1idPlHms0MswSiWTbPGiIx2vuWrvhXVMR6XkU WnL7lHtu2y5YInUkOyc6YYbJ+YyMZOyaX6VetzB8rAhjcwH7L1jcLGhpqoJPd3iTqUQ1 dNQjwCQDWy/PlCBNgPOOFGfLa2wHkiAyAEiHBsGTMmFG840H1ZDmeTA745j4Xd7kZSJv rxrqlUztv9pv74ZIlwPiVGtpvnJTTCB3hGp2H3qKrMxHv8B5zJ4HRQjUdyYF67qJUBZD vqO4oTXLK/H1sWmy174msIvzDi3RamnaVJYMCU5gkVx926Wxg7ezDeybpNyhXJEPyAHm AB6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QrhS3LTuU3+XNI0p3h2fXGmudzlD8JRBXhiK5DozhlE=; b=rgdP94Gcra59jrXn80Q7Vw2MgH7j8Qf1zY7NUt6fbAiWyUC6B+g7h00HWZgkA1u/MI baoFvh3gHz97Jke/JtMenB0Ekh0uC1rej9SSlbJSgyKkC2yR/CrC58FvFPDqsfxUYMn5 HTjrnCyIXhhaojZ7ZF5QqM4+lj+owylTebxv7GdjWZmSsenjXWE+9siGnWb3aD+Zp+zM Mh3H81sEG9jPKUWlA8TvEFxzjEZedWieh3cCRfwN7h07yC01tn4kT50V3IBEQULI93Tv 0UfcX7/AJnm7HBc7Qd4rk3a9QAlOQdEf2V8OJPCbfWl60rLCw+OTFS46RC/0YtIgS5WE viyA== X-Gm-Message-State: AOAM530kybuoWCUC/KDhD/t6E+RhN8kod7dpzIeLUuCci2RSNG1EsVOH HebbA5Lg/DXR5RVjgT//McU= X-Google-Smtp-Source: ABdhPJxcVcMgZPPk9T3dmVUHKa5l0JwMCgOBrxv8XyGGHB4KduVicPNXQOZXfeM3vdDo5Yrr754YSw== X-Received: by 2002:a17:90b:1bd1:: with SMTP id oa17mr2215152pjb.82.1631626766123; Tue, 14 Sep 2021 06:39:26 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, 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, will@kernel.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, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, pgonda@google.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, aneesh.kumar@linux.ibm.com, saravanand@fb.com, krish.sadhukhan@oracle.com, xen-devel@lists.xenproject.org, tj@kernel.org, rientjes@google.com, 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 V5 01/12] x86/hyperv: Initialize GHCB page in Isolation VM Date: Tue, 14 Sep 2021 09:39:02 -0400 Message-Id: <20210914133916.1440931-2-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210914133916.1440931-1-ltykernel@gmail.com> References: <20210914133916.1440931-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1631626787347100001 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 --- Change since v4: * Fix typo comment 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..a7e922755ad1 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 + * memory 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 e095c28d27ae..b09ade389040 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 Fri Apr 19 14:35:49 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=1631626788; cv=none; d=zohomail.com; s=zohoarc; b=hg4BlhfIqRB4VY1p/o76jMxvdZo+iUr/vT8JVYSGf3496CgTYJbTAWGQOu6HCJ860rUO+fnqSEaFPpTFGnGx72SizzmS8vRh9ox2/L6nhGpo2eDOxDGfN3MK8sy1HtJEpUwRCaufmCxVIpfgtUhvovPEdqS0lmTyNcQpmTk6V8w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1631626788; 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=YUwTqwYZOOkMzYqyDRP5u7DXlTVFM/o7xmj81RyBU+U=; b=c6OUI67AMK8OIJxVYJTuLoI9iFJOWeY5FxAIFuy7hiIvwh7IbHtWUbHNI1281/2J7sYtyPLc0XMfm6gyLsNrjbkMgnufrfTDWdu7RtU0f10JCwiUf+adWN1gLOVMcuVGnOidurHQ28hROiTlZ7gVbOEBs9P3xYvNIZ4U4Jgj6JY= 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 1631626788064220.44713029480863; Tue, 14 Sep 2021 06:39:48 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.186649.335397 (Exim 4.92) (envelope-from ) id 1mQ8et-0002Yv-9S; Tue, 14 Sep 2021 13:39:35 +0000 Received: by outflank-mailman (output) from mailman id 186649.335397; Tue, 14 Sep 2021 13:39:35 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8et-0002Yo-5x; Tue, 14 Sep 2021 13:39:35 +0000 Received: by outflank-mailman (input) for mailman id 186649; Tue, 14 Sep 2021 13:39:34 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8es-0001wz-Qw for xen-devel@lists.xenproject.org; Tue, 14 Sep 2021 13:39:34 +0000 Received: from mail-pl1-x62c.google.com (unknown [2607:f8b0:4864:20::62c]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 3789e736-6a54-4fee-bbb4-fa76cd6f82cf; Tue, 14 Sep 2021 13:39:29 +0000 (UTC) Received: by mail-pl1-x62c.google.com with SMTP id w6so8208421pll.3 for ; Tue, 14 Sep 2021 06:39:29 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:7:6ea2:a529:4af3:5057]) by smtp.gmail.com with ESMTPSA id v13sm10461234pfm.16.2021.09.14.06.39.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:39: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: 3789e736-6a54-4fee-bbb4-fa76cd6f82cf DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YUwTqwYZOOkMzYqyDRP5u7DXlTVFM/o7xmj81RyBU+U=; b=Zy+W6lLKWmt7luEwpTRuKO/55L+1pJhM+U2Y08JCjUl/Sx3GrJY7QhjVoeYU8XS0sZ VYkNN1yYSgJpcM/oeMCO+0ImLroJ2E6tKqOy3G3BQc+A+XteLhWOhUNt04iKCz4oLukL ZXdeQ2jx1nvY0B/yoIhbppgpWtNXrdn1B8PukHkeaXer6DbeBQQ6LMeN6zIn0UbmFUqA XFkU1p/3fDU6Rv88cbsucwIK2FeuA5bSFEOuD0P01iDmKeSieEZM0MwidbNyW+kC5LvM reTUrry6bD4yBiSWg+2gSNkw7hmnvgvjy6KZtQZkdkIbi9CfdzA5mje9Mgsm1Myh/rpm yczA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YUwTqwYZOOkMzYqyDRP5u7DXlTVFM/o7xmj81RyBU+U=; b=fkTE9sCk7UZIaOE3Fkrq0YAxUSmzjn4SFyIT/099Rs+ezUmQIun/5Xi7NrwpWEMfJL ALOo/HBwPk6F6Znat5AJsHwKIqabMfs/CkqJlOgPVLUnb1Er18cKW8tGQAIoKkBX4CF4 Q1vaRIQG6wlF9u7w/rLCfisrGRvLboTcvCMrDHh1BvZVuzKMJwLCsYunrlukI23bgyWX R7ZTvZxNHDvrFpPJj5EnW2GSurbI2bf4usDqBqO3X286gbsBlVUw5wzF01oRV5zj++pZ PLfu2HSefoHUqeteO1IWGlecZZAdj23Z3smPTzlNxO5YM5zxNE2ThDivbEGFH4eH64Tb uWmw== X-Gm-Message-State: AOAM533nR4jru6DD1asRxM1+8dY2cYvaB9PPWfl1btuftbU3tlnubJJ8 wJ9CEmr7EVQz749XF2X7jJ0= X-Google-Smtp-Source: ABdhPJx44lS4Ai2yXXn8V591ghM4am9GxTdD+xMKN2ARZV8UrnEqqpomOlRQvCNWc6041rvSAnTYOg== X-Received: by 2002:a17:90a:9404:: with SMTP id r4mr2180743pjo.240.1631626769029; Tue, 14 Sep 2021 06:39:29 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, 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, will@kernel.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, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, pgonda@google.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, aneesh.kumar@linux.ibm.com, saravanand@fb.com, krish.sadhukhan@oracle.com, xen-devel@lists.xenproject.org, tj@kernel.org, rientjes@google.com, 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 V5 02/12] x86/hyperv: Initialize shared memory boundary in the Isolation VM. Date: Tue, 14 Sep 2021 09:39:03 -0400 Message-Id: <20210914133916.1440931-3-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210914133916.1440931-1-ltykernel@gmail.com> References: <20210914133916.1440931-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1631626789494100003 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 v4: * Rename reserve field. 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 b09ade389040..4794b716ec79 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..e04efb87fee5 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 reserved1 : 1; + u32 shared_gpa_boundary_active : 1; + u32 shared_gpa_boundary_bits : 6; + u32 reserved2 : 20; + }; + }; + u64 shared_gpa_boundary; }; extern struct ms_hyperv_info ms_hyperv; =20 --=20 2.25.1 From nobody Fri Apr 19 14:35:49 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=1631626794; cv=none; d=zohomail.com; s=zohoarc; b=CRiQlsh3OBviUkd7xit1NeEbrl4SkgA/JlsHTAO5PZDs0v59rwU1CgOfXIi6k/o1N9uWkRcdGopEpe48VwddLcsemeHwYCHAlxyDjLRTYD0U8DUjMH4fnm98cH2isivDlyiGYkByRvI3K5cN0Rt8oteoPV6swW28JpAeyEDBZag= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1631626794; 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=iJLckU3abLu4a4Vn3DJ3KB7tPATL+qkVDgrKdWDNQvA=; b=aYw8nsmH9ErM+bLI1lzoPQ6rOTn3hid3gYu50Kc4yh0zCblqID8pdAp572grrF6Asmp65BDC2Qzo2IIbzlmFoXLtobYiJB4CTHsP2nhzEZeb3iPFEqqqP/1EXZlxTmOglTYHlvB0MuDG1eDpkMPg1rP0nHWKiDOvrjOCY0V2P2Q= 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 1631626794847204.46114972014902; Tue, 14 Sep 2021 06:39:54 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.186650.335408 (Exim 4.92) (envelope-from ) id 1mQ8ey-0002wm-IN; Tue, 14 Sep 2021 13:39:40 +0000 Received: by outflank-mailman (output) from mailman id 186650.335408; Tue, 14 Sep 2021 13:39:40 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8ey-0002wV-F1; Tue, 14 Sep 2021 13:39:40 +0000 Received: by outflank-mailman (input) for mailman id 186650; Tue, 14 Sep 2021 13:39:39 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8ex-0001wz-R0 for xen-devel@lists.xenproject.org; Tue, 14 Sep 2021 13:39:39 +0000 Received: from mail-pj1-x102c.google.com (unknown [2607:f8b0:4864:20::102c]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 2e51e196-b0fe-4698-a9cd-225e048bc0d9; Tue, 14 Sep 2021 13:39:32 +0000 (UTC) Received: by mail-pj1-x102c.google.com with SMTP id oc9so8895861pjb.4 for ; Tue, 14 Sep 2021 06:39:32 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:7:6ea2:a529:4af3:5057]) by smtp.gmail.com with ESMTPSA id v13sm10461234pfm.16.2021.09.14.06.39.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:39:31 -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: 2e51e196-b0fe-4698-a9cd-225e048bc0d9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=iJLckU3abLu4a4Vn3DJ3KB7tPATL+qkVDgrKdWDNQvA=; b=fzFgKrgO3jR5OKFVzEwIfXK3KnTUpULKrPF1M+i/SNeZQDHvH52YDOWEoP4MQb1ozB +oNlHnH4gt/YrymRtTUrhVGA30YglWkcl2xkpeHfyB0Ic1vejz3UUxhTDRKfUqgp/HNN eQXM4aWS/XLaXBYpkvaCYSaW3dXCFEJUZGhqoZZlPKXdCGKbNvC6RMO9b6vH5Kze8cof w4hjDa73RlAeZRTFwZFgpYwyp7E56g8iDihxR5KWFZAHj35Qjhu4nFdLWoTi9ZEEaPj+ SOVkuFXNs1puyXoaXf2zO/cDECY6Ac/eRl7xItk7LRaW7iYZ6GlolkoDeF5TcexsMPxH CFRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iJLckU3abLu4a4Vn3DJ3KB7tPATL+qkVDgrKdWDNQvA=; b=kw8pUFCpi7GigK03gvmT2h9OdPqlJc8fjRBzGkjQuM4PtJfPPvsIAgIiP1EvyZdaiL KDYAZ+w+wqCg8m+v1DudiNPfUaeZ/PPTEw0o/CSLT66iiq6+MGxsGdPQtzhJjm0q+/xm c5sfoToHscNcrKcQUy5vHN0+o4R3BLG4fIT0CJBj2TI/ppriAF7htN/WhyknzPCwgOwt UET5FvXOiMfiqXmw0/V8ffolIEclX2/WW2yqEYEj32a/p76MZyCXFCJl3lTsyUhlJ6a/ J/8FccbTKS+sb9w45aWtmv4GaRZRtiARGYEhF5MI7LAhODHtl2qHIBOwWm03IHtzwTsF JAXQ== X-Gm-Message-State: AOAM530NwUhYAhK+flvTsS7FXEK1giNxTmvHrzROG3XYMiwWMEGSHxna 4emwoqne7iVFl1g7QHqwJ6o= X-Google-Smtp-Source: ABdhPJzvBiQY8jpYU+5kaM8wZ2SZ6gM/MP0CdPvROt1+d+Wt4/7tv+dIMdviYBvG4czVMjty3Drajw== X-Received: by 2002:a17:90b:a42:: with SMTP id gw2mr2163041pjb.26.1631626771780; Tue, 14 Sep 2021 06:39:31 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, 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, will@kernel.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, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, pgonda@google.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, aneesh.kumar@linux.ibm.com, saravanand@fb.com, krish.sadhukhan@oracle.com, xen-devel@lists.xenproject.org, tj@kernel.org, rientjes@google.com, 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 V5 03/12] x86/hyperv: Add new hvcall guest address host visibility support Date: Tue, 14 Sep 2021 09:39:04 -0400 Message-Id: <20210914133916.1440931-4-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210914133916.1440931-1-ltykernel@gmail.com> References: <20210914133916.1440931-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1631626796678100001 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 v4: * Fix typo in the comment * Make hv_mark_gpa_visibility() to be a static function * Merge __hv_set_mem_host_visibility() and hv_set_mem_host_visibility() 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 | 105 +++++++++++++++++++++++++++++ arch/x86/include/asm/hyperv-tlfs.h | 17 +++++ arch/x86/include/asm/mshyperv.h | 2 +- arch/x86/mm/pat/set_memory.c | 19 ++++-- include/asm-generic/hyperv-tlfs.h | 1 + include/asm-generic/mshyperv.h | 1 + 8 files changed, 146 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 a7e922755ad1..d57df6825527 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 false; + + if (!hypervisor_is_type(X86_HYPER_MS_HYPERV)) + return false; + 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..79e7fb83472a --- /dev/null +++ b/arch/x86/hyperv/ivm.c @@ -0,0 +1,105 @@ +// 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 encrypted from host and guest + * needs to set memory visible to host via hvcall before sharing memory + * with host. + */ +static 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; +} + +/* + * 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 kbuffer, int pagecount, bool = visible) +{ + enum hv_mem_host_visibility visibility =3D visible ? + VMBUS_PAGE_VISIBLE_READ_WRITE : VMBUS_PAGE_NOT_VISIBLE; + 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((void *)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; +} 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..ede440f9a1e2 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -192,7 +192,7 @@ 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_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 e04efb87fee5..cb529c85c0ad 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 bool hv_is_isolation_supported(void) { return false; } #endif /* CONFIG_HYPERV */ =20 #endif --=20 2.25.1 From nobody Fri Apr 19 14:35:49 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=1631626808; cv=none; d=zohomail.com; s=zohoarc; b=a/wlTgrePMaSj3FbCDCqPdOxCGphkOmpy3qVPOo/jNo1Nimb4UYWdY6xIgvkByUhSfz07vV1ct90qW0zaf2+4pPZ5kmtZndONTKBo18NSB9g9g50HfhvQEV1fI60xGP6/Z4HQEZYLLjkDMris77IZwwiCKRiyUfj1CpBiP84yPw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1631626808; 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=02yTUUUdT/7RUZ1OmKs0RzX0RhFgmF+tfZ6adAfAN0Q=; b=dyFkto3Xo/+NHLTj5Hjbjs+A2HjkYjrEg4hzhav7IqHPWWmaIln5Gs173OtJuJvcJJKt++R0rP2fLUrKMC1KWaB2wnBB13TJx4G4F4Kwy2PUbIxe+AvJgd68n1OUKfU77GVsBZxPPq0up2ESZkdpGLxRqsAlDg6TfE1g+1Uo17M= 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 1631626808174676.9687807944783; Tue, 14 Sep 2021 06:40:08 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.186654.335420 (Exim 4.92) (envelope-from ) id 1mQ8f9-0003cj-1V; Tue, 14 Sep 2021 13:39:51 +0000 Received: by outflank-mailman (output) from mailman id 186654.335420; Tue, 14 Sep 2021 13:39:50 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8f8-0003cW-TJ; Tue, 14 Sep 2021 13:39:50 +0000 Received: by outflank-mailman (input) for mailman id 186654; Tue, 14 Sep 2021 13:39:49 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8f7-0001wz-RQ for xen-devel@lists.xenproject.org; Tue, 14 Sep 2021 13:39:49 +0000 Received: from mail-pf1-x42a.google.com (unknown [2607:f8b0:4864:20::42a]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 0c1cf805-08fe-4e35-9547-26d1d010eea5; Tue, 14 Sep 2021 13:39:35 +0000 (UTC) Received: by mail-pf1-x42a.google.com with SMTP id j16so12255448pfc.2 for ; Tue, 14 Sep 2021 06:39:35 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:7:6ea2:a529:4af3:5057]) by smtp.gmail.com with ESMTPSA id v13sm10461234pfm.16.2021.09.14.06.39.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:39:33 -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: 0c1cf805-08fe-4e35-9547-26d1d010eea5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=02yTUUUdT/7RUZ1OmKs0RzX0RhFgmF+tfZ6adAfAN0Q=; b=ArjBR52nkO4bzAaOf5wvfC4rn632GJGm1maXaaJ0TgHHpjedDqm+WJBv+1VeIaZgH8 5flQwVivXRx6pq+gRBaVASuOXo4ymVynu6o0WucDl6BnBxy/91jZ+nwMIgdhlKICaUAN fL2xHyxJgQNN+aDYvnsnscMMXj3E3OC8kfXdFSGOUc0+WouUasML3nSV8Cf8i+dRrNsL SBZXPNecPo4cfaG1kwB4eYB7mQqvkhfeKDY6slY3Csa2hCRcX2Jhc3s7mqeRwF6/hgpa VplUwV00fbi/jQIiWIZD05ygm/4jJojIcYosTkxoDhhh7QGJk2WMeezVJnuWZVbEcLJ2 7OGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=02yTUUUdT/7RUZ1OmKs0RzX0RhFgmF+tfZ6adAfAN0Q=; b=adH0pCISCGDrI3QbxvZ4akEWsxwYW7CPESIhHZGIMhaKt96J7om7vPgKenfJ1/ZrFq WqeKGzVfdD6EvVHEbs/dGFeNXrZ7e7cnPCETN1E/iViAY7HWRR6/2H34H1TQQ6LwdRlb WLAAyE5w+6dBJGHFT7dgoJPRKiV8MUGiaP6FLmybCj+GcAiFAgO6da7F+jm2DLZoWjxR kR1/N7h6KhJAmiVCPeJYFv3Zk4lpynbjfR3dlLDDDDwlTy4SHCErzWLuAw8yniWd3k6Y x5p97oPD8uur47Sh+FRIzgB0vzPJ5bVEMZNGyOaAiGfnPav6FWMVsgdl2Oh4BEB7IT6g Srgg== X-Gm-Message-State: AOAM531Z8PlRy5qXZXvnW+oYr2dRDwMHMn5PCbhMgrRS2j430bBmCeWf NOMD2OCFUFG9YSKJATKjHLY= X-Google-Smtp-Source: ABdhPJzPDtIgzdi7XXhZ9rLdA7pHLUxlkJdK1Y4rWJkJJlHJjwPXRjwfsPAQ3lITRYtRblyGoAlIXQ== X-Received: by 2002:a63:6544:: with SMTP id z65mr15696367pgb.325.1631626774304; Tue, 14 Sep 2021 06:39:34 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, 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, will@kernel.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, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, pgonda@google.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, aneesh.kumar@linux.ibm.com, saravanand@fb.com, krish.sadhukhan@oracle.com, xen-devel@lists.xenproject.org, tj@kernel.org, rientjes@google.com, 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 V5 04/12] Drivers: hv: vmbus: Mark vmbus ring buffer visible to host in Isolation VM Date: Tue, 14 Sep 2021 09:39:05 -0400 Message-Id: <20210914133916.1440931-5-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210914133916.1440931-1-ltykernel@gmail.com> References: <20210914133916.1440931-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1631626810202100003 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 sincv v4 * Change gpadl handle in netvsc and uio driver from u32 to struct vmbus_gpadl. * Change vmbus_establish_gpadl()'s gpadl_handle parameter to vmbus_gpadl data structure. 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 | 54 ++++++++++++++++++++++++--------- drivers/net/hyperv/hyperv_net.h | 5 +-- drivers/net/hyperv/netvsc.c | 17 ++++++----- drivers/uio/uio_hv_generic.c | 20 ++++++------ include/linux/hyperv.h | 12 ++++++-- 5 files changed, 71 insertions(+), 37 deletions(-) diff --git a/drivers/hv/channel.c b/drivers/hv/channel.c index f3761c73b074..cf419eb1de77 100644 --- a/drivers/hv/channel.c +++ b/drivers/hv/channel.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include =20 @@ -456,7 +457,7 @@ static int create_gpadl_header(enum hv_gpadl_type type,= void *kbuffer, static int __vmbus_establish_gpadl(struct vmbus_channel *channel, enum hv_gpadl_type type, void *kbuffer, u32 size, u32 send_offset, - u32 *gpadl_handle) + struct vmbus_gpadl *gpadl) { struct vmbus_channel_gpadl_header *gpadlmsg; struct vmbus_channel_gpadl_body *gpadl_body; @@ -474,6 +475,15 @@ 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) { + dev_warn(&channel->device_obj->device, + "Failed to set host visibility for new GPADL %d.\n", + ret); + return ret; + } + init_completion(&msginfo->waitevent); msginfo->waiting_channel =3D channel; =20 @@ -537,7 +547,10 @@ static int __vmbus_establish_gpadl(struct vmbus_channe= l *channel, } =20 /* At this point, we received the gpadl created msg */ - *gpadl_handle =3D gpadlmsg->gpadl; + gpadl->gpadl_handle =3D gpadlmsg->gpadl; + gpadl->buffer =3D kbuffer; + gpadl->size =3D size; + =20 cleanup: spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); @@ -549,6 +562,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 @@ -561,10 +579,10 @@ static int __vmbus_establish_gpadl(struct vmbus_chann= el *channel, * @gpadl_handle: some funky thing */ int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer, - u32 size, u32 *gpadl_handle) + u32 size, struct vmbus_gpadl *gpadl) { return __vmbus_establish_gpadl(channel, HV_GPADL_BUFFER, kbuffer, size, - 0U, gpadl_handle); + 0U, gpadl); } EXPORT_SYMBOL_GPL(vmbus_establish_gpadl); =20 @@ -639,6 +657,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; @@ -675,7 +694,7 @@ static int __vmbus_open(struct vmbus_channel *newchanne= l, goto error_clean_ring; =20 /* Establish the gpadl for the ring buffer */ - newchannel->ringbuffer_gpadlhandle =3D 0; + newchannel->ringbuffer_gpadlhandle.gpadl_handle =3D 0; =20 err =3D __vmbus_establish_gpadl(newchannel, HV_GPADL_RING, page_address(newchannel->ringbuffer_page), @@ -701,7 +720,8 @@ static int __vmbus_open(struct vmbus_channel *newchanne= l, open_msg->header.msgtype =3D CHANNELMSG_OPENCHANNEL; open_msg->openid =3D newchannel->offermsg.child_relid; open_msg->child_relid =3D newchannel->offermsg.child_relid; - open_msg->ringbuffer_gpadlhandle =3D newchannel->ringbuffer_gpadlhandle; + open_msg->ringbuffer_gpadlhandle + =3D newchannel->ringbuffer_gpadlhandle.gpadl_handle; /* * The unit of ->downstream_ringbuffer_pageoffset is HV_HYP_PAGE and * the unit of ->ringbuffer_send_offset (i.e. send_pages) is PAGE, so @@ -759,8 +779,8 @@ static int __vmbus_open(struct vmbus_channel *newchanne= l, error_free_info: kfree(open_info); error_free_gpadl: - vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle); - newchannel->ringbuffer_gpadlhandle =3D 0; + vmbus_teardown_gpadl(newchannel, &newchannel->ringbuffer_gpadlhandle); + newchannel->ringbuffer_gpadlhandle.gpadl_handle =3D 0; error_clean_ring: hv_ringbuffer_cleanup(&newchannel->outbound); hv_ringbuffer_cleanup(&newchannel->inbound); @@ -806,7 +826,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 +845,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 +879,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 +922,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); @@ -933,9 +960,8 @@ static int vmbus_close_internal(struct vmbus_channel *c= hannel) } =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); + else if (channel->ringbuffer_gpadlhandle.gpadl_handle) { + ret =3D vmbus_teardown_gpadl(channel, &channel->ringbuffer_gpadlhandle); if (ret) { pr_err("Close failed: teardown gpadl return %d\n", ret); /* @@ -944,7 +970,7 @@ static int vmbus_close_internal(struct vmbus_channel *c= hannel) */ } =20 - channel->ringbuffer_gpadlhandle =3D 0; + channel->ringbuffer_gpadlhandle.gpadl_handle =3D 0; } =20 if (!ret) diff --git a/drivers/net/hyperv/hyperv_net.h b/drivers/net/hyperv/hyperv_ne= t.h index bc48855dff10..315278a7cf88 100644 --- a/drivers/net/hyperv/hyperv_net.h +++ b/drivers/net/hyperv/hyperv_net.h @@ -1075,14 +1075,15 @@ struct netvsc_device { /* Receive buffer allocated by us but manages by NetVSP */ void *recv_buf; u32 recv_buf_size; /* allocated bytes */ - u32 recv_buf_gpadl_handle; + struct vmbus_gpadl recv_buf_gpadl_handle; u32 recv_section_cnt; u32 recv_section_size; u32 recv_completion_cnt; =20 /* Send buffer allocated by us */ void *send_buf; - u32 send_buf_gpadl_handle; + u32 send_buf_size; + struct vmbus_gpadl send_buf_gpadl_handle; u32 send_section_cnt; u32 send_section_size; unsigned long *send_section_map; diff --git a/drivers/net/hyperv/netvsc.c b/drivers/net/hyperv/netvsc.c index 7bd935412853..1f87e570ed2b 100644 --- a/drivers/net/hyperv/netvsc.c +++ b/drivers/net/hyperv/netvsc.c @@ -278,9 +278,9 @@ static void netvsc_teardown_recv_gpadl(struct hv_device= *device, { int ret; =20 - if (net_device->recv_buf_gpadl_handle) { + if (net_device->recv_buf_gpadl_handle.gpadl_handle) { ret =3D vmbus_teardown_gpadl(device->channel, - net_device->recv_buf_gpadl_handle); + &net_device->recv_buf_gpadl_handle); =20 /* If we failed here, we might as well return and have a leak * rather than continue and a bugchk @@ -290,7 +290,7 @@ static void netvsc_teardown_recv_gpadl(struct hv_device= *device, "unable to teardown receive buffer's gpadl\n"); return; } - net_device->recv_buf_gpadl_handle =3D 0; + net_device->recv_buf_gpadl_handle.gpadl_handle =3D 0; } } =20 @@ -300,9 +300,9 @@ static void netvsc_teardown_send_gpadl(struct hv_device= *device, { int ret; =20 - if (net_device->send_buf_gpadl_handle) { + if (net_device->send_buf_gpadl_handle.gpadl_handle) { ret =3D vmbus_teardown_gpadl(device->channel, - net_device->send_buf_gpadl_handle); + &net_device->send_buf_gpadl_handle); =20 /* If we failed here, we might as well return and have a leak * rather than continue and a bugchk @@ -312,7 +312,7 @@ static void netvsc_teardown_send_gpadl(struct hv_device= *device, "unable to teardown send buffer's gpadl\n"); return; } - net_device->send_buf_gpadl_handle =3D 0; + net_device->send_buf_gpadl_handle.gpadl_handle =3D 0; } } =20 @@ -380,7 +380,7 @@ static int netvsc_init_buf(struct hv_device *device, memset(init_packet, 0, sizeof(struct nvsp_message)); init_packet->hdr.msg_type =3D NVSP_MSG1_TYPE_SEND_RECV_BUF; init_packet->msg.v1_msg.send_recv_buf. - gpadl_handle =3D net_device->recv_buf_gpadl_handle; + gpadl_handle =3D net_device->recv_buf_gpadl_handle.gpadl_handle; init_packet->msg.v1_msg. send_recv_buf.id =3D NETVSC_RECEIVE_BUFFER_ID; =20 @@ -463,6 +463,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 @@ -482,7 +483,7 @@ static int netvsc_init_buf(struct hv_device *device, memset(init_packet, 0, sizeof(struct nvsp_message)); init_packet->hdr.msg_type =3D NVSP_MSG1_TYPE_SEND_SEND_BUF; init_packet->msg.v1_msg.send_send_buf.gpadl_handle =3D - net_device->send_buf_gpadl_handle; + net_device->send_buf_gpadl_handle.gpadl_handle; init_packet->msg.v1_msg.send_send_buf.id =3D NETVSC_SEND_BUFFER_ID; =20 trace_nvsp_send(ndev, init_packet); diff --git a/drivers/uio/uio_hv_generic.c b/drivers/uio/uio_hv_generic.c index 652fe2547587..548243dcd895 100644 --- a/drivers/uio/uio_hv_generic.c +++ b/drivers/uio/uio_hv_generic.c @@ -58,11 +58,11 @@ struct hv_uio_private_data { atomic_t refcnt; =20 void *recv_buf; - u32 recv_gpadl; + struct vmbus_gpadl recv_gpadl; char recv_name[32]; /* "recv_4294967295" */ =20 void *send_buf; - u32 send_gpadl; + struct vmbus_gpadl send_gpadl; char send_name[32]; }; =20 @@ -179,15 +179,15 @@ hv_uio_new_channel(struct vmbus_channel *new_sc) static void hv_uio_cleanup(struct hv_device *dev, struct hv_uio_private_data *pdata) { - if (pdata->send_gpadl) { - vmbus_teardown_gpadl(dev->channel, pdata->send_gpadl); - pdata->send_gpadl =3D 0; + if (pdata->send_gpadl.gpadl_handle) { + vmbus_teardown_gpadl(dev->channel, &pdata->send_gpadl); + pdata->send_gpadl.gpadl_handle =3D 0; vfree(pdata->send_buf); } =20 - if (pdata->recv_gpadl) { - vmbus_teardown_gpadl(dev->channel, pdata->recv_gpadl); - pdata->recv_gpadl =3D 0; + if (pdata->recv_gpadl.gpadl_handle) { + vmbus_teardown_gpadl(dev->channel, &pdata->recv_gpadl); + pdata->recv_gpadl.gpadl_handle =3D 0; vfree(pdata->recv_buf); } } @@ -303,7 +303,7 @@ hv_uio_probe(struct hv_device *dev, =20 /* put Global Physical Address Label in name */ snprintf(pdata->recv_name, sizeof(pdata->recv_name), - "recv:%u", pdata->recv_gpadl); + "recv:%u", pdata->recv_gpadl.gpadl_handle); pdata->info.mem[RECV_BUF_MAP].name =3D pdata->recv_name; pdata->info.mem[RECV_BUF_MAP].addr =3D (uintptr_t)pdata->recv_buf; @@ -324,7 +324,7 @@ hv_uio_probe(struct hv_device *dev, } =20 snprintf(pdata->send_name, sizeof(pdata->send_name), - "send:%u", pdata->send_gpadl); + "send:%u", pdata->send_gpadl.gpadl_handle); pdata->info.mem[SEND_BUF_MAP].name =3D pdata->send_name; pdata->info.mem[SEND_BUF_MAP].addr =3D (uintptr_t)pdata->send_buf; diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h index ddc8713ce57b..a9e0bc3b1511 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 @@ -822,7 +828,7 @@ struct vmbus_channel { bool rescind_ref; /* got rescind msg, got channel reference */ struct completion rescind_event; =20 - u32 ringbuffer_gpadlhandle; + struct vmbus_gpadl ringbuffer_gpadlhandle; =20 /* Allocated memory for ring buffer */ struct page *ringbuffer_page; @@ -1192,10 +1198,10 @@ extern int vmbus_sendpacket_mpb_desc(struct vmbus_c= hannel *channel, extern int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer, u32 size, - u32 *gpadl_handle); + struct vmbus_gpadl *gpadl); =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 Fri Apr 19 14:35:49 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=1631626820; cv=none; d=zohomail.com; s=zohoarc; b=NLpTXi8/t+BqvDUWhdgNEQfISlT3pIARFH8H7PPrhfyMsuYjz0KbIhO7CJMnHOSSIvT0erD2mzoLPJTv1uUz4Xtus0exxyWhvB0P9YWmsfLCnnV5n2Fdp8ny5InoVyTzftVkZdTzik9kbusVM66ZGY4SfVN/hwhUTnCefUefP2E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1631626820; 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=zUkXn4sp/qQgiPEGSRnTvw2J9RtbphVDtIRuiQqhe5I=; b=eVgfXDuE4UZi73SAxHjzWL7Mg5J4b8MO5Ig7Sn8jmDqjVI+lib0wf5+Jqu6HQ4K3YikY2E6fHa7opdv05Fvteo0/OhbcQSUcrM+3S3S3QtQTWZKfjqVKeI/eF9yDOYF6uwTcKdXHN8n3fA6yHqbTmpwGl6JbfWJLoCBE97eqoww= 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 1631626820101100.85967561352629; Tue, 14 Sep 2021 06:40:20 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.186664.335431 (Exim 4.92) (envelope-from ) id 1mQ8fJ-0004PE-Aq; Tue, 14 Sep 2021 13:40:01 +0000 Received: by outflank-mailman (output) from mailman id 186664.335431; Tue, 14 Sep 2021 13:40:01 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8fJ-0004Of-7g; Tue, 14 Sep 2021 13:40:01 +0000 Received: by outflank-mailman (input) for mailman id 186664; Tue, 14 Sep 2021 13:39:59 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8fH-0001wz-Rd for xen-devel@lists.xenproject.org; Tue, 14 Sep 2021 13:39:59 +0000 Received: from mail-pg1-x52f.google.com (unknown [2607:f8b0:4864:20::52f]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id a8072179-f310-4121-9ce6-f13ea5cbfd79; Tue, 14 Sep 2021 13:39:37 +0000 (UTC) Received: by mail-pg1-x52f.google.com with SMTP id w8so12730199pgf.5 for ; Tue, 14 Sep 2021 06:39:37 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:7:6ea2:a529:4af3:5057]) by smtp.gmail.com with ESMTPSA id v13sm10461234pfm.16.2021.09.14.06.39.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:39:36 -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: a8072179-f310-4121-9ce6-f13ea5cbfd79 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zUkXn4sp/qQgiPEGSRnTvw2J9RtbphVDtIRuiQqhe5I=; b=qZtlo7fWNwe59y7IrBkgFI8skL03Pkvbnc/q8fXggTmEI9tn+Ho4t22cbkbvbJovOT mWtn7+Cl7NIOidFVYUEmp1/a18j9Ui2v0aZn2WlO1fIZSVhlpWJn2wLOEVZJU65hpDKS TbjN1BhZEYIH/Vptm0SF+Dg3jmqz1QMh7FL6rNTY+16gkezB327gktsNZG8tMQQLvlOk KwGOvjQ6llHrapWMeKShRAOx/cjevc0rB28Cv4aPicR1r2d3Ncwkv4mIgfHZ7oE7UyLw 4xcNunfiZFvZIp1vvt3X8dea3yvRpEHd3BkS9EhKCO9Pv71tFLWLFB8cf8kjQ0T1R9cq 0jjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zUkXn4sp/qQgiPEGSRnTvw2J9RtbphVDtIRuiQqhe5I=; b=N6Ym97YxD5KbTwNkhhYaS0W4BRPuzd6vKqP/hve5UXhXpcMfZV5WLDicW2XyKZj5E7 dEZQnY4hXfuWIHoDFN/h0RksKx101LUuyHoqwluu4v6cPAE++p45uTOfVwnECSrzuqm9 9h89BDlHk5K24FVxb7lSi4Z9xfA2ebWgozzcBXGUPJTJZDEFiGTTzxmAMwOWQd1y2q+/ iKGEhOOvtIILf4/+RXYKnBFOsiHEvHAywDITDzRcI54iNO2XIVinSJmYWng7ndKOgqKQ eN6X9+n2AfBFWGWMkXuYhdJePKHEy/t8sYPxejyoCh+5VFJ0rglkyzEJGgUxjQeEtdoA fv0w== X-Gm-Message-State: AOAM531uHPCGyNf+3FOAejN9RgWCWLIS0yJ1NsYnZpNHHv6rswpFQL7w +5vjAUXzXHslZrWTiF52v+Edm4oDOdavEw== X-Google-Smtp-Source: ABdhPJzmO9j1fgt6LuREDGB93sw+XgWPBQLWOatojtm5X4J7XEc1TUjh25t15N4hYiTqkBTF0edZnw== X-Received: by 2002:a63:740e:: with SMTP id p14mr15442707pgc.329.1631626777043; Tue, 14 Sep 2021 06:39:37 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, 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, will@kernel.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, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, pgonda@google.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, aneesh.kumar@linux.ibm.com, saravanand@fb.com, krish.sadhukhan@oracle.com, xen-devel@lists.xenproject.org, tj@kernel.org, rientjes@google.com, 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 V5 05/12] x86/hyperv: Add Write/Read MSR registers via ghcb page Date: Tue, 14 Sep 2021 09:39:06 -0400 Message-Id: <20210914133916.1440931-6-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210914133916.1440931-1-ltykernel@gmail.com> References: <20210914133916.1440931-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1631626820969100001 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 v4: * Remove hv_get_simp(), hv_get_siefp() hv_get_synint_*() helper function. Move the logic into hv_get/set_register(). 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. Change since v1: * Introduce sev_es_ghcb_hv_call_simple() and share code between SEV and Hyper-V code. Fix for hyperv: Add Write/Read MSR registers via ghcb page --- arch/x86/hyperv/hv_init.c | 36 +++-------- arch/x86/hyperv/ivm.c | 103 ++++++++++++++++++++++++++++++++ arch/x86/include/asm/mshyperv.h | 56 ++++++++++++----- arch/x86/include/asm/sev.h | 6 ++ arch/x86/kernel/sev-shared.c | 63 +++++++++++-------- drivers/hv/hv.c | 77 +++++++++++++++++++----- drivers/hv/hv_common.c | 6 ++ include/asm-generic/mshyperv.h | 2 + 8 files changed, 266 insertions(+), 83 deletions(-) diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c index d57df6825527..a16a83e46a30 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 false; - - if (!hypervisor_is_type(X86_HYPER_MS_HYPERV)) - return false; - - 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 79e7fb83472a..5439723446c9 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -6,12 +6,115 @@ * Tianyu Lan */ =20 +#include +#include #include #include #include #include +#include +#include #include #include +#include + +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); +} + +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) +{ + if (!cpu_feature_enabled(X86_FEATURE_HYPERVISOR)) + return false; + + if (!hypervisor_is_type(X86_HYPER_MS_HYPERV)) + return false; + + 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); +} =20 /* * 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 ede440f9a1e2..165423e8b67a 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -11,25 +11,14 @@ #include #include =20 +union hv_ghcb; + DECLARE_STATIC_KEY_FALSE(isolation_type_snp); =20 typedef int (*hyperv_fill_flush_list_func)( struct hv_guest_mapping_flush_list *flush, void *data); =20 -static inline void hv_set_register(unsigned int reg, u64 value) -{ - wrmsrl(reg, value); -} - -static inline u64 hv_get_register(unsigned int reg) -{ - u64 value; - - rdmsrl(reg, value); - return value; -} - #define hv_get_raw_timer() rdtsc_ordered() =20 void hyperv_vector_handler(struct pt_regs *regs); @@ -41,7 +30,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); @@ -193,6 +182,8 @@ int hv_map_ioapic_interrupt(int ioapic_id, bool level, = int vcpu, int vector, struct hv_interrupt_entry *entry); int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *en= try); int hv_set_mem_host_visibility(unsigned long addr, int numpages, bool visi= ble); +void hv_ghcb_msr_write(u64 msr, u64 value); +void hv_ghcb_msr_read(u64 msr, u64 *value); #else /* CONFIG_HYPERV */ static inline void hyperv_init(void) {} static inline void hyperv_setup_mmu_ops(void) {} @@ -209,9 +200,46 @@ static inline int hyperv_flush_guest_mapping_range(u64= as, { return -1; } + +static inline void hv_ghcb_msr_write(u64 msr, u64 value) {} +static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {} #endif /* CONFIG_HYPERV */ =20 +static inline void hv_set_register(unsigned int reg, u64 value); =20 #include =20 +static inline bool hv_is_synic_reg(unsigned int reg) +{ + if ((reg >=3D HV_REGISTER_SCONTROL) && + (reg <=3D HV_REGISTER_SINT15)) + return true; + return false; +} + +static inline u64 hv_get_register(unsigned int reg) +{ + u64 value; + + if (hv_is_synic_reg(reg) && hv_isolation_type_snp()) + hv_ghcb_msr_read(reg, &value); + else + rdmsrl(reg, value); + return value; +} + +static inline void hv_set_register(unsigned int reg, u64 value) +{ + if (hv_is_synic_reg(reg) && hv_isolation_type_snp()) { + hv_ghcb_msr_write(reg, value); + + /* Write proxy bit via wrmsl instruction */ + if (reg >=3D HV_REGISTER_SINT0 && + reg <=3D HV_REGISTER_SINT15) + wrmsrl(reg, value | 1 << 20); + } else { + wrmsrl(reg, value); + } +} + #endif diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index fa5cd05d3b5b..60bfdbd141b1 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -81,12 +81,18 @@ 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) { } static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh)= { return 0; } static inline void sev_es_nmi_complete(void) { } static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; } +static inline enum es_result sev_es_ghcb_hv_call_simple(struct ghcb *ghcb, + u64 exit_code, u64 exit_info_1, + u64 exit_info_2) { return ES_VMM_ERROR; } #endif =20 #endif 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..dee1a96bc535 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); @@ -201,16 +209,35 @@ void hv_synic_enable_regs(unsigned int cpu) /* Setup the Synic's message page */ simp.as_uint64 =3D hv_get_register(HV_REGISTER_SIMP); simp.simp_enabled =3D 1; - simp.base_simp_gpa =3D virt_to_phys(hv_cpu->synic_message_page) - >> HV_HYP_PAGE_SHIFT; + + 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; + } =20 hv_set_register(HV_REGISTER_SIMP, simp.as_uint64); =20 /* Setup the Synic's event page */ siefp.as_uint64 =3D hv_get_register(HV_REGISTER_SIEFP); siefp.siefp_enabled =3D 1; - siefp.base_siefp_gpa =3D virt_to_phys(hv_cpu->synic_event_page) - >> HV_HYP_PAGE_SHIFT; + + 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; + } =20 hv_set_register(HV_REGISTER_SIEFP, siefp.as_uint64); =20 @@ -257,30 +284,48 @@ 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); =20 shared_sint.masked =3D 1; =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); =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); =20 siefp.as_uint64 =3D hv_get_register(HV_REGISTER_SIEFP); siefp.siefp_enabled =3D 0; - siefp.base_siefp_gpa =3D 0; + + if (hv_isolation_type_snp()) + memunmap(hv_cpu->synic_event_page); + else + siefp.base_siefp_gpa =3D 0; =20 hv_set_register(HV_REGISTER_SIEFP, siefp.as_uint64); =20 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 cb529c85c0ad..94750bafd4cc 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) --=20 2.25.1 From nobody Fri Apr 19 14:35:49 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=1631626824; cv=none; d=zohomail.com; s=zohoarc; b=POdL34pHxTd5JonZMFikRmBQJZAGtKI0Wo/Yh2n2Tam/JFxVTXIIBW+SGMrnf4qbh0bXcax9242DEobA5WpYZEZGsuGQoqKRD7vbW9TonrbH2V2uPk3GJ5NcBNWnXGh9Q5w/iTnF0lIVTwWpUM3FjfInfrVHWsM6eAMp7gWnvd4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1631626824; 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=azCewkuzRAkBZyiDOjmA0vSW/QH/3obR4Ytj+SPfQzQ=; b=LrmuA9Z5Hf8BvSicCt0KS8dHMbsf2+qxW9ZBgvSgY2yX6SC9AFntcNpLwbjh3u1rbKJ4ppXjCPIrcc/OhyVvN+0UDJEiGLoiqJXvGUxmZfAANVnE/LTckksjvYmRtPUiMBBaKsLkDmve3530a7SqswJkF/OIgMSABak/O4dSAR0= 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 1631626823999219.22034948666658; Tue, 14 Sep 2021 06:40:23 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.186671.335442 (Exim 4.92) (envelope-from ) id 1mQ8fS-0005ht-QQ; Tue, 14 Sep 2021 13:40:10 +0000 Received: by outflank-mailman (output) from mailman id 186671.335442; Tue, 14 Sep 2021 13:40:10 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8fS-0005hg-N6; Tue, 14 Sep 2021 13:40:10 +0000 Received: by outflank-mailman (input) for mailman id 186671; Tue, 14 Sep 2021 13:40:09 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8fR-0001wz-SB for xen-devel@lists.xenproject.org; Tue, 14 Sep 2021 13:40:09 +0000 Received: from mail-pj1-x102f.google.com (unknown [2607:f8b0:4864:20::102f]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 25a0bd9d-8c5d-4bd4-8d96-9a0e7cc43ac6; Tue, 14 Sep 2021 13:39:40 +0000 (UTC) Received: by mail-pj1-x102f.google.com with SMTP id lb1-20020a17090b4a4100b001993f863df2so2799919pjb.5 for ; Tue, 14 Sep 2021 06:39:40 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:7:6ea2:a529:4af3:5057]) by smtp.gmail.com with ESMTPSA id v13sm10461234pfm.16.2021.09.14.06.39.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:39:39 -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: 25a0bd9d-8c5d-4bd4-8d96-9a0e7cc43ac6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=azCewkuzRAkBZyiDOjmA0vSW/QH/3obR4Ytj+SPfQzQ=; b=YK5mVlypHJRSS7afMsq7DyxTNlQ/p9SKLwx+cOOkVvxOGeuM6eWoauMwXmPc1oqD+h 8e/7mt+/7qYq08phSAIbvSPhlho5l6bHdYmjrjK0P59fIP9AAVx6da+9UhivbazZeao7 hkI2XuYefI+84XSLv5sDvueSTlukH2kXJ0Bo0iKwh+5F/fDZNQK+wmNjmUUu8Uuiu0ht b+s3OSft1wHLaAepa21DDSmakrwSRefK8GEX0khdKFbF/DlzqOOFmYHIZBetHsCFHxn8 r46AcAy8J11/ApUlIPT+c5gLWcT9l6OhimerwJRDl7pJyHQqUxUDREPhPiA5a1Miq6DQ Eslw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=azCewkuzRAkBZyiDOjmA0vSW/QH/3obR4Ytj+SPfQzQ=; b=V+HuCxcKoiIzqmMhXv6qTOn2IWoBbB3AcDqWD2yP3YGzcK1D8m18HTK9T3fFkkrwr4 KrQfrxwEMxxS2VfN9wQv6ygr43TpKi8vZjJZGyfqCpfFUnxqVBJ+O+n1xSpyiAS3d0OL FnJr4DFVBmXa4Ka/9IwpMXyV9azqGzktQp/99L3Aw9nG8wfu+qrheEGrnB0GGVloAfVZ +5gdvQAfKVkLF6Fl8PZME6YSUF3udC0bOMKWk9/Zbz4VYTtJIC59atKJuDYy5jbCxbrE hzIBVP6e68A4RVj6wGatA39kq+Znxn3QWd6FZBNcjFKLBvX56evf1X9QK2brA8ESYZSl 8O+w== X-Gm-Message-State: AOAM533Z93PB4UOEX4Aab50Ri1eh4r7NKGDzFMNjoKa09vu2nGC78Spa l6kip0yXQsRFRda1UX2PoTs= X-Google-Smtp-Source: ABdhPJztnF9gFB+MKR5DdaKIrIGDyLZ4ZBz+QRP7UkoIajfwGN+xMzTLqQUKzjlgKwa664+jV4M3Pw== X-Received: by 2002:a17:903:1207:b0:138:e2f9:6c98 with SMTP id l7-20020a170903120700b00138e2f96c98mr15512885plh.11.1631626779382; Tue, 14 Sep 2021 06:39:39 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, 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, will@kernel.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, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, pgonda@google.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, aneesh.kumar@linux.ibm.com, saravanand@fb.com, krish.sadhukhan@oracle.com, xen-devel@lists.xenproject.org, tj@kernel.org, rientjes@google.com, 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 V5 06/12] x86/hyperv: Add ghcb hvcall support for SNP VM Date: Tue, 14 Sep 2021 09:39:07 -0400 Message-Id: <20210914133916.1440931-7-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210914133916.1440931-1-ltykernel@gmail.com> References: <20210914133916.1440931-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1631626825410100001 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 | 74 ++++++++++++++++++++++++++++++++++ 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, 93 insertions(+), 2 deletions(-) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index 5439723446c9..dfdac3a40036 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -18,10 +18,84 @@ #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; + u64 status; + + 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)); + + status =3D hv_ghcb->hypercall.hypercalloutput.callstatus; + + local_irq_restore(flags); + + return status; +} + 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..8820ae68f20f 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(channel->sig_event)); + 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 dee1a96bc535..5644ba2bfa5c 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(*aligned_msg)); + 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 94750bafd4cc..a0ec607a2fd6 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 Fri Apr 19 14:35:49 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=1631627242; cv=none; d=zohomail.com; s=zohoarc; b=Tb9sRmi1LXhE5VtSFtoEIsFVx16nqVPA/YPiyLGWsQVPqBk8dqPH5c/YBks4UXBfro0gmp0V4bs/BeCG11IZk8Fcu1gHGAPvQ6jsQHLtx0omMNEhLUaEb+85qZTpUh4tnIbwaExb4uJNvF/fzQQOq/uRlbTmGIm3+w73bleb9Fw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1631627242; 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=04R/nF7ZQn0L4lmwG9K7NZhI1RIoqJtAembHuZpzcoE=; b=e/qWdQSd6JrB6teXHj7sQqrOqNcDtd0EwuhYxAJnfJEk3WdcLFpf+mph/DK56oLTTlU3WcJ2XyKTJ2dam02CHhjHTjJc8tS3dCi1Zt9ZAs1dNBu+EEQxW4K3taOnlbtNNH6EPdapRhb2/V+kAKs9tD+h0GToG7GYhgX+5iMqjq4= 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 16316272429681011.8693497898479; Tue, 14 Sep 2021 06:47:22 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.186687.335452 (Exim 4.92) (envelope-from ) id 1mQ8m5-0007Al-It; Tue, 14 Sep 2021 13:47:01 +0000 Received: by outflank-mailman (output) from mailman id 186687.335452; Tue, 14 Sep 2021 13:47:01 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8m5-0007Ae-FD; Tue, 14 Sep 2021 13:47:01 +0000 Received: by outflank-mailman (input) for mailman id 186687; Tue, 14 Sep 2021 13:46:59 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8fl-0001wz-So for xen-devel@lists.xenproject.org; Tue, 14 Sep 2021 13:40:29 +0000 Received: from mail-pg1-x532.google.com (unknown [2607:f8b0:4864:20::532]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 41ee5e18-fc5d-4791-9e59-19ee5069d3d7; Tue, 14 Sep 2021 13:39:42 +0000 (UTC) Received: by mail-pg1-x532.google.com with SMTP id r2so12725885pgl.10 for ; Tue, 14 Sep 2021 06:39:42 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:7:6ea2:a529:4af3:5057]) by smtp.gmail.com with ESMTPSA id v13sm10461234pfm.16.2021.09.14.06.39.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:39: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: 41ee5e18-fc5d-4791-9e59-19ee5069d3d7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=04R/nF7ZQn0L4lmwG9K7NZhI1RIoqJtAembHuZpzcoE=; b=cXWjfh70bA6/tTG3cRi7I0j1g+p2Lm3CJSGXyCEtns1z4ew1JMJSouaTXxuUkcmAAF pD2ztmNCM/EaSPUeDDaq6UxsWFBxK9H35yFnlW25xXgoMBjP2i0pScCK83QZEclf8ecy N9PLUvbIXVRHq4/y1x9TK184cTltER/wXcMXRfCw3qyn5rb59Xp4R9JHz9uej2V7BUzJ 4nbgXuO4P+lxZbUEZEs/1u05P/Zm7T9ezY8W5GtsXzTaY96NJ/PhksjnQl9x9HnadNWx cTQCObr34jF7frx/WSelSqFMtM9MhrWIvfcA85EYhYphGuBONqJDe9RZDWb/6vKzX5CK TR2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=04R/nF7ZQn0L4lmwG9K7NZhI1RIoqJtAembHuZpzcoE=; b=xbK8sNR9lALL8t5RW+5eQocFepqs38aoTNCOArRHMJ0iAZ6P6kGf7R2dWMjz24/xyA ufV0fLfKhh++ooFMDNc0/RyzwTeu7BirQrIWPYeO6JGIwKNtEafubd1o0vM9xSeRUjCK FRszI4p9bdu4yyDJoMQylsWRhYo/mlSJxDWM+yGehO3QBvASTgEPB2tPoRLWpEIPky3F ZOldbAUqX7AduFcxJ+Wl8XlHHzDpIX9NFEyIjQ9Hh9wnW+mw0G93TDtRzxRcHMIT3orF cSFW7Rcpwa0MllldUiTf+hPA6AStY+TLzyevOZhJWNkBWyyprRF3Nu+VRyjJiKyAYl4d 33wQ== X-Gm-Message-State: AOAM532eQmKRMwQiAyCqkWL3VlA5a6Q14o3mhW4G8aLk3Eb5aQkBC6Ub KWmXkOinW+LjJwuZDhg6vv0= X-Google-Smtp-Source: ABdhPJyVP18hjlg38ErK5oPFLiCLyC2tXuvAHanGQz+Hb5O3TLtyTYhzBvzQ3suhx9t9fxq63nkGDQ== X-Received: by 2002:a63:7112:: with SMTP id m18mr15630957pgc.93.1631626782143; Tue, 14 Sep 2021 06:39:42 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, 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, will@kernel.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, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, pgonda@google.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, aneesh.kumar@linux.ibm.com, saravanand@fb.com, krish.sadhukhan@oracle.com, xen-devel@lists.xenproject.org, tj@kernel.org, rientjes@google.com, 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 V5 07/12] Drivers: hv: vmbus: Add SNP support for VMbus channel initiate message Date: Tue, 14 Sep 2021 09:39:08 -0400 Message-Id: <20210914133916.1440931-8-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210914133916.1440931-1-ltykernel@gmail.com> References: <20210914133916.1440931-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1631627245789100001 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. Introduce monitor_pages_pa[] to store monitor pages' physical address and use it to populate pa in the initiate msg. Signed-off-by: Tianyu Lan --- Change since v4: * Introduce monitor_pages_pa[] to store monitor pages' physical address and use it to populate pa in the initiate msg. * Move code of mapping moniter pages in extra address into vmbus_connect(). 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 | 90 ++++++++++++++++++++++++++++++++++++--- drivers/hv/hyperv_vmbus.h | 2 + 2 files changed, 86 insertions(+), 6 deletions(-) diff --git a/drivers/hv/connection.c b/drivers/hv/connection.c index 8820ae68f20f..edd8f7dd169f 100644 --- a/drivers/hv/connection.c +++ b/drivers/hv/connection.c @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include =20 #include "hyperv_vmbus.h" @@ -102,8 +104,9 @@ int vmbus_negotiate_version(struct vmbus_channel_msginf= o *msginfo, u32 version) vmbus_connection.msg_conn_id =3D VMBUS_MESSAGE_CONNECTION_ID; } =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]); + msg->monitor_page1 =3D vmbus_connection.monitor_pages_pa[0]; + msg->monitor_page2 =3D vmbus_connection.monitor_pages_pa[1]; + msg->target_vcpu =3D hv_cpu_number_to_vp_number(VMBUS_CONNECT_CPU); =20 /* @@ -216,6 +219,65 @@ 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]; + vmbus_connection.monitor_pages_pa[0] + =3D virt_to_phys(vmbus_connection.monitor_pages[0]); + vmbus_connection.monitor_pages_pa[1] + =3D virt_to_phys(vmbus_connection.monitor_pages[1]); + + if (hv_is_isolation_supported()) { + vmbus_connection.monitor_pages_pa[0] +=3D + ms_hyperv.shared_gpa_boundary; + vmbus_connection.monitor_pages_pa[1] +=3D + ms_hyperv.shared_gpa_boundary; + + ret =3D set_memory_decrypted((unsigned long) + vmbus_connection.monitor_pages[0], + 1); + ret |=3D set_memory_decrypted((unsigned long) + vmbus_connection.monitor_pages[1], + 1); + if (ret) + goto cleanup; + + /* + * Isolation VM with AMD SNP needs to access monitor page via + * address space above shared gpa boundary. + */ + if (hv_isolation_type_snp()) { + vmbus_connection.monitor_pages[0] + =3D memremap(vmbus_connection.monitor_pages_pa[0], + HV_HYP_PAGE_SIZE, + MEMREMAP_WB); + if (!vmbus_connection.monitor_pages[0]) { + ret =3D -ENOMEM; + goto cleanup; + } + + vmbus_connection.monitor_pages[1] + =3D memremap(vmbus_connection.monitor_pages_pa[1], + HV_HYP_PAGE_SIZE, + MEMREMAP_WB); + if (!vmbus_connection.monitor_pages[1]) { + ret =3D -ENOMEM; + goto cleanup; + } + } + + /* + * 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); + + } + msginfo =3D kzalloc(sizeof(*msginfo) + sizeof(struct vmbus_channel_initiate_contact), GFP_KERNEL); @@ -303,10 +365,26 @@ 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]); + + set_memory_encrypted((unsigned long) + vmbus_connection.monitor_pages_original[0], + 1); + set_memory_encrypted((unsigned long) + vmbus_connection.monitor_pages_original[1], + 1); + } + + 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..560cba916d1d 100644 --- a/drivers/hv/hyperv_vmbus.h +++ b/drivers/hv/hyperv_vmbus.h @@ -240,6 +240,8 @@ struct vmbus_connection { * is child->parent notification */ struct hv_monitor_page *monitor_pages[2]; + void *monitor_pages_original[2]; + unsigned long monitor_pages_pa[2]; struct list_head chn_msg_list; spinlock_t channelmsg_lock; =20 --=20 2.25.1 From nobody Fri Apr 19 14:35:49 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=1631627293; cv=none; d=zohomail.com; s=zohoarc; b=Szf1bzbkHIoV484jVylKQd7u+cj+T9tzR+h26xYRGpouPq86beRqNNe+SDbNbSzZ12xPvYCVGOWZXZMXztvFxphkPr/4YoI2SxpY7KouW7Wjq8eT/rlgEk5Cy8Pn+1CXuQc7wcRx+R1roxXjouwo8JlHA9zwefe3FzJthInQzLQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1631627293; 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=/usMlQBuF7F2k7aIrpXXc/KIZyi4prmIZLY9hJqrLaI=; b=JgReoMkX7p3+VnmmQ/XfbkNAj6we1ZIXS2yRQmL7qTF6WyDaMhyhhBahY8m23capv1b4S8SeKJikcq8uJwRjTErxXvG7KdhB47AltobzgHiwanzks/GUod23ttj3yppXzTRdJrGxT7CIbX4Q5SPIfFKZnw3gTdKMzSgpw+y6iWY= 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 16316272935611020.7524821358047; Tue, 14 Sep 2021 06:48:13 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.186746.335508 (Exim 4.92) (envelope-from ) id 1mQ8n2-0001Wg-74; Tue, 14 Sep 2021 13:48:00 +0000 Received: by outflank-mailman (output) from mailman id 186746.335508; Tue, 14 Sep 2021 13:48:00 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8n2-0001WV-3o; Tue, 14 Sep 2021 13:48:00 +0000 Received: by outflank-mailman (input) for mailman id 186746; Tue, 14 Sep 2021 13:47: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 1mQ8fq-0001wz-Sx for xen-devel@lists.xenproject.org; Tue, 14 Sep 2021 13:40:34 +0000 Received: from mail-pg1-x52d.google.com (unknown [2607:f8b0:4864:20::52d]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id eb471e53-b4b5-4916-b9e9-d353826a2057; Tue, 14 Sep 2021 13:39:45 +0000 (UTC) Received: by mail-pg1-x52d.google.com with SMTP id w8so12730577pgf.5 for ; Tue, 14 Sep 2021 06:39:45 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:7:6ea2:a529:4af3:5057]) by smtp.gmail.com with ESMTPSA id v13sm10461234pfm.16.2021.09.14.06.39.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:39:44 -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: eb471e53-b4b5-4916-b9e9-d353826a2057 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/usMlQBuF7F2k7aIrpXXc/KIZyi4prmIZLY9hJqrLaI=; b=L7DFk/u0wH9b/iGIHpdByH28JFlOCJvQJXIcQEQHfMZXJGvU9AkaMwEr1HnbxliyPL kfkbVqsa3rXNVuWDdCe7Bc1f248xQkyj17Gq8RLbwe1bkJrwpIjU2pIVT66S/RvRtCJ+ XqFp3mMkYzBghBFtb2kQFehpWcmMo4tserFm8EZKV6vENVhy3H9Gs/VmnaI1kPljNwfI q5UnZA04pfDmnJfxVMpg0DvIeRkONnlOalgt3Cd3iCECHRA2yCzKdEcsr84DqJJ9hiQL 0oWKvFTKQvk4YIQRsjK06WsNDVD9CME0l3HHBXaK/AC0I+rdqCj2QaJqIkNYv2L3xIx2 Iejw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/usMlQBuF7F2k7aIrpXXc/KIZyi4prmIZLY9hJqrLaI=; b=y1zkA6lqIMBmLFQE/smKc5Ed9fGUYgBuo8cWymDP+HU1JQUSMf1Qxm+6dOC7ypzzQS Q51olGoyIJuWVjxEek6WLn/6yp2xmFfV6SRfON62CBMPmeJ9nQjT57LEfAoNRY8pnZLO 6ZOhJKF9KbI+itDGy6t3qug/3MqUQ1f9bezELzy5OZbJcrOnoDCquHT2ZskN0K1ufT0G NtC/DBCghKsnp3p2a6Qxf5Eg5y8kHnIywu5luHJuPV9llcQaVkOOcxC92TyZA5C8bxnD dDV4/d9EuR9EcE65dJQrhum6ePyvyOZtlud5E03zcUkiil7nAsNLxLkP6jaAN0JMF/BH tQAg== X-Gm-Message-State: AOAM532PUJFvHuN1Nfwrt+uCTD8fkxg699vKe/oWHc7OO+PB2HmCnIS5 9wp/Nk/jS/qbipawBDWI1qE= X-Google-Smtp-Source: ABdhPJwtcm6RMZWi5bhD5h9QY6d/jTIRK78GsCcyNNGg4YhuGPJvGaV4TuX2VKOGUZcAcHGS6Xympw== X-Received: by 2002:a62:7985:0:b0:437:36f1:d0df with SMTP id u127-20020a627985000000b0043736f1d0dfmr4833203pfc.52.1631626784499; Tue, 14 Sep 2021 06:39:44 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, 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, will@kernel.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, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, pgonda@google.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, aneesh.kumar@linux.ibm.com, saravanand@fb.com, krish.sadhukhan@oracle.com, xen-devel@lists.xenproject.org, tj@kernel.org, rientjes@google.com, 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 V5 08/12] Drivers: hv : vmbus: Initialize VMbus ring buffer for Isolation VM Date: Tue, 14 Sep 2021 09:39:09 -0400 Message-Id: <20210914133916.1440931-9-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210914133916.1440931-1-ltykernel@gmail.com> References: <20210914133916.1440931-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1631627294576100001 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 v4: * Use PFN_DOWN instead of HVPFN_DOWN in the hv_ringbuffer_init() 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 | 55 ++++++++++++++++++++++++++++++---------- 3 files changed, 53 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 cf419eb1de77..ec847bd14119 100644 --- a/drivers/hv/channel.c +++ b/drivers/hv/channel.c @@ -684,15 +684,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.gpadl_handle =3D 0; =20 @@ -704,6 +695,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..5e014d23a7ad 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,48 @@ 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) + + PFN_DOWN(ms_hyperv.shared_gpa_boundary); + + pfns_wraparound =3D kcalloc(page_cnt * 2 - 1, + sizeof(unsigned long), GFP_KERNEL); + if (!pfns_wraparound) + return -ENOMEM; + + 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 - 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_pfn(pfns_wraparound, page_cnt * 2 - 1, + PAGE_KERNEL); + kfree(pfns_wraparound); =20 - ring_info->ring_buffer =3D (struct hv_ring_buffer *) - vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, PAGE_KERNEL); + if (!ring_info->ring_buffer) + return -ENOMEM; + + /* Zero ring buffer after setting memory host visibility. */ + memset(ring_info->ring_buffer, 0x00, 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]; =20 - kfree(pages_wraparound); + ring_info->ring_buffer =3D (struct hv_ring_buffer *) + vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, + PAGE_KERNEL); =20 + 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 Fri Apr 19 14:35:49 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=1631627288; cv=none; d=zohomail.com; s=zohoarc; b=JqEmFBoTX4oYfJBghI90+7yHO7MspFsNeNTZ69rx/hHgCH7molschYbqGnpVCMPJbr2j/nKs+ik6/3Rlh1226/mw2gVQWAHI9GfcEnTkHtlsW6S866+45dKi3MMLuFpP8sKLsKZJ0R/J2EWju2n4G32pgLY7PlDDd+9EhthLKyE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1631627288; 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=owa2epIC/5mze9MxnGBTWuFjw26S5l9iHD474a0FPZY=; b=k+VFyEkXkbB2mPJHu/7J9ER6xSiQzoAz/mFqB3OCE1jONqnAawnJpQx7Th70Qe8EHV6Tvh3NYelPzAN03DabFF5UHiqEDCA5V9eFxVkn4LPeAuMLEOmoHvXijnFdTZj+FmtavEVp8WNp0KK+V4ksYo30H7hma+vq3vYUIVuHMhg= 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 1631627288420975.3267294855203; Tue, 14 Sep 2021 06:48:08 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.186739.335496 (Exim 4.92) (envelope-from ) id 1mQ8ms-000107-QR; Tue, 14 Sep 2021 13:47:50 +0000 Received: by outflank-mailman (output) from mailman id 186739.335496; Tue, 14 Sep 2021 13:47:50 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8ms-000100-N4; Tue, 14 Sep 2021 13:47:50 +0000 Received: by outflank-mailman (input) for mailman id 186739; Tue, 14 Sep 2021 13:47:49 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8g0-0001wz-TF for xen-devel@lists.xenproject.org; Tue, 14 Sep 2021 13:40:44 +0000 Received: from mail-pg1-x530.google.com (unknown [2607:f8b0:4864:20::530]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 3c7a927e-7bd9-435b-9bc2-5a5258cff843; Tue, 14 Sep 2021 13:39:47 +0000 (UTC) Received: by mail-pg1-x530.google.com with SMTP id k24so12733905pgh.8 for ; Tue, 14 Sep 2021 06:39:47 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:7:6ea2:a529:4af3:5057]) by smtp.gmail.com with ESMTPSA id v13sm10461234pfm.16.2021.09.14.06.39.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:39: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: 3c7a927e-7bd9-435b-9bc2-5a5258cff843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=owa2epIC/5mze9MxnGBTWuFjw26S5l9iHD474a0FPZY=; b=YPR0RRcFGHUBaUpbP9gJUeI5hFz/Ykc2vEDRk21Y+Hx2ak94WCKoxp0mcESjgioo67 zQ0QtdVf0Bv2thg+qZaXcGTuwv8ZB/9ONE9jPeCXqMp2Inclm/3Hhux53T4BLmW7uz90 j7APkz+ekg0qxBWYqZuPr9Uy9XzMQeYfkCqJqS/Wzvh1ec7KuAQZ7pi6wk63IJlMzJ4i 2oz+AnJOtCDleuDhptkGHliv7R41SodRqvAJ7f3Fb1eGVT1WzZv2aCriGL1Fh2H9+Gg/ t9SRIkFCG/H7bZf6edW4ThgnW3aT2vnhMToatGUVRBJ7o+6iRnTqJSTDYkSghGECv/mb un1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=owa2epIC/5mze9MxnGBTWuFjw26S5l9iHD474a0FPZY=; b=4FC8UNG3+65FAT1OnlVRHpXgHNLx5GlbmOU0EZi0WGAZl4D6AiF0l4C9Jn45o2d1uI /x6DbLytVX8nzlFYzKZHLqUwl8OkZS5yccBFhYsgK+TPy11zFfihXdXHR31OW92RUDcv duo7qEZq465jYSd6F1E+3TWO+nRabTrqdlXM9XQtNQSu7QxEprSABRkNr7XKzyqrzyfr 8/DhdbH7jk3sHaS2m79qAsbjpk1YOaq2b1+1QMnn9GX+PMUbSTeVde42nJ6Ubu5oYuV7 ptaDj4gD4KvsmYAp17q9hkBoOZ7+tVgkNaV7h9v0nuOMRufs9XBHgKRq/c1Vtavt4MaO 6fag== X-Gm-Message-State: AOAM533HV5SV1M4SoxD9dW9KOQE3Rz/uiCZfJjmF53ujtHktdma1xGdN Ia3Cr88++E92YcS8CV60axY= X-Google-Smtp-Source: ABdhPJxXTtvlP911RHquYd4PWMjAFBqIUg4/wztNKPqgo3aV/dGiro8zblLWz9doF4YohiyUAH/bGw== X-Received: by 2002:a05:6a00:2449:b0:43c:4a5e:55a6 with SMTP id d9-20020a056a00244900b0043c4a5e55a6mr4918302pfj.43.1631626786861; Tue, 14 Sep 2021 06:39:46 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, 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, will@kernel.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, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, pgonda@google.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, aneesh.kumar@linux.ibm.com, saravanand@fb.com, krish.sadhukhan@oracle.com, xen-devel@lists.xenproject.org, tj@kernel.org, rientjes@google.com, 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 V5 09/12] x86/Swiotlb: Add Swiotlb bounce buffer remap function for HV IVM Date: Tue, 14 Sep 2021 09:39:10 -0400 Message-Id: <20210914133916.1440931-10-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210914133916.1440931-1-ltykernel@gmail.com> References: <20210914133916.1440931-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1631627290228100001 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. Expose swiotlb_unencrypted_base for platforms to set unencrypted memory base offset and call memremap() to map bounce buffer in the swiotlb code, store map address and use the address to copy data from/to swiotlb bounce buffer. Signed-off-by: Tianyu Lan --- Change since v4: * Expose swiotlb_unencrypted_base to set unencrypted memory offset. * Use memremap() to map bounce buffer if swiotlb_unencrypted_ base is set. Change since v1: * Make swiotlb_init_io_tlb_mem() return error code and return error when dma_map_decrypted() fails. --- include/linux/swiotlb.h | 6 ++++++ kernel/dma/swiotlb.c | 41 +++++++++++++++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index b0cb2a9973f4..4998ed44ae3d 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. @@ -91,6 +94,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; @@ -185,4 +189,6 @@ static inline bool is_swiotlb_for_alloc(struct device *= dev) } #endif /* CONFIG_DMA_RESTRICTED_POOL */ =20 +extern phys_addr_t swiotlb_unencrypted_base; + #endif /* __LINUX_SWIOTLB_H */ diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 87c40517e822..9e30cc4bd872 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -50,6 +50,7 @@ #include #include =20 +#include #include #include #include @@ -72,6 +73,8 @@ enum swiotlb_force swiotlb_force; =20 struct io_tlb_mem io_tlb_default_mem; =20 +phys_addr_t swiotlb_unencrypted_base; + /* * Max segment that we can provide which (if pages are contingous) will * not be bounced (unless SWIOTLB_FORCE is set). @@ -175,7 +178,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); @@ -196,13 +199,34 @@ static void swiotlb_init_io_tlb_mem(struct io_tlb_mem= *mem, phys_addr_t start, mem->slots[i].orig_addr =3D INVALID_PHYS_ADDR; mem->slots[i].alloc_size =3D 0; } + + if (set_memory_decrypted((unsigned long)vaddr, bytes >> PAGE_SHIFT)) + return -EFAULT; + + /* + * Map memory in the unencrypted physical address space when requested + * (e.g. for Hyper-V AMD SEV-SNP Isolation VMs). + */ + if (swiotlb_unencrypted_base) { + phys_addr_t paddr =3D __pa(vaddr) + swiotlb_unencrypted_base; + + vaddr =3D memremap(paddr, bytes, MEMREMAP_WB); + if (!vaddr) { + pr_err("Failed to map the unencrypted memory.\n"); + return -ENOMEM; + } + } + memset(vaddr, 0, bytes); + mem->vaddr =3D vaddr; + return 0; } =20 int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verb= ose) { struct io_tlb_mem *mem =3D &io_tlb_default_mem; size_t alloc_size; + int ret; =20 if (swiotlb_force =3D=3D SWIOTLB_NO_FORCE) return 0; @@ -217,7 +241,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 if (verbose) swiotlb_print_info(); @@ -304,7 +332,7 @@ int swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs) { struct io_tlb_mem *mem =3D &io_tlb_default_mem; - unsigned long bytes =3D nslabs << IO_TLB_SHIFT; + int ret; =20 if (swiotlb_force =3D=3D SWIOTLB_NO_FORCE) return 0; @@ -318,8 +346,9 @@ swiotlb_late_init_with_tbl(char *tlb, unsigned long nsl= abs) if (!mem->slots) return -ENOMEM; =20 - set_memory_decrypted((unsigned long)tlb, bytes >> PAGE_SHIFT); - 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) + return ret; =20 swiotlb_print_info(); swiotlb_set_max_segment(mem->nslabs << IO_TLB_SHIFT); @@ -371,7 +400,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, orig_addr_offset; =20 if (orig_addr =3D=3D INVALID_PHYS_ADDR) --=20 2.25.1 From nobody Fri Apr 19 14:35:49 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=1631627249; cv=none; d=zohomail.com; s=zohoarc; b=adYMsM/WHb5MzpmJz9vI2Go90HySL61pZFC873s8SVFOJZ8veT9kZIMw4ai1YH2EgKKwwjxnyfVpd2POVuJ+3wHBKKGCYejgbl4Vw+9WHBY6e9VazQzi/AMkgxBlgNqGPl7WAzTlWRPBF2jjxGC8GTbvMjhCfiYGLna8EEDRahg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1631627249; 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=ChpyrJrtZ0GWASO9SJSkVtTi3NWXtvZFVFa0kxYT1A8=; b=hycAPOmWooAaqM+8uXbvmEf6nKW1TCSgENnevMzH0tNO6IT07U2/1OByMIiRNyukiFUsVPAZQKeO+Sl9gYQTE2iRp148KHgSk0GdMcFtGXYcJO3JytOjJb2NAs15K6beuh3O4XIcrMlqkacN7AldQUwEx2Epdg7+oxkbptDd3Jw= 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 1631627249205462.4260428998076; Tue, 14 Sep 2021 06:47:29 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.186699.335469 (Exim 4.92) (envelope-from ) id 1mQ8mH-0007ct-5n; Tue, 14 Sep 2021 13:47:13 +0000 Received: by outflank-mailman (output) from mailman id 186699.335469; Tue, 14 Sep 2021 13:47:13 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8mH-0007bW-0z; Tue, 14 Sep 2021 13:47:13 +0000 Received: by outflank-mailman (input) for mailman id 186699; Tue, 14 Sep 2021 13:47:11 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8gA-0001wz-TU for xen-devel@lists.xenproject.org; Tue, 14 Sep 2021 13:40:54 +0000 Received: from mail-pf1-x436.google.com (unknown [2607:f8b0:4864:20::436]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 12e9ab66-ee91-44c8-8903-43d60f23f701; Tue, 14 Sep 2021 13:39:50 +0000 (UTC) Received: by mail-pf1-x436.google.com with SMTP id m26so12258771pff.3 for ; Tue, 14 Sep 2021 06:39:50 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:7:6ea2:a529:4af3:5057]) by smtp.gmail.com with ESMTPSA id v13sm10461234pfm.16.2021.09.14.06.39.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:39: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: 12e9ab66-ee91-44c8-8903-43d60f23f701 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ChpyrJrtZ0GWASO9SJSkVtTi3NWXtvZFVFa0kxYT1A8=; b=HE+Mpp7NrG39OPwhSHFYcEZgDEVLplripW8SNkjWBluPNhIPHnO4Zc3m25/+SMv9ox teu8xjn6xSwxQBB952yLTWC0vcJbuUUEVkiGOR8FS9DSekELD6QzR1hT2fU+xQdK7Uul Cj41tmdrUxQIfllJ/7zBfNfb/Gindx6VmnHKCORnCWA39fPeFGYGFF7S6OD3J97lxNxs DVoG0FIayXg0fvP17onaBJXLG3VHM82NewgI22FMQXLITV20NYgiHQLQz5oTFXLXtY7G 0u7GQsCFLuYkCnMhkeKP27UJxUm0AOfwxhp2ZeUejXbuePKmYRrXRlyyAbznfW2hDEGT 74nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ChpyrJrtZ0GWASO9SJSkVtTi3NWXtvZFVFa0kxYT1A8=; b=XyQ0aBjdmSoN5L2JpD66Z+aUTTAs4PCkCIH8SgQeui8Ue+ACxvyrpewxBek1c2SxMl rmDUIPiO0EAuK6mmsbvHjTVt5HuVy9bFON12F95HGdOwE01YqQAXJqcn8n9fzELGhsWY xq1xH36QkraxN4QUtEgX9QQaZs/dBJhJdo0xjxkLTuPUxAeolcD7MW+J1RZXi5bqEfa7 +GYpUMRtH5XI9pcdEidGP1RsOC8lisn7vBnTFDqz1dIz69ju6zDkiepfS3hFi0EDbXgr nrSac8JHM0hfuDL5CcWUUlQyLnoBCaFZ0rlsHyQY+/Bi18unufQzDTqHRUfZLT/c5LFv RPkA== X-Gm-Message-State: AOAM5338npscI/bM8nlncQV/1dg2oA5ClG7JvEZIdpiPwxcC8C1NwvWZ +0l4yeXHt7rMRquGY+ctd2c= X-Google-Smtp-Source: ABdhPJz7GLWCu0uNZSZf3h3ggTXiZShd7wRNgOpywDn3xOoYCN0POtH6uJq1e060FQC3kbngt9TJWQ== X-Received: by 2002:a62:b414:0:b029:32e:3ef0:7735 with SMTP id h20-20020a62b4140000b029032e3ef07735mr4848036pfn.61.1631626789107; Tue, 14 Sep 2021 06:39:49 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, 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, will@kernel.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, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, pgonda@google.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, aneesh.kumar@linux.ibm.com, saravanand@fb.com, krish.sadhukhan@oracle.com, xen-devel@lists.xenproject.org, tj@kernel.org, rientjes@google.com, 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 V5 10/12] hyperv/IOMMU: Enable swiotlb bounce buffer for Isolation VM Date: Tue, 14 Sep 2021 09:39:11 -0400 Message-Id: <20210914133916.1440931-11-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210914133916.1440931-1-ltykernel@gmail.com> References: <20210914133916.1440931-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1631627250086100001 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. 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. Swiotlb bounce buffer code calls set_memory_decrypted() to mark bounce buffer visible to host and map it in extra address space via memremap. Populate the shared_gpa_boundary (vTOM) via swiotlb_unencrypted_base variable. The map function memremap() 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 v4: * Use swiotlb_unencrypted_base variable to pass shared_gpa_ boundary and map bounce buffer inside swiotlb code. 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/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 | 60 +++++++++++++++++++++++++++++++++ include/linux/hyperv.h | 1 + 6 files changed, 70 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyper= v.h index 165423e8b67a..2d22f29f90c9 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -182,6 +182,8 @@ int hv_map_ioapic_interrupt(int ioapic_id, bool level, = int vcpu, int vector, struct hv_interrupt_entry *entry); int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *en= try); 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_ghcb_msr_write(u64 msr, u64 value); void hv_ghcb_msr_read(u64 msr, u64 *value); #else /* CONFIG_HYPERV */ 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 392c1ac4f819..b0be287e9a32 100644 --- a/drivers/hv/vmbus_drv.c +++ b/drivers/hv/vmbus_drv.c @@ -23,6 +23,7 @@ #include #include =20 +#include #include #include #include @@ -2078,6 +2079,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 */ @@ -2118,6 +2120,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..a8ac2239de0f 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,53 @@ static const struct irq_domain_ops hyperv_root_ir_doma= in_ops =3D { .free =3D hyperv_root_irq_remapping_free, }; =20 +static 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, 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)) + return 0; + + if (!hv_is_isolation_supported()) + return 0; + + /* + * Enable swiotlb force mode in Isolation VM to + * use swiotlb bounce buffer for dma transaction. + */ + swiotlb_unencrypted_base =3D ms_hyperv.shared_gpa_boundary; + swiotlb_force =3D SWIOTLB_FORCE; + return 1; +} + +static 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 a9e0bc3b1511..bb1a1519b93a 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 Fri Apr 19 14:35:49 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=1631627270; cv=none; d=zohomail.com; s=zohoarc; b=Ora83KiP/4WuCTPJ9cSggLGrlpip/M4wyW91IOj63+Liy54CHVcG5JW1WvvYF5ZlSsBiTiLtxm6PnErpwJVzw8hTgt0dHhveesHsKoPQ8PZyW3mpk8WqyDxfFQATShmqjHHmrosRHZwDJmixHpPpNWT9VJCZrxiEtMXpHmHiA5U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1631627270; 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=V6GY3z4JvJviWkSRCk5kJjmxao2kOnqCUtugNfHg/pw=; b=fawp7Og4PxEVCeyZRRbVuqLOn+PgN85IaoTiVO1hz9DlUPzsd//ni/kyx8gXfLRjzF6a+f70vxyZYz112WWf5fUA6sHi09TbXNNT1HufanmLrd83s91V5el8Y+lIsoju9syzBp4h4OvyPXdi7g/WlL5dXl8XF1oMcaGqOAuDN/0= 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 1631627270882937.1254024586951; Tue, 14 Sep 2021 06:47:50 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.186731.335486 (Exim 4.92) (envelope-from ) id 1mQ8mg-0000We-He; Tue, 14 Sep 2021 13:47:38 +0000 Received: by outflank-mailman (output) from mailman id 186731.335486; Tue, 14 Sep 2021 13:47:38 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8mg-0000WT-El; Tue, 14 Sep 2021 13:47:38 +0000 Received: by outflank-mailman (input) for mailman id 186731; Tue, 14 Sep 2021 13:47: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 1mQ8gF-0001wz-Tk for xen-devel@lists.xenproject.org; Tue, 14 Sep 2021 13:40:59 +0000 Received: from mail-pg1-x535.google.com (unknown [2607:f8b0:4864:20::535]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 45bdebf3-7175-4c1a-a337-030cfe2d2541; Tue, 14 Sep 2021 13:39:52 +0000 (UTC) Received: by mail-pg1-x535.google.com with SMTP id s11so12736189pgr.11 for ; Tue, 14 Sep 2021 06:39:52 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:7:6ea2:a529:4af3:5057]) by smtp.gmail.com with ESMTPSA id v13sm10461234pfm.16.2021.09.14.06.39.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:39: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: 45bdebf3-7175-4c1a-a337-030cfe2d2541 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=V6GY3z4JvJviWkSRCk5kJjmxao2kOnqCUtugNfHg/pw=; b=dDHEgjjhONvetT5YclBkax+X6EY3U8rrUfLy/aimO6iFEaJzZte34OHy3Gmk7JZfPE Pggybi9YLY9iIVSeqJqx7clgxMGVMEP8TtLaHzOW6LPUCpK91pv50TaSb2AEO9aMpLBd 5vNa0hpfM90xmm+vOpPn4FaKRXi5urhJjU86eJpvLASlL+/dKBZuE22QOsg1W6uzTiyo kH5MFhd5SbmeVWhmueq7+NQylhNDj3Eh9b63XJbTIpiudQB42/28UOhDAfQf8/ph6yE7 dcIqRYrSNXK3RNbQ3Xe+HFePVpn7E6KSoYLVPniJoegZD+jyC822quIHXse/2XucEYnb Llbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=V6GY3z4JvJviWkSRCk5kJjmxao2kOnqCUtugNfHg/pw=; b=NVzID8JQEFGwD5FdpeHVWi2FUyz7Iah69wvu0QCO9AFr+ZwdaiK7E0O5rrWqL7XiyT l0uLhpRGQkWZaFkl+lwpIcz8z3/TbHYKvpiJS2UEVIepz0ilFw4vasLwaad9D1wM8yYj 5CPY+ogvzHIRMLaPAqAcC1TaobdKZ6UEC325soQeG9/G42V3AitMZbsoWnOMHTl0HnRZ vKGJBaFgARZirGbBywrNmEnJIFMu7rInPJDX4xgoMoMjzPSrnmCb1m/HBh714iKxzCm5 R5v02F8+9I+LF9tkc75MNcx+NB1kArULmf84h3l8w7v75ZeYhoAILlmYj44VkTs86ZI/ uvjw== X-Gm-Message-State: AOAM530zkzTiBG3CyZBi86NhCG8bN4lc4ZWIUUbhk1jogL7aFn1YEhBy kwy/lHay1kEQzv5jjh/pRqs= X-Google-Smtp-Source: ABdhPJyd7B/cycREjmjwvYi/L8C41okdW25JkBh0jV2R9467KDZelN884wrT/mvAlGHMBU2oT9UWmg== X-Received: by 2002:a63:ec06:: with SMTP id j6mr15654053pgh.259.1631626791699; Tue, 14 Sep 2021 06:39:51 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, 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, will@kernel.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, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, pgonda@google.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, aneesh.kumar@linux.ibm.com, saravanand@fb.com, krish.sadhukhan@oracle.com, xen-devel@lists.xenproject.org, tj@kernel.org, rientjes@google.com, 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 V5 11/12] scsi: storvsc: Add Isolation VM support for storvsc driver Date: Tue, 14 Sep 2021 09:39:12 -0400 Message-Id: <20210914133916.1440931-12-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210914133916.1440931-1-ltykernel@gmail.com> References: <20210914133916.1440931-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1631627271796100001 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(scsi_dma_map/unmap) 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 v4: * use scsi_dma_map/unmap() instead of dma_map/unmap_sg() * Add deleted comments back. * Fix error calculation of hvpnfs_to_add 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 | 24 +++++++++++++++--------- include/linux/hyperv.h | 1 + 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c index b0be287e9a32..9c53f823cde1 100644 --- a/drivers/hv/vmbus_drv.c +++ b/drivers/hv/vmbus_drv.c @@ -2121,6 +2121,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 ebbbc1299c62..d10b450bcf0c 100644 --- a/drivers/scsi/storvsc_drv.c +++ b/drivers/scsi/storvsc_drv.c @@ -21,6 +21,8 @@ #include #include #include +#include + #include #include #include @@ -1322,6 +1324,7 @@ static void storvsc_on_channel_callback(void *context) continue; } request =3D (struct storvsc_cmd_request *)scsi_cmd_priv(scmnd); + scsi_dma_unmap(scmnd); } =20 storvsc_on_receive(stor_device, packet, request); @@ -1735,7 +1738,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; @@ -1817,10 +1819,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 @@ -1834,8 +1837,11 @@ static int storvsc_queuecommand(struct Scsi_Host *ho= st, struct scsi_cmnd *scmnd) payload->range.len =3D length; payload->range.offset =3D offset_in_hvpg; =20 + sg_count =3D scsi_dma_map(scmnd); + if (sg_count < 0) + return SCSI_MLQUEUE_DEVICE_BUSY; =20 - for (i =3D 0; sgl !=3D NULL; sgl =3D sg_next(sgl)) { + for_each_sg(sgl, sg, sg_count, j) { /* * Init values for the current sgl entry. hvpgoff * and hvpfns_to_add are in units of Hyper-V size @@ -1845,10 +1851,9 @@ static int storvsc_queuecommand(struct Scsi_Host *ho= st, struct scsi_cmnd *scmnd) * 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; + hvpfn =3D HVPFN_DOWN(sg_dma_address(sg)); + hvpfns_to_add =3D HVPFN_UP(sg_dma_address(sg) + + sg_dma_len(sg)) - hvpfn; =20 /* * Fill the next portion of the PFN array with @@ -1858,7 +1863,7 @@ static int storvsc_queuecommand(struct Scsi_Host *hos= t, struct scsi_cmnd *scmnd) * the PFN array is filled. */ while (hvpfns_to_add--) - payload->range.pfn_array[i++] =3D hvpfn++; + payload->range.pfn_array[i++] =3D hvpfn++; } } =20 @@ -2002,6 +2007,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 bb1a1519b93a..c94c534a944e 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 From nobody Fri Apr 19 14:35:49 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=1631627248; cv=none; d=zohomail.com; s=zohoarc; b=fIfqPy/YGctQ0VdRbMNlI9o9+tOgSj8xmh3jGaBZpQVskYktw36BG7FA+v4y1oFNdQ36pNxisPSk/EzxdJmhboF4rcfXryHGihVBmIEaud7YkG9x5saSnlcyFoEaAeUMHlpxYA2uddPkH4HMpDIZBFl9y08/YOO+i/KvmxFaz+Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1631627248; 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=LkmXj6KxjUvBaz8z0a8dNOFjY1uabCCopAH/jkVX7K0=; b=JAbZYfuu4d7vJUvNG7l104zrt5K3TwMojGSViJFMHn/CU17lKHuyfM547S1H+vLMqo8Y+Ao0LPKnWCY/E01uq+Ie+R0kb6wIKIqsowLf97Ibwwheli6u0nNk8sMGsmpAfU28N8ezMv/5Oqe5Q8koaWxW3Se5nTlA/I2Fe1/I3RI= 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 1631627248346227.925519882289; Tue, 14 Sep 2021 06:47:28 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.186698.335464 (Exim 4.92) (envelope-from ) id 1mQ8mG-0007Yw-RX; Tue, 14 Sep 2021 13:47:12 +0000 Received: by outflank-mailman (output) from mailman id 186698.335464; Tue, 14 Sep 2021 13:47:12 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mQ8mG-0007YE-OE; Tue, 14 Sep 2021 13:47:12 +0000 Received: by outflank-mailman (input) for mailman id 186698; Tue, 14 Sep 2021 13:47: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 1mQ8gP-0001wz-Tz for xen-devel@lists.xenproject.org; Tue, 14 Sep 2021 13:41:09 +0000 Received: from mail-pl1-x630.google.com (unknown [2607:f8b0:4864:20::630]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id e1b20c67-e05b-4253-910b-5c3d6b9844ff; Tue, 14 Sep 2021 13:39:54 +0000 (UTC) Received: by mail-pl1-x630.google.com with SMTP id n18so8199569plp.7 for ; Tue, 14 Sep 2021 06:39:54 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:7:6ea2:a529:4af3:5057]) by smtp.gmail.com with ESMTPSA id v13sm10461234pfm.16.2021.09.14.06.39.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:39:53 -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: e1b20c67-e05b-4253-910b-5c3d6b9844ff DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LkmXj6KxjUvBaz8z0a8dNOFjY1uabCCopAH/jkVX7K0=; b=H0oAe82GjXdTdE+tPELa3975j1MGt/XfBwybJVWm9rXqcL3sZBlP4N3dVxqejuMmCR fTxH2exCC64AO/wTOZvdNd7kolrZ/irkjYbpTtFRFS52HpEJiYpKx7tvDB22KmzcCOv7 c9DS1KJW86b06AqUV3XvHCJsPD24m0nR2St4Gm7JJGLUJisEPWa/XBbIzSmSmqfr13m7 8k2iUw5lq4EvEwGop+B0nMUmtEgs3N2LwGcdbL1UtdAgaL9kn7ZyWDVSGWcSLQCnunTP f35mOoI/LXthKpV4WOGUGrXrLzkkUj0AyvgnRnOwf8rvVBG3C6mESM16QWBrymuP71V7 1ddg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=LkmXj6KxjUvBaz8z0a8dNOFjY1uabCCopAH/jkVX7K0=; b=l1cv9P85CrKZMgyqGgPGyY2W8XxgU25LdyxOpOvSA9KjsxoAyAMOX+eycZ1NOpGhz1 f4ZXdJTqvq7VDTIo+Jh2k6zObUYMwZd9CAsVncwVy62rZne8UsgE6s+1TiFA7JWV4dh3 HDAZlT17ymCSi6dJSnGjH4+8K6G3SrwNsTOXtE5Y652Fct08jhbwXMOGESCBCthx+Eu5 nZDbUYrnpIPJuZWhWditkTf4XeCCxY0f9BjnyvpQwua2e0qerZOTovyF22fBWm6H3CIt nqm8SiLe4kBb23mhrhuwGiNYoumKF0JHzdud+0u3Qk1vjOpeVkPDdfHvlGO5Bo1aqkm3 VGDA== X-Gm-Message-State: AOAM530fYqm9o5JpJnHB042fGb6LgulULOAgkMxNj1tD8cGOpEvDSjH9 x86P9Y1l/I7HwAPGNIS+elI= X-Google-Smtp-Source: ABdhPJzd50JbAXqZtpTD7xpue98Virh8fDhY7VdwfyRuohNOtMS2fLTQ6Ka6cqdkhWO9Ql38QPYNrQ== X-Received: by 2002:a17:902:da89:b0:13b:7d3d:59e9 with SMTP id j9-20020a170902da8900b0013b7d3d59e9mr14413893plx.41.1631626794081; Tue, 14 Sep 2021 06:39:54 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, 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, will@kernel.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, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, pgonda@google.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, aneesh.kumar@linux.ibm.com, saravanand@fb.com, krish.sadhukhan@oracle.com, xen-devel@lists.xenproject.org, tj@kernel.org, rientjes@google.com, 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 V5 12/12] net: netvsc: Add Isolation VM support for netvsc driver Date: Tue, 14 Sep 2021 09:39:13 -0400 Message-Id: <20210914133916.1440931-13-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210914133916.1440931-1-ltykernel@gmail.com> References: <20210914133916.1440931-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1631627250104100002 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 address will be returned. The swiotlb bounce buffer has been masked to be visible to host during boot up. Allocate rx/tx ring buffer via alloc_pages() in Isolation VM and map these pages via vmap(). After calling vmbus_establish_gpadl() which marks these pages visible to host, unmap these pages to release the virtual address mapped with physical address below shared_gpa_boundary and map them in the extra address space via vmap_pfn(). Signed-off-by: Tianyu Lan Reviewed-by: Haiyang Zhang --- Change since v4: * Allocate rx/tx ring buffer via alloc_pages() in Isolation VM * Map pages after calling vmbus_establish_gpadl(). * set dma_set_min_align_mask for netvsc driver. Change since v3: * Add comment to explain why not to use dma_map_sg() * Fix some error handle. --- drivers/net/hyperv/hyperv_net.h | 7 + drivers/net/hyperv/netvsc.c | 287 +++++++++++++++++++++++++++++- drivers/net/hyperv/netvsc_drv.c | 1 + drivers/net/hyperv/rndis_filter.c | 2 + include/linux/hyperv.h | 5 + 5 files changed, 296 insertions(+), 6 deletions(-) diff --git a/drivers/net/hyperv/hyperv_net.h b/drivers/net/hyperv/hyperv_ne= t.h index 315278a7cf88..87e8c74398a5 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,8 @@ struct netvsc_device { =20 /* Receive buffer allocated by us but manages by NetVSP */ void *recv_buf; + struct page **recv_pages; + u32 recv_page_count; u32 recv_buf_size; /* allocated bytes */ struct vmbus_gpadl recv_buf_gpadl_handle; u32 recv_section_cnt; @@ -1082,6 +1085,8 @@ struct netvsc_device { =20 /* Send buffer allocated by us */ void *send_buf; + struct page **send_pages; + u32 send_page_count; u32 send_buf_size; struct vmbus_gpadl send_buf_gpadl_handle; u32 send_section_cnt; @@ -1731,4 +1736,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 1f87e570ed2b..7d5254bf043e 100644 --- a/drivers/net/hyperv/netvsc.c +++ b/drivers/net/hyperv/netvsc.c @@ -20,6 +20,7 @@ #include #include #include +#include =20 #include #include @@ -150,11 +151,33 @@ static void free_netvsc_device(struct rcu_head *head) { struct netvsc_device *nvdev =3D container_of(head, struct netvsc_device, rcu); + unsigned int alloc_unit; int i; =20 kfree(nvdev->extension); - vfree(nvdev->recv_buf); - vfree(nvdev->send_buf); + + if (nvdev->recv_pages) { + alloc_unit =3D (nvdev->recv_buf_size / + nvdev->recv_page_count) >> PAGE_SHIFT; + + vunmap(nvdev->recv_buf); + for (i =3D 0; i < nvdev->recv_page_count; i++) + __free_pages(nvdev->recv_pages[i], alloc_unit); + } else { + vfree(nvdev->recv_buf); + } + + if (nvdev->send_pages) { + alloc_unit =3D (nvdev->send_buf_size / + nvdev->send_page_count) >> PAGE_SHIFT; + + vunmap(nvdev->send_buf); + for (i =3D 0; i < nvdev->send_page_count; i++) + __free_pages(nvdev->send_pages[i], alloc_unit); + } else { + vfree(nvdev->send_buf); + } + kfree(nvdev->send_section_map); =20 for (i =3D 0; i < VRSS_CHANNEL_MAX; i++) { @@ -330,6 +353,108 @@ int netvsc_alloc_recv_comp_ring(struct netvsc_device = *net_device, u32 q_idx) return nvchan->mrc.slots ? 0 : -ENOMEM; } =20 +void *netvsc_alloc_pages(struct page ***pages_array, unsigned int *array_l= en, + unsigned long size) +{ + struct page *page, **pages, **vmap_pages; + unsigned long pg_count =3D size >> PAGE_SHIFT; + int alloc_unit =3D MAX_ORDER_NR_PAGES; + int i, j, vmap_page_index =3D 0; + void *vaddr; + + if (pg_count < alloc_unit) + alloc_unit =3D 1; + + /* vmap() accepts page array with PAGE_SIZE as unit while try to + * allocate high order pages here in order to save page array space. + * vmap_pages[] is used as input parameter of vmap(). pages[] is to + * store allocated pages and map them later. + */ + vmap_pages =3D kmalloc_array(pg_count, sizeof(*vmap_pages), GFP_KERNEL); + if (!vmap_pages) + return NULL; + +retry: + *array_len =3D pg_count / alloc_unit; + pages =3D kmalloc_array(*array_len, sizeof(*pages), GFP_KERNEL); + if (!pages) + goto cleanup; + + for (i =3D 0; i < *array_len; i++) { + page =3D alloc_pages(GFP_KERNEL | __GFP_ZERO, + get_order(alloc_unit << PAGE_SHIFT)); + if (!page) { + /* Try allocating small pages if high order pages are not available. */ + if (alloc_unit =3D=3D 1) { + goto cleanup; + } else { + memset(vmap_pages, 0, + sizeof(*vmap_pages) * vmap_page_index); + vmap_page_index =3D 0; + + for (j =3D 0; j < i; j++) + __free_pages(pages[j], alloc_unit); + + kfree(pages); + alloc_unit =3D 1; + goto retry; + } + } + + pages[i] =3D page; + for (j =3D 0; j < alloc_unit; j++) + vmap_pages[vmap_page_index++] =3D page++; + } + + vaddr =3D vmap(vmap_pages, vmap_page_index, VM_MAP, PAGE_KERNEL); + kfree(vmap_pages); + + *pages_array =3D pages; + return vaddr; + +cleanup: + for (j =3D 0; j < i; j++) + __free_pages(pages[i], alloc_unit); + + kfree(pages); + kfree(vmap_pages); + return NULL; +} + +static void *netvsc_map_pages(struct page **pages, int count, int alloc_un= it) +{ + int pg_count =3D count * alloc_unit; + struct page *page; + unsigned long *pfns; + int pfn_index =3D 0; + void *vaddr; + int i, j; + + if (!pages) + return NULL; + + pfns =3D kcalloc(pg_count, sizeof(*pfns), GFP_KERNEL); + if (!pfns) + return NULL; + + for (i =3D 0; i < count; i++) { + page =3D pages[i]; + if (!page) { + pr_warn("page is not available %d.\n", i); + return NULL; + } + + for (j =3D 0; j < alloc_unit; j++) { + pfns[pfn_index++] =3D page_to_pfn(page++) + + (ms_hyperv.shared_gpa_boundary >> PAGE_SHIFT); + } + } + + vaddr =3D vmap_pfn(pfns, pg_count, PAGE_KERNEL_IO); + kfree(pfns); + return vaddr; +} + static int netvsc_init_buf(struct hv_device *device, struct netvsc_device *net_device, const struct netvsc_device_info *device_info) @@ -337,7 +462,7 @@ static int netvsc_init_buf(struct hv_device *device, struct nvsp_1_message_send_receive_buffer_complete *resp; struct net_device *ndev =3D hv_get_drvdata(device); struct nvsp_message *init_packet; - unsigned int buf_size; + unsigned int buf_size, alloc_unit; size_t map_words; int i, ret =3D 0; =20 @@ -350,7 +475,14 @@ static int netvsc_init_buf(struct hv_device *device, buf_size =3D min_t(unsigned int, buf_size, NETVSC_RECEIVE_BUFFER_SIZE_LEGACY); =20 - net_device->recv_buf =3D vzalloc(buf_size); + if (hv_isolation_type_snp()) + net_device->recv_buf =3D + netvsc_alloc_pages(&net_device->recv_pages, + &net_device->recv_page_count, + buf_size); + else + net_device->recv_buf =3D vzalloc(buf_size); + if (!net_device->recv_buf) { netdev_err(ndev, "unable to allocate receive buffer of size %u\n", @@ -375,6 +507,27 @@ static int netvsc_init_buf(struct hv_device *device, goto cleanup; } =20 + if (hv_isolation_type_snp()) { + alloc_unit =3D (buf_size / net_device->recv_page_count) + >> PAGE_SHIFT; + + /* Unmap previous virtual address and map pages in the extra + * address space(above shared gpa boundary) in Isolation VM. + */ + vunmap(net_device->recv_buf); + net_device->recv_buf =3D + netvsc_map_pages(net_device->recv_pages, + net_device->recv_page_count, + alloc_unit); + if (!net_device->recv_buf) { + netdev_err(ndev, + "unable to allocate receive buffer of size %u\n", + buf_size); + ret =3D -ENOMEM; + goto cleanup; + } + } + /* Notify the NetVsp of the gpadl handle */ init_packet =3D &net_device->channel_init_pkt; memset(init_packet, 0, sizeof(struct nvsp_message)); @@ -456,13 +609,21 @@ static int netvsc_init_buf(struct hv_device *device, buf_size =3D device_info->send_sections * device_info->send_section_size; buf_size =3D round_up(buf_size, PAGE_SIZE); =20 - net_device->send_buf =3D vzalloc(buf_size); + if (hv_isolation_type_snp()) + net_device->send_buf =3D + netvsc_alloc_pages(&net_device->send_pages, + &net_device->send_page_count, + buf_size); + else + net_device->send_buf =3D vzalloc(buf_size); + if (!net_device->send_buf) { netdev_err(ndev, "unable to allocate send buffer of size %u\n", buf_size); ret =3D -ENOMEM; goto cleanup; } + net_device->send_buf_size =3D buf_size; =20 /* Establish the gpadl handle for this buffer on this @@ -478,6 +639,27 @@ static int netvsc_init_buf(struct hv_device *device, goto cleanup; } =20 + if (hv_isolation_type_snp()) { + alloc_unit =3D (buf_size / net_device->send_page_count) + >> PAGE_SHIFT; + + /* Unmap previous virtual address and map pages in the extra + * address space(above shared gpa boundary) in Isolation VM. + */ + vunmap(net_device->send_buf); + net_device->send_buf =3D + netvsc_map_pages(net_device->send_pages, + net_device->send_page_count, + alloc_unit); + if (!net_device->send_buf) { + netdev_err(ndev, + "unable to allocate receive buffer of size %u\n", + buf_size); + ret =3D -ENOMEM; + goto cleanup; + } + } + /* Notify the NetVsp of the gpadl handle */ init_packet =3D &net_device->channel_init_pkt; memset(init_packet, 0, sizeof(struct nvsp_message)); @@ -768,7 +950,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; @@ -784,6 +966,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 @@ -948,6 +1131,87 @@ static void netvsc_copy_to_send_buf(struct netvsc_dev= ice *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 because 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. + */ +static 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, @@ -988,14 +1252,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; + } + 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), @@ -1003,6 +1277,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/netvsc_drv.c b/drivers/net/hyperv/netvsc_dr= v.c index 382bebc2420d..c3dc884b31e3 100644 --- a/drivers/net/hyperv/netvsc_drv.c +++ b/drivers/net/hyperv/netvsc_drv.c @@ -2577,6 +2577,7 @@ static int netvsc_probe(struct hv_device *dev, list_add(&net_device_ctx->list, &netvsc_dev_list); rtnl_unlock(); =20 + dma_set_min_align_mask(&dev->device, HV_HYP_PAGE_SIZE - 1); netvsc_devinfo_put(device_info); return 0; =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 c94c534a944e..81e58dd582dc 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -1597,6 +1597,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