From nobody Sun Apr 28 05:50:41 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=1628531807; cv=none; d=zohomail.com; s=zohoarc; b=NMWJxVWEY0BsPm8XHQmtQohjwa/AII44f9ESDmVegrCdLnKK3JqKxqnR+y+e7RkiJrxNUX39KlmoUqa4MFZUO8ls6sQw4X2ZrLtGIkc/PCgT21hMGuYTq6eu+lG5kwKdPsuLjbVs0WS0luD4VJxweoa93tuQwKd71dGhxJuven4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628531807; 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=+SXmZnVGaWXOY/RYkmLfCtWMULRdMkbPmQ9ViuTHD4g=; b=Vwn1aBKr1+zMfj7ez9dpWCcS1dtiOr4T+nGLWifJCcKjMNPDIUW7sW+z/JidlaT9iAHrqs71+ZNmbxRjMmDjamRY8erraO6GfPhvLERaMHKuD8mPUX9cymdZLYXyYZghfzPsvbFSXchPt7XwpwO1GXAevEC5kKu4ANO2gURBuss= 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 162853180752544.67152547895557; Mon, 9 Aug 2021 10:56:47 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.165113.301766 (Exim 4.92) (envelope-from ) id 1mD9Vp-0000UI-45; Mon, 09 Aug 2021 17:56:33 +0000 Received: by outflank-mailman (output) from mailman id 165113.301766; Mon, 09 Aug 2021 17:56:33 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9Vp-0000UB-1D; Mon, 09 Aug 2021 17:56:33 +0000 Received: by outflank-mailman (input) for mailman id 165113; Mon, 09 Aug 2021 17:56:32 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9Vo-0000Aq-1I for xen-devel@lists.xenproject.org; Mon, 09 Aug 2021 17:56:32 +0000 Received: from mail-pj1-x1033.google.com (unknown [2607:f8b0:4864:20::1033]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 64359619-a63f-477e-aaae-c806bd099924; Mon, 09 Aug 2021 17:56:27 +0000 (UTC) Received: by mail-pj1-x1033.google.com with SMTP id j1so29234607pjv.3 for ; Mon, 09 Aug 2021 10:56:27 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:2:106e:6ed1:5da1:2ac4]) by smtp.gmail.com with ESMTPSA id x14sm20589708pfa.127.2021.08.09.10.56.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Aug 2021 10:56:26 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 64359619-a63f-477e-aaae-c806bd099924 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+SXmZnVGaWXOY/RYkmLfCtWMULRdMkbPmQ9ViuTHD4g=; b=vEiVBsmsjWFoq4H9xIe2JkVBV6ZRxO8oTc1d0POF8FBUFnHfygUIcXu1Q6lIUO6G8V OZ0eKnK8b2FYfXzWwIFzo7hO/SDSbktZBgBA9q5MzDM2tSVD+lUqIOyamzBgk9mM4sd7 +/ChsJIEW5dMQMfRN0VzltH1RuxxvHykERIWU2rB7pLq/8xj0nq7SVaiK3ZsuHFQ8FoW D9nA4u0vEJoYCDwR5KWdept1EbyGZC9nhnkSNJdwsfiss59YjCsICZPyWMfbNLKZ4vZL o6/PPIdsLDr6vZuwTGl4jdGx8871bBC+zvCYBedAkrhQuufnqjjdtdNfQqXrkOuRK22k 4L9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+SXmZnVGaWXOY/RYkmLfCtWMULRdMkbPmQ9ViuTHD4g=; b=tsq5ZbeWDPfYp82NJxEOGzI2MGJXVvy3taVOFShOy1Y+tsZZT6yJfyPILR7Xl4Pf5q Zv8+v/5DQfonrqj42S1M0UOL1n08P2x0yJy1y8+u49v6zF29vavsYw9FcY9Eib3mgCN/ 0FjcVKMyCFqlA5N5N0VV+VqWPto917TZ7kBtxQh8Doz21xKUPV1QRNaH7MCnbvdZDCy2 sNQKtR9A1mAhz8oeXBLd7rSEAf8m4pCDyHtW2/zdAFAXiDVMLIlWT18WgWAfgy7NWx/P J0k2fAK2VE2c5K+yc0nz3kmTHr9QsNPw5ymBioWNsrtb99NaS8WpGaM/N8eWEi53w/P3 /8rQ== X-Gm-Message-State: AOAM530WLI7FkjGL5iF7KlF8IZvjRvHo3+51TqWPjSOrQWHba10darSW N5agjtsK/gCGAPmTXaMurjo= X-Google-Smtp-Source: ABdhPJw/Bw/9aycFrRX8qJNE8oCpvKM3rGElUB7qQ9nDgVBBQ7qhpDJqT8kHjxicHZLhAf9ipjwyRA== X-Received: by 2002:a62:f244:0:b029:3c3:100f:c302 with SMTP id y4-20020a62f2440000b02903c3100fc302mr25175993pfl.37.1628531786781; Mon, 09 Aug 2021 10:56: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, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, ardb@kernel.org, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, saravanand@fb.com, krish.sadhukhan@oracle.com, aneesh.kumar@linux.ibm.com, xen-devel@lists.xenproject.org, rientjes@google.com, hannes@cmpxchg.org, tj@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V3 01/13] x86/HV: Initialize GHCB page in Isolation VM Date: Mon, 9 Aug 2021 13:56:05 -0400 Message-Id: <20210809175620.720923-2-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210809175620.720923-1-ltykernel@gmail.com> References: <20210809175620.720923-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628531809206100001 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan Hyper-V 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. Signed-off-by: Tianyu Lan --- arch/x86/hyperv/hv_init.c | 66 +++++++++++++++++++++++++++++++-- arch/x86/include/asm/mshyperv.h | 2 + include/asm-generic/mshyperv.h | 2 + 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c index 708a2712a516..0bb4d9ca7a55 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 @@ -42,6 +43,31 @@ static void *hv_hypercall_pg_saved; 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 (!ms_hyperv.ghcb_base) + return -EINVAL; + + /* + * GHCB page is allocated by paravisor. The address + * returned by MSR_AMD64_SEV_ES_GHCB is above shared + * ghcb boundary and map it here. + */ + rdmsrl(MSR_AMD64_SEV_ES_GHCB, ghcb_gpa); + ghcb_va =3D memremap(ghcb_gpa, HV_HYP_PAGE_SIZE, MEMREMAP_WB); + if (!ghcb_va) + return -ENOMEM; + + ghcb_base =3D (void **)this_cpu_ptr(ms_hyperv.ghcb_base); + *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,6 +111,8 @@ static int hv_cpu_init(unsigned int cpu) } } =20 + hyperv_init_ghcb(); + return 0; } =20 @@ -177,6 +205,14 @@ static int hv_cpu_die(unsigned int cpu) { struct hv_reenlightenment_control re_ctrl; unsigned int new_cpu; + void **ghcb_va =3D NULL; + + if (ms_hyperv.ghcb_base) { + ghcb_va =3D (void **)this_cpu_ptr(ms_hyperv.ghcb_base); + if (*ghcb_va) + memunmap(*ghcb_va); + *ghcb_va =3D NULL; + } =20 hv_common_cpu_die(cpu); =20 @@ -383,9 +419,19 @@ 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; + + if (hv_isolation_type_snp()) { + ms_hyperv.ghcb_base =3D alloc_percpu(void *); + if (!ms_hyperv.ghcb_base) + goto clean_guest_os_id; + + if (hyperv_init_ghcb()) { + free_percpu(ms_hyperv.ghcb_base); + ms_hyperv.ghcb_base =3D NULL; + goto clean_guest_os_id; + } } =20 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); @@ -456,7 +502,8 @@ 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_vp_assist_page: kfree(hv_vp_assist_page); @@ -484,6 +531,9 @@ void hyperv_cleanup(void) */ hv_hypercall_pg =3D NULL; =20 + if (ms_hyperv.ghcb_base) + free_percpu(ms_hyperv.ghcb_base); + /* Reset the hypercall page */ hypercall_msr.as_uint64 =3D 0; wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); @@ -559,3 +609,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..6627cfd2bfba 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); diff --git a/include/asm-generic/mshyperv.h b/include/asm-generic/mshyperv.h index c1ab6a6e72b5..4269f3174e58 100644 --- a/include/asm-generic/mshyperv.h +++ b/include/asm-generic/mshyperv.h @@ -36,6 +36,7 @@ struct ms_hyperv_info { u32 max_lp_index; u32 isolation_config_a; u32 isolation_config_b; + void __percpu **ghcb_base; }; extern struct ms_hyperv_info ms_hyperv; =20 @@ -237,6 +238,7 @@ bool hv_is_hyperv_initialized(void); bool hv_is_hibernation_supported(void); enum hv_isolation_type hv_get_isolation_type(void); bool hv_is_isolation_supported(void); +bool hv_isolation_type_snp(void); void hyperv_cleanup(void); bool hv_query_ext_cap(u64 cap_query); #else /* CONFIG_HYPERV */ --=20 2.25.1 From nobody Sun Apr 28 05:50:41 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=1628531810; cv=none; d=zohomail.com; s=zohoarc; b=Xj7gbT0F0RsUlVppELgZ+wd+XGBz98ysPSORZs07ByC3PSwqpMzaOBsq4FYHaKC30zD11bqzXfwr4ReZwcCTLIy3bGZM7IjCXdlYd+INDulpT59YE9RmfUSMtIIdSk4XZLy+i7N6mVANQcVDfcz2nrMha1pNc2h6Sk3iFHeUztI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628531810; 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=WJ8Qj60FdMGjOsTL0G9bdAidXdjqOzX7Q2WO2gNuvy8=; b=REr0eAZplmkIUf/0rt/4m4elAFyAKBcay7aRvoioTjUO9zfouX8JJs5WrE0FKMXlMQ0IHgEZCM6iwomCgf6jCckdfe2f+JDknvFsfyYIbpeb1DfXZ2zMDAhD21L4bi56ADOr6K7OrKA6b8wk3RjElRE+x30i6KfrrbxSS0nJPLw= 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 1628531810124547.9910888247514; Mon, 9 Aug 2021 10:56:50 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.165114.301778 (Exim 4.92) (envelope-from ) id 1mD9Vu-0000pI-CP; Mon, 09 Aug 2021 17:56:38 +0000 Received: by outflank-mailman (output) from mailman id 165114.301778; Mon, 09 Aug 2021 17:56: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 1mD9Vu-0000pA-8z; Mon, 09 Aug 2021 17:56:38 +0000 Received: by outflank-mailman (input) for mailman id 165114; Mon, 09 Aug 2021 17:56: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 1mD9Vt-0000Aq-1J for xen-devel@lists.xenproject.org; Mon, 09 Aug 2021 17:56:37 +0000 Received: from mail-pl1-x62e.google.com (unknown [2607:f8b0:4864:20::62e]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id da110ea0-bc60-4e7e-a9ca-3b825ff09e61; Mon, 09 Aug 2021 17:56:29 +0000 (UTC) Received: by mail-pl1-x62e.google.com with SMTP id e19so3789067pla.10 for ; Mon, 09 Aug 2021 10:56:29 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:2:106e:6ed1:5da1:2ac4]) by smtp.gmail.com with ESMTPSA id x14sm20589708pfa.127.2021.08.09.10.56.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Aug 2021 10:56: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: da110ea0-bc60-4e7e-a9ca-3b825ff09e61 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WJ8Qj60FdMGjOsTL0G9bdAidXdjqOzX7Q2WO2gNuvy8=; b=kCOSzGmSmj2pkcs1y/QfkWI/Rb7FWu5Rt4HurHSycKb+4UJ6Aawe7iIjpLCK57NZvg WiKFB2lpA+UNPjQlXnkCjTyoLNfzfD6exWmoaSXwOTH3P5K09H7X97U+U6AERK1MWKDZ Uf9gBLdRp3afuP7JcZ7KvCly3MyakdtS0qQtW8YXpaZpP2oaIZuXn8AWr8r0q7m5PL19 tGreCVJAddF2G7xYeJM4tw24jIsggOxYdQF69HhvqJzxkbw7sa2z7MLBTUyIBYEQNqtI yiQt/exmEXhRuDLkVp63WhMYXTqisSEe9mzIC1qRRU8DWNDpsvO5gCpWRZsGVSmFCKkw sUiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WJ8Qj60FdMGjOsTL0G9bdAidXdjqOzX7Q2WO2gNuvy8=; b=Vl4tH/b2ddUQeUamUXKMW+JnG5I9qv3Psv+3HxESFdDQHYn404rDDh/gFyjP5DPkdw KI4j9rQdGmpID1mfAFMupZfA+vZEk1B0WQBkuzK3OhyxF7a2+aqrXclH8FC5HiMdizg4 ICBsFTlz09uwoQw+LfO1dcvFTdb6a4ANy1qjkIKafC82IydhF7Z6TzYGq3npDA5SwbTW V1BFWReljR6jqT6Ziq6hkNQ/+V49ZEoJg7iu96iJnm1M6e/mY/RF35Cfa/42SwUQsHip Q727jJhvBbBagx//ZEXAJ16JAJis+2o2tsIA0EK/Njj9C8zWKCxkgeYKyPkz+kgBTgix T+QA== X-Gm-Message-State: AOAM532P0+Smwnt7bF7xikahuz6xyP1QNlnvNXXQW/MksF6Sd4m2GGx+ Uxfd3bhP7qjYcO+mOzjaWbo= X-Google-Smtp-Source: ABdhPJxmwF3scgEEjXrYinjzLnwHVvcE27E2cso3ne1oTy5d8B/iiqwH78j0wxYg/3jFPvSCO0RgTg== X-Received: by 2002:a62:1c42:0:b029:3c3:59ee:6068 with SMTP id c63-20020a621c420000b02903c359ee6068mr19584222pfc.72.1628531788936; Mon, 09 Aug 2021 10:56:28 -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, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, ardb@kernel.org, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, saravanand@fb.com, krish.sadhukhan@oracle.com, aneesh.kumar@linux.ibm.com, xen-devel@lists.xenproject.org, rientjes@google.com, hannes@cmpxchg.org, tj@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V3 02/13] x86/HV: Initialize shared memory boundary in the Isolation VM. Date: Mon, 9 Aug 2021 13:56:06 -0400 Message-Id: <20210809175620.720923-3-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210809175620.720923-1-ltykernel@gmail.com> References: <20210809175620.720923-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628531811311100005 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 --- 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 6b5835a087a3..2b7f396ef1a5 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 + (u64)1 << 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 4269f3174e58..aa26d24a5ca9 100644 --- a/include/asm-generic/mshyperv.h +++ b/include/asm-generic/mshyperv.h @@ -35,8 +35,18 @@ struct ms_hyperv_info { u32 max_vp_index; u32 max_lp_index; u32 isolation_config_a; - u32 isolation_config_b; + union { + u32 isolation_config_b; + struct { + u32 cvm_type : 4; + u32 Reserved11 : 1; + u32 shared_gpa_boundary_active : 1; + u32 shared_gpa_boundary_bits : 6; + u32 Reserved12 : 20; + }; + }; void __percpu **ghcb_base; + u64 shared_gpa_boundary; }; extern struct ms_hyperv_info ms_hyperv; =20 --=20 2.25.1 From nobody Sun Apr 28 05:50:41 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=1628531819; cv=none; d=zohomail.com; s=zohoarc; b=oKD4zDorz4YHFsHXg7Kqzlilo37wVnXVo9vOGXI/rns9Uj4xTiGpkR+1Coiksw1M43FC6Wqj6yXKM0pKf+Ep5mS1ropK7Lp7g3XC/MaiVsRjete0TLfZkCWxbCQ3j3A67uxVCne6hxqpzIGcoWpNnPabSF66rMM1licXgoONnm4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628531819; 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=Zn6gCEnKxJzxqfuyJcO64i09lNmUHRRDFMW+U0r9l4c=; b=J6fT8cROhodOfYo4PNAV5bhUhjaOXySXJh9fP0nfbcjRH+Ys8Do19I8ohMVuS326KschYmTQ6npUlFWcGwJDes4qmWPL5wAZnFyO/gIuycQ6QvfeDGojbzmry1Jez9g5N1EruWzpxOHeax/CLQSAmnKINpKpFS3t1qQAyhABilc= 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 1628531819897953.9698443751281; Mon, 9 Aug 2021 10:56:59 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.165115.301789 (Exim 4.92) (envelope-from ) id 1mD9Vz-0001Fj-Kl; Mon, 09 Aug 2021 17:56:43 +0000 Received: by outflank-mailman (output) from mailman id 165115.301789; Mon, 09 Aug 2021 17:56:43 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9Vz-0001FW-H6; Mon, 09 Aug 2021 17:56:43 +0000 Received: by outflank-mailman (input) for mailman id 165115; Mon, 09 Aug 2021 17:56:42 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9Vy-0000Aq-1J for xen-devel@lists.xenproject.org; Mon, 09 Aug 2021 17:56:42 +0000 Received: from mail-pj1-x1029.google.com (unknown [2607:f8b0:4864:20::1029]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id f8797023-e79b-4cc4-b7c3-d387a19bcc87; Mon, 09 Aug 2021 17:56:32 +0000 (UTC) Received: by mail-pj1-x1029.google.com with SMTP id pj14-20020a17090b4f4eb029017786cf98f9so1155830pjb.2 for ; Mon, 09 Aug 2021 10:56:32 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:2:106e:6ed1:5da1:2ac4]) by smtp.gmail.com with ESMTPSA id x14sm20589708pfa.127.2021.08.09.10.56.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Aug 2021 10:56:30 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: f8797023-e79b-4cc4-b7c3-d387a19bcc87 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Zn6gCEnKxJzxqfuyJcO64i09lNmUHRRDFMW+U0r9l4c=; b=F4F48f4jr6DNkNJAqrdEmKYpyJXH1h/G5q/zjgsZrCdkssJo2/cWbiFQpW3YP7iLjY sXZCJj9c6tMDviC1cbicPKVYWNBpnrK7yAV4AHtPvfQjM5XkcSZOWLxb0fywEI9nUj6c 6ZcdMH0wYWG6o7FLNJ2ZkE0455JwRDus1HZ57miGOfy51q/Dc3MjRHe4mt5bOIDiS8AK 6CcihOvWiRXqTaeIV/+AlVz0rod48dEm17wA5a35+fmvkjBNVjiMtcaWCgPg/EA63dgQ ziV2CQI+/ejbhhsQ1JkIqLBwWPkQ8Q31SoHAavmQAa9PymbgUO5Wc/Iq2MHRhBzMJcK8 B8Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Zn6gCEnKxJzxqfuyJcO64i09lNmUHRRDFMW+U0r9l4c=; b=KqkhAqIuigWNiblHdtlBKLlAp1bOTLFgENrPqauZcNfrjzm3Wfv8neNDbNoXofVCvp LnaLZKkRZP/zzhY+hXfqb56iSVGb7/Jn/fmDCi2hl0B+cORJir6dKoDIbv1+Je4Zl7O7 N5MrvU7fqmZbSQfbG0GDXwWGbJwvnFrf0Muo2sXS8wVSYpCkXzKNcQpSJLtZOgDVoTZ7 5O4AKg1yZuOW+aY2MIB5jZh4xkg85NSB+BzVEIKcnrcuNtZrv+IwAMffP6Injs100xU3 cQgca6IaSzrLhdl87Bz/9glNyrURPQFsAae4ETlabJneGbWkEJFXX1cXO/CK4/84cMoN ZIjQ== X-Gm-Message-State: AOAM532OvT3Q25dz/iLmpYtFmw7iWYI366myxFLAUor/0173BsFamZo0 7TdIuyQIjlz6MRwRopgTu34= X-Google-Smtp-Source: ABdhPJzj2I3P/UMvsOq2c11ZGlrODCC2Lo52LstZDkdvjFRxOvD2OIwXBZKpPA2TUULjg7qyGih5Qg== X-Received: by 2002:a17:90a:1f49:: with SMTP id y9mr296003pjy.225.1628531791065; Mon, 09 Aug 2021 10:56: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, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, ardb@kernel.org, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, saravanand@fb.com, krish.sadhukhan@oracle.com, aneesh.kumar@linux.ibm.com, xen-devel@lists.xenproject.org, rientjes@google.com, hannes@cmpxchg.org, tj@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V3 03/13] x86/HV: Add new hvcall guest address host visibility support Date: Mon, 9 Aug 2021 13:56:07 -0400 Message-Id: <20210809175620.720923-4-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210809175620.720923-1-ltykernel@gmail.com> References: <20210809175620.720923-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628531820578100001 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. Signed-off-by: Tianyu Lan Acked-by: Dave Hansen --- 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 | 114 +++++++++++++++++++++++++++++ arch/x86/include/asm/hyperv-tlfs.h | 20 +++++ arch/x86/include/asm/mshyperv.h | 4 +- arch/x86/mm/pat/set_memory.c | 19 +++-- include/asm-generic/hyperv-tlfs.h | 1 + include/asm-generic/mshyperv.h | 1 + 8 files changed, 160 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 0bb4d9ca7a55..b3683083208a 100644 --- a/arch/x86/hyperv/hv_init.c +++ b/arch/x86/hyperv/hv_init.c @@ -607,6 +607,12 @@ EXPORT_SYMBOL_GPL(hv_get_isolation_type); =20 bool hv_is_isolation_supported(void) { + if (!cpu_feature_enabled(X86_FEATURE_HYPERVISOR)) + return 0; + + if (!hypervisor_is_type(X86_HYPER_MS_HYPERV)) + return 0; + return hv_get_isolation_type() !=3D HV_ISOLATION_TYPE_NONE; } =20 diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c new file mode 100644 index 000000000000..8c905ffdba7f --- /dev/null +++ b/arch/x86/hyperv/ivm.c @@ -0,0 +1,114 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Hyper-V Isolation VM interface with paravisor and hypervisor + * + * Author: + * Tianyu Lan + */ + +#include +#include +#include +#include +#include +#include + +/* + * hv_mark_gpa_visibility - Set pages visible to host via hvcall. + * + * In Isolation VM, all guest memory is encripted from host and guest + * needs to set memory visible to host via hvcall before sharing memory + * with host. + */ +int hv_mark_gpa_visibility(u16 count, const u64 pfn[], + enum hv_mem_host_visibility visibility) +{ + struct hv_gpa_range_for_visibility **input_pcpu, *input; + u16 pages_processed; + u64 hv_status; + unsigned long flags; + + /* no-op if partition isolation is not enabled */ + if (!hv_is_isolation_supported()) + return 0; + + if (count > HV_MAX_MODIFY_GPA_REP_COUNT) { + pr_err("Hyper-V: GPA count:%d exceeds supported:%lu\n", count, + HV_MAX_MODIFY_GPA_REP_COUNT); + return -EINVAL; + } + + local_irq_save(flags); + input_pcpu =3D (struct hv_gpa_range_for_visibility **) + this_cpu_ptr(hyperv_pcpu_input_arg); + input =3D *input_pcpu; + if (unlikely(!input)) { + local_irq_restore(flags); + return -EINVAL; + } + + input->partition_id =3D HV_PARTITION_ID_SELF; + input->host_visibility =3D visibility; + input->reserved0 =3D 0; + input->reserved1 =3D 0; + memcpy((void *)input->gpa_page_list, pfn, count * sizeof(*pfn)); + hv_status =3D hv_do_rep_hypercall( + HVCALL_MODIFY_SPARSE_GPA_PAGE_HOST_VISIBILITY, count, + 0, input, &pages_processed); + local_irq_restore(flags); + + if (!(hv_status & HV_HYPERCALL_RESULT_MASK)) + return 0; + + return hv_status & HV_HYPERCALL_RESULT_MASK; +} +EXPORT_SYMBOL(hv_mark_gpa_visibility); + +static int __hv_set_mem_host_visibility(void *kbuffer, int pagecount, + enum hv_mem_host_visibility visibility) +{ + u64 *pfn_array; + int ret =3D 0; + int i, pfn; + + if (!hv_is_isolation_supported() || !ms_hyperv.ghcb_base) + return 0; + + pfn_array =3D kzalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL); + if (!pfn_array) + return -ENOMEM; + + for (i =3D 0, pfn =3D 0; i < pagecount; i++) { + pfn_array[pfn] =3D virt_to_hvpfn(kbuffer + i * HV_HYP_PAGE_SIZE); + pfn++; + + if (pfn =3D=3D HV_MAX_MODIFY_GPA_REP_COUNT || i =3D=3D pagecount - 1) { + ret |=3D hv_mark_gpa_visibility(pfn, pfn_array, + visibility); + pfn =3D 0; + + if (ret) + goto err_free_pfn_array; + } + } + + err_free_pfn_array: + kfree(pfn_array); + return ret; +} + +/* + * hv_set_mem_host_visibility - Set specified memory visible to host. + * + * In Isolation VM, all guest memory is encrypted from host and guest + * needs to set memory visible to host via hvcall before sharing memory + * with host. This function works as wrap of hv_mark_gpa_visibility() + * with memory base and size. + */ +int hv_set_mem_host_visibility(unsigned long addr, int numpages, bool visi= ble) +{ + enum hv_mem_host_visibility visibility =3D visible ? + VMBUS_PAGE_VISIBLE_READ_WRITE : VMBUS_PAGE_NOT_VISIBLE; + + return __hv_set_mem_host_visibility((void *)addr, numpages, visibility); +} diff --git a/arch/x86/include/asm/hyperv-tlfs.h b/arch/x86/include/asm/hype= rv-tlfs.h index 2322d6bd5883..1691d2bce0b7 100644 --- a/arch/x86/include/asm/hyperv-tlfs.h +++ b/arch/x86/include/asm/hyperv-tlfs.h @@ -276,6 +276,13 @@ 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 +}; + /* * Declare the MSR used to setup pages used to communicate with the hyperv= isor. */ @@ -587,4 +594,17 @@ enum hv_interrupt_type { =20 #include =20 +/* All input parameters should be in single page. */ +#define HV_MAX_MODIFY_GPA_REP_COUNT \ + ((PAGE_SIZE / sizeof(u64)) - 2) + +/* HvCallModifySparseGpaPageHostVisibility hypercall */ +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; + #endif diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyper= v.h index 6627cfd2bfba..87a386fa97f7 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -190,7 +190,9 @@ struct irq_domain *hv_create_pci_msi_domain(void); int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vecto= r, struct hv_interrupt_entry *entry); int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *en= try); - +int hv_mark_gpa_visibility(u16 count, const u64 pfn[], + enum hv_mem_host_visibility visibility); +int hv_set_mem_host_visibility(unsigned long addr, int numpages, bool visi= ble); #else /* CONFIG_HYPERV */ static inline void hyperv_init(void) {} static inline void hyperv_setup_mmu_ops(void) {} diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index ad8a5c586a35..1e4a0882820a 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -29,6 +29,8 @@ #include #include #include +#include +#include =20 #include "../mm_internal.h" =20 @@ -1980,15 +1982,11 @@ int set_memory_global(unsigned long addr, int numpa= ges) __pgprot(_PAGE_GLOBAL), 0); } =20 -static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc) +static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool= enc) { struct cpa_data cpa; int ret; =20 - /* Nothing to do if memory encryption is not active */ - if (!mem_encrypt_active()) - return 0; - /* Should not be working on unaligned addresses */ if (WARN_ONCE(addr & ~PAGE_MASK, "misaligned address: %#lx\n", addr)) addr &=3D PAGE_MASK; @@ -2023,6 +2021,17 @@ static int __set_memory_enc_dec(unsigned long addr, = int numpages, bool enc) return ret; } =20 +static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc) +{ + if (hv_is_isolation_supported()) + return hv_set_mem_host_visibility(addr, numpages, !enc); + + if (mem_encrypt_active()) + return __set_memory_enc_pgtable(addr, numpages, enc); + + return 0; +} + int set_memory_encrypted(unsigned long addr, int numpages) { return __set_memory_enc_dec(addr, numpages, true); diff --git a/include/asm-generic/hyperv-tlfs.h b/include/asm-generic/hyperv= -tlfs.h index 56348a541c50..8ed6733d5146 100644 --- a/include/asm-generic/hyperv-tlfs.h +++ b/include/asm-generic/hyperv-tlfs.h @@ -158,6 +158,7 @@ struct ms_hyperv_tsc_page { #define HVCALL_RETARGET_INTERRUPT 0x007e #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE 0x00af #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_LIST 0x00b0 +#define HVCALL_MODIFY_SPARSE_GPA_PAGE_HOST_VISIBILITY 0x00db =20 /* Extended hypercalls */ #define HV_EXT_CALL_QUERY_CAPABILITIES 0x8001 diff --git a/include/asm-generic/mshyperv.h b/include/asm-generic/mshyperv.h index aa26d24a5ca9..079988ed45b9 100644 --- a/include/asm-generic/mshyperv.h +++ b/include/asm-generic/mshyperv.h @@ -255,6 +255,7 @@ bool hv_query_ext_cap(u64 cap_query); static inline bool hv_is_hyperv_initialized(void) { return false; } static inline bool hv_is_hibernation_supported(void) { return false; } static inline void hyperv_cleanup(void) {} +static inline hv_is_isolation_supported(void); #endif /* CONFIG_HYPERV */ =20 #endif --=20 2.25.1 From nobody Sun Apr 28 05:50:41 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=1628531821; cv=none; d=zohomail.com; s=zohoarc; b=cooZDL09tKHNm2cUUzMjr/dPvOkwSoMaXSfhq2ZHh/PeFMD859w4PbiYxGYNRlJADq//xUBdXmPzSzG1rslY1ezkka3RRF6YwYgBRtREkG+Gt9psTlq5qo1q5aF3gH+bt8XPQeJNyKU7ykzZhXNgvTlqdPG/2LKF9KiHWxlPjn0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628531821; 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=Men1AZwUmuVs+mP8DF3xiO7Kxjvnd8nVq1v+nRjKpCQ=; b=LavHNBpzlKVD325ckC0f04AW59Q6XkylQXPlP3F6lQOIl3+Ft1O99dqyp9VsRIEoyjStGmBQiZqYrE18x0gv61uJo6+h04UmmmKcC0WlTBHqQJOkpqBi09euF119RpyJsEhRAQmX4VFDtOcvVQUSdCuVWcCkz0Vw/QytdKUImks= 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 1628531821078288.7532728746353; Mon, 9 Aug 2021 10:57:01 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.165116.301800 (Exim 4.92) (envelope-from ) id 1mD9W4-0001mb-7y; Mon, 09 Aug 2021 17:56:48 +0000 Received: by outflank-mailman (output) from mailman id 165116.301800; Mon, 09 Aug 2021 17:56:48 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9W4-0001mO-4J; Mon, 09 Aug 2021 17:56:48 +0000 Received: by outflank-mailman (input) for mailman id 165116; Mon, 09 Aug 2021 17:56:47 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9W3-0000Aq-1Z for xen-devel@lists.xenproject.org; Mon, 09 Aug 2021 17:56:47 +0000 Received: from mail-pl1-x633.google.com (unknown [2607:f8b0:4864:20::633]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id f0d68e06-ee91-4a1e-b2bc-ae23731dd6bd; Mon, 09 Aug 2021 17:56:35 +0000 (UTC) Received: by mail-pl1-x633.google.com with SMTP id k2so17264619plk.13 for ; Mon, 09 Aug 2021 10:56:35 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:2:106e:6ed1:5da1:2ac4]) by smtp.gmail.com with ESMTPSA id x14sm20589708pfa.127.2021.08.09.10.56.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Aug 2021 10:56:34 -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: f0d68e06-ee91-4a1e-b2bc-ae23731dd6bd DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Men1AZwUmuVs+mP8DF3xiO7Kxjvnd8nVq1v+nRjKpCQ=; b=khPH2C+O3EAicxhd7Ol8cKTH5M6jlmzfQ8AO/O1nDTvaVoKxSYt9ZFCTLGRbikb5lh PDJ33DWVYKUJkew4s5UHIfYAMLSYuNH+uf/XtXD0IORwvY1DIt7A3cBWxmGFheDk576S V3f7/9klpb/XtUJHfTr/xvgxlJQr/zFFSZScoCFNkrbrD15HrsX7R1uv6/aQEWxckoRN 9QsVgqkiNeWlCadxH+8e5eldRu1GEL06smeSmjiPy+yGlokaFMzt3CKLV48oxtK81odS 6JMrI0ItN47jYQqCSSni4G8Qh04K6SLFdJInx7pD26u4zhhajSI26iXbbtm/G5rwvFIk Sluw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Men1AZwUmuVs+mP8DF3xiO7Kxjvnd8nVq1v+nRjKpCQ=; b=P1PUWBnQiU8cEQ4wAVroGl48RRpFq+NV91ePhy49G2W+UmKoCej2fVmW4qZ4tHtX7X AiRZyQ2YADIGu3UG1d9gJ4Z8v7HTP+owZxMynGz3Md65oGuVHGhYce9ae9nnucN5o0+g DFrOp25ebDazpnuxyR6zZIE8D8hQ6wTQaTm416bC2HjsO7QlrId1LZn8fpiIaK1ZwERE 9mEN7XGDa45gXk9HqZ/C1vWy7ftPObuH4tFYrKLK9lGLGobrvjonUuuYTRuazmBjVc7S EYu5FjSezzhvA+NieHSSEMGdD3dnbZSTBtqSFaqil1AyooLgdoUrAVhEPhNuuSEAQrEu NMZQ== X-Gm-Message-State: AOAM530db8+LriKqALkKStTUDF33TG9/BjkynPW23Vty2Q9jwWD8Oh5t OLhNQUDwHlM36B77zwZb+lc= X-Google-Smtp-Source: ABdhPJzwKjD0clYdhmrGRt/ZwcFYRptbvrLB1rs9cJlW5QDBm6ePK+6Ij95/tTvvfr9i4l9z3WVYCQ== X-Received: by 2002:a17:90b:21c9:: with SMTP id ll9mr314744pjb.161.1628531794508; Mon, 09 Aug 2021 10:56: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, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, ardb@kernel.org, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, saravanand@fb.com, krish.sadhukhan@oracle.com, aneesh.kumar@linux.ibm.com, xen-devel@lists.xenproject.org, rientjes@google.com, hannes@cmpxchg.org, tj@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V3 04/13] HV: Mark vmbus ring buffer visible to host in Isolation VM Date: Mon, 9 Aug 2021 13:56:08 -0400 Message-Id: <20210809175620.720923-5-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210809175620.720923-1-ltykernel@gmail.com> References: <20210809175620.720923-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628531822549100005 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 --- drivers/hv/channel.c | 44 ++++++++++++++++++++++++++++++++++++++++-- include/linux/hyperv.h | 11 +++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/drivers/hv/channel.c b/drivers/hv/channel.c index f3761c73b074..4c4717c26240 100644 --- a/drivers/hv/channel.c +++ b/drivers/hv/channel.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include =20 @@ -465,7 +466,14 @@ static int __vmbus_establish_gpadl(struct vmbus_channe= l *channel, struct list_head *curr; u32 next_gpadl_handle; unsigned long flags; - int ret =3D 0; + int ret =3D 0, index; + + index =3D atomic_inc_return(&channel->gpadl_index) - 1; + + if (index > VMBUS_GPADL_RANGE_COUNT - 1) { + pr_err("Gpadl handle position(%d) has been occupied.\n", index); + return -ENOSPC; + } =20 next_gpadl_handle =3D (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1); @@ -474,6 +482,13 @@ static int __vmbus_establish_gpadl(struct vmbus_channe= l *channel, if (ret) return ret; =20 + ret =3D set_memory_decrypted((unsigned long)kbuffer, + HVPFN_UP(size)); + if (ret) { + pr_warn("Failed to set host visibility.\n"); + return ret; + } + init_completion(&msginfo->waitevent); msginfo->waiting_channel =3D channel; =20 @@ -539,6 +554,10 @@ static int __vmbus_establish_gpadl(struct vmbus_channe= l *channel, /* At this point, we received the gpadl created msg */ *gpadl_handle =3D gpadlmsg->gpadl; =20 + channel->gpadl_array[index].size =3D size; + channel->gpadl_array[index].buffer =3D kbuffer; + channel->gpadl_array[index].gpadlhandle =3D *gpadl_handle; + cleanup: spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); list_del(&msginfo->msglistentry); @@ -549,6 +568,13 @@ static int __vmbus_establish_gpadl(struct vmbus_channe= l *channel, } =20 kfree(msginfo); + + if (ret) { + set_memory_encrypted((unsigned long)kbuffer, + HVPFN_UP(size)); + atomic_dec(&channel->gpadl_index); + } + return ret; } =20 @@ -676,6 +702,7 @@ static int __vmbus_open(struct vmbus_channel *newchanne= l, =20 /* Establish the gpadl for the ring buffer */ newchannel->ringbuffer_gpadlhandle =3D 0; + atomic_set(&newchannel->gpadl_index, 0); =20 err =3D __vmbus_establish_gpadl(newchannel, HV_GPADL_RING, page_address(newchannel->ringbuffer_page), @@ -811,7 +838,7 @@ int vmbus_teardown_gpadl(struct vmbus_channel *channel,= u32 gpadl_handle) struct vmbus_channel_gpadl_teardown *msg; struct vmbus_channel_msginfo *info; unsigned long flags; - int ret; + int ret, i; =20 info =3D kzalloc(sizeof(*info) + sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL); @@ -859,6 +886,19 @@ int vmbus_teardown_gpadl(struct vmbus_channel *channel= , u32 gpadl_handle) spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); =20 kfree(info); + + /* Find gpadl buffer virtual address and size. */ + for (i =3D 0; i < VMBUS_GPADL_RANGE_COUNT; i++) + if (channel->gpadl_array[i].gpadlhandle =3D=3D gpadl_handle) + break; + + if (set_memory_encrypted((unsigned long)channel->gpadl_array[i].buffer, + HVPFN_UP(channel->gpadl_array[i].size))) + pr_warn("Fail to set mem host visibility.\n"); + + channel->gpadl_array[i].gpadlhandle =3D 0; + atomic_dec(&channel->gpadl_index); + return ret; } EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl); diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h index ddc8713ce57b..90b542597143 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -803,6 +803,14 @@ struct vmbus_device { =20 #define VMBUS_DEFAULT_MAX_PKT_SIZE 4096 =20 +struct vmbus_gpadl { + u32 gpadlhandle; + u32 size; + void *buffer; +}; + +#define VMBUS_GPADL_RANGE_COUNT 3 + struct vmbus_channel { struct list_head listentry; =20 @@ -823,6 +831,9 @@ struct vmbus_channel { struct completion rescind_event; =20 u32 ringbuffer_gpadlhandle; + /* GPADL_RING and Send/Receive GPADL_BUFFER. */ + struct vmbus_gpadl gpadl_array[VMBUS_GPADL_RANGE_COUNT]; + atomic_t gpadl_index; =20 /* Allocated memory for ring buffer */ struct page *ringbuffer_page; --=20 2.25.1 From nobody Sun Apr 28 05:50:41 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=1628531828; cv=none; d=zohomail.com; s=zohoarc; b=AjEDp8hdvM4yXCowz5KSd4PQAv2zWG2vfnarzLBVZ2uKO5nbHOCZardSNAD2JbTIqPFbbnwVzp9D2LEQaOBTx/Q+JsyHkcAGZDHmWjUR2Iua8IGnSNj0HsO9n5KI13r+Gj5Q8YK8pqre57Glx1Ujn3FO8mLQ4On/Q9nBonoXFmo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628531828; 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=SGrxuB4DVPdzsnNP0DulQ9yeXx6GxEaahsdAOqnVAjM=; b=Nf1DfqZC0nsAKFOhEJKc9Mi+G210bxdvcHLbX52QGnKqbItmttlSNLcfNfec4NdC2tvvsN9kIgGFcHlJQGIQ7w6X7zJYSuOglBZDPeEqUtJAQibGO5zlGD/yQhyZFx3kJ2Lo3eDmBtA2m/bXc7pWJZwWtUwFAfvkTyUcu/dcxlw= 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 1628531828293313.5489234976218; Mon, 9 Aug 2021 10:57:08 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.165120.301811 (Exim 4.92) (envelope-from ) id 1mD9W9-0002Ip-Gn; Mon, 09 Aug 2021 17:56:53 +0000 Received: by outflank-mailman (output) from mailman id 165120.301811; Mon, 09 Aug 2021 17:56:53 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9W9-0002IY-D4; Mon, 09 Aug 2021 17:56:53 +0000 Received: by outflank-mailman (input) for mailman id 165120; Mon, 09 Aug 2021 17:56:52 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9W8-0000Aq-1n for xen-devel@lists.xenproject.org; Mon, 09 Aug 2021 17:56:52 +0000 Received: from mail-pj1-x1035.google.com (unknown [2607:f8b0:4864:20::1035]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 7d2505eb-fb3b-49aa-ab43-a017d7c8c24a; Mon, 09 Aug 2021 17:56:37 +0000 (UTC) Received: by mail-pj1-x1035.google.com with SMTP id cp15-20020a17090afb8fb029017891959dcbso86904pjb.2 for ; Mon, 09 Aug 2021 10:56:37 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:2:106e:6ed1:5da1:2ac4]) by smtp.gmail.com with ESMTPSA id x14sm20589708pfa.127.2021.08.09.10.56.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Aug 2021 10:56: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: 7d2505eb-fb3b-49aa-ab43-a017d7c8c24a DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SGrxuB4DVPdzsnNP0DulQ9yeXx6GxEaahsdAOqnVAjM=; b=AEz62xouP0OoIkaPiX66MaEEzJqmuDB5n+Jjqb3xJ9ZVZC7tqVVAhj8DXtgga9Pv9x 4Cri82FFw3GW982rAw19ZTpBdw0b13pW+5XSPiMzIbLifhi1sJmuNCC/qvKFT2eHKYLT 7NLtHhNw0EjcCMCzCnKMhH/SsOWO82zv35oIUnAkXIYytyTTe0Mm7Gu98yX7UV/8VSa7 WgJWkEI/GHiUjna70lOXK5gnsaPZHI4w7RfTpRMpI4IPrFhgYMHOhxAMTg0IlDkKgjFP mS8/YMpiXYkRhr/edhU5cc1CK6QW/REyf/Hh4XB+5VG8FVNacW50XufN8JF0eRdVOmDJ baKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SGrxuB4DVPdzsnNP0DulQ9yeXx6GxEaahsdAOqnVAjM=; b=l/cmh01gSMFGVusyyjqiyHD91jN6Snlc0anD6lsamY64IAnqqjd6PPhFSjz5EocR3j KDAgKIR9O/NCXEfuUP7kW5TcIDjczcb8aqNoYmvuv0/kAVKhCpxZ4usyYp1jBaffA5dB 4pBSfd94UfpWyqzSffgidEu7qtoOGsff++nwBdVKdjxfWUqC8rwj0LIe10PmUWxpPI5B +TnotHN5zFn05EDZ9iMV1UvgaFRQL/6mN3Kp0k3a8Yre2WVB3LEzeyG/+562Y0Vl8H0S 5A+lHf5NczD3fyw5JcPyuQ1pim9s1KNckCwb7irDMq6j0YxXqaAV+HXOiuIrc/U0UXyn QIXw== X-Gm-Message-State: AOAM530FadkTDVdgdIdpVQe6g4ypKlzso958DK0VBa6+kx4aFILjjpFf J/sASXGuSsgQ3d3xEqRmzTQ= X-Google-Smtp-Source: ABdhPJz2NsNe+rB36EhkRcSdVQ4tNPo8tby+PnC8VCKPNzQ9joAqp6ab2g9vHv38avkubu3FdLiSWQ== X-Received: by 2002:a17:90a:ba8e:: with SMTP id t14mr26985957pjr.176.1628531796987; Mon, 09 Aug 2021 10:56:36 -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, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, ardb@kernel.org, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, saravanand@fb.com, krish.sadhukhan@oracle.com, aneesh.kumar@linux.ibm.com, xen-devel@lists.xenproject.org, rientjes@google.com, hannes@cmpxchg.org, tj@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V3 05/13] HV: Add Write/Read MSR registers via ghcb page Date: Mon, 9 Aug 2021 13:56:09 -0400 Message-Id: <20210809175620.720923-6-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210809175620.720923-1-ltykernel@gmail.com> References: <20210809175620.720923-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628531830682100003 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan Hyper-V 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. Hyper-V 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. Signed-off-by: Tianyu Lan --- Change since v1: * Introduce sev_es_ghcb_hv_call_simple() and share code between SEV and Hyper-V code. --- arch/x86/hyperv/hv_init.c | 33 ++------- arch/x86/hyperv/ivm.c | 110 +++++++++++++++++++++++++++++ arch/x86/include/asm/mshyperv.h | 78 +++++++++++++++++++- arch/x86/include/asm/sev.h | 3 + arch/x86/kernel/cpu/mshyperv.c | 3 + arch/x86/kernel/sev-shared.c | 63 ++++++++++------- drivers/hv/hv.c | 121 ++++++++++++++++++++++---------- include/asm-generic/mshyperv.h | 12 +++- 8 files changed, 329 insertions(+), 94 deletions(-) diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c index b3683083208a..ab0b33f621e7 100644 --- a/arch/x86/hyperv/hv_init.c +++ b/arch/x86/hyperv/hv_init.c @@ -423,7 +423,7 @@ void __init hyperv_init(void) goto clean_guest_os_id; =20 if (hv_isolation_type_snp()) { - ms_hyperv.ghcb_base =3D alloc_percpu(void *); + ms_hyperv.ghcb_base =3D alloc_percpu(union hv_ghcb __percpu *); if (!ms_hyperv.ghcb_base) goto clean_guest_os_id; =20 @@ -432,6 +432,9 @@ void __init hyperv_init(void) ms_hyperv.ghcb_base =3D NULL; goto clean_guest_os_id; } + + /* 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); } =20 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); @@ -523,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, @@ -596,30 +600,3 @@ bool hv_is_hyperv_initialized(void) return hypercall_msr.enable; } EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized); - -enum hv_isolation_type hv_get_isolation_type(void) -{ - if (!(ms_hyperv.priv_high & HV_ISOLATION)) - return HV_ISOLATION_TYPE_NONE; - return FIELD_GET(HV_ISOLATION_TYPE, ms_hyperv.isolation_config_b); -} -EXPORT_SYMBOL_GPL(hv_get_isolation_type); - -bool hv_is_isolation_supported(void) -{ - if (!cpu_feature_enabled(X86_FEATURE_HYPERVISOR)) - return 0; - - if (!hypervisor_is_type(X86_HYPER_MS_HYPERV)) - return 0; - - return hv_get_isolation_type() !=3D HV_ISOLATION_TYPE_NONE; -} - -DEFINE_STATIC_KEY_FALSE(isolation_type_snp); - -bool hv_isolation_type_snp(void) -{ - return static_branch_unlikely(&isolation_type_snp); -} -EXPORT_SYMBOL_GPL(hv_isolation_type_snp); diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index 8c905ffdba7f..ec0e5c259740 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -6,6 +6,8 @@ * Tianyu Lan */ =20 +#include +#include #include #include #include @@ -13,6 +15,114 @@ #include #include =20 +void hv_ghcb_msr_write(u64 msr, u64 value) +{ + union hv_ghcb *hv_ghcb; + void **ghcb_base; + unsigned long flags; + + if (!ms_hyperv.ghcb_base) + return; + + WARN_ON(in_nmi()); + + local_irq_save(flags); + ghcb_base =3D (void **)this_cpu_ptr(ms_hyperv.ghcb_base); + 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, value >> 32); + + 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; + + if (!ms_hyperv.ghcb_base) + return; + + WARN_ON(in_nmi()); + + local_irq_save(flags); + ghcb_base =3D (void **)this_cpu_ptr(ms_hyperv.ghcb_base); + hv_ghcb =3D (union hv_ghcb *)*ghcb_base; + if (!hv_ghcb) { + local_irq_restore(flags); + return; + } + + ghcb_set_rcx(&hv_ghcb->ghcb, msr); + if (sev_es_ghcb_hv_call_simple(&hv_ghcb->ghcb, SVM_EXIT_MSR, 0, 0)) + pr_warn("Fail to read msr via ghcb %llx.\n", msr); + else + *value =3D (u64)lower_32_bits(hv_ghcb->ghcb.save.rax) + | ((u64)lower_32_bits(hv_ghcb->ghcb.save.rdx) << 32); + local_irq_restore(flags); +} + +void hv_sint_rdmsrl_ghcb(u64 msr, u64 *value) +{ + hv_ghcb_msr_read(msr, value); +} +EXPORT_SYMBOL_GPL(hv_sint_rdmsrl_ghcb); + +void hv_sint_wrmsrl_ghcb(u64 msr, u64 value) +{ + hv_ghcb_msr_write(msr, value); + + /* Write proxy bit vua wrmsrl instruction. */ + if (msr >=3D HV_X64_MSR_SINT0 && msr <=3D HV_X64_MSR_SINT15) + wrmsrl(msr, value | 1 << 20); +} +EXPORT_SYMBOL_GPL(hv_sint_wrmsrl_ghcb); + +void hv_signal_eom_ghcb(void) +{ + hv_sint_wrmsrl_ghcb(HV_X64_MSR_EOM, 0); +} +EXPORT_SYMBOL_GPL(hv_signal_eom_ghcb); + +enum hv_isolation_type hv_get_isolation_type(void) +{ + if (!(ms_hyperv.priv_high & HV_ISOLATION)) + return HV_ISOLATION_TYPE_NONE; + return FIELD_GET(HV_ISOLATION_TYPE, ms_hyperv.isolation_config_b); +} +EXPORT_SYMBOL_GPL(hv_get_isolation_type); + +/* + * hv_is_isolation_supported - Check system runs in the Hyper-V + * isolation VM. + */ +bool hv_is_isolation_supported(void) +{ + return hv_get_isolation_type() !=3D HV_ISOLATION_TYPE_NONE; +} + +DEFINE_STATIC_KEY_FALSE(isolation_type_snp); + +/* + * hv_isolation_type_snp - Check system runs in the AMD SEV-SNP based + * isolation VM. + */ +bool hv_isolation_type_snp(void) +{ + return static_branch_unlikely(&isolation_type_snp); +} +EXPORT_SYMBOL_GPL(hv_isolation_type_snp); + /* * hv_mark_gpa_visibility - Set pages visible to host via hvcall. * diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyper= v.h index 87a386fa97f7..730985676ea3 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -30,6 +30,63 @@ static inline u64 hv_get_register(unsigned int reg) return value; } =20 +#define hv_get_sint_reg(val, reg) { \ + if (hv_isolation_type_snp()) \ + hv_get_##reg##_ghcb(&val); \ + else \ + rdmsrl(HV_X64_MSR_##reg, val); \ + } + +#define hv_set_sint_reg(val, reg) { \ + if (hv_isolation_type_snp()) \ + hv_set_##reg##_ghcb(val); \ + else \ + wrmsrl(HV_X64_MSR_##reg, val); \ + } + + +#define hv_get_simp(val) hv_get_sint_reg(val, SIMP) +#define hv_get_siefp(val) hv_get_sint_reg(val, SIEFP) + +#define hv_set_simp(val) hv_set_sint_reg(val, SIMP) +#define hv_set_siefp(val) hv_set_sint_reg(val, SIEFP) + +#define hv_get_synic_state(val) { \ + if (hv_isolation_type_snp()) \ + hv_get_synic_state_ghcb(&val); \ + else \ + rdmsrl(HV_X64_MSR_SCONTROL, val); \ + } +#define hv_set_synic_state(val) { \ + if (hv_isolation_type_snp()) \ + hv_set_synic_state_ghcb(val); \ + else \ + wrmsrl(HV_X64_MSR_SCONTROL, val); \ + } + +#define hv_get_vp_index(index) rdmsrl(HV_X64_MSR_VP_INDEX, index) + +#define hv_signal_eom() { \ + if (hv_isolation_type_snp() && \ + old_msg_type !=3D HVMSG_TIMER_EXPIRED) \ + hv_signal_eom_ghcb(); \ + else \ + wrmsrl(HV_X64_MSR_EOM, 0); \ + } + +#define hv_get_synint_state(int_num, val) { \ + if (hv_isolation_type_snp()) \ + hv_get_synint_state_ghcb(int_num, &val);\ + else \ + rdmsrl(HV_X64_MSR_SINT0 + int_num, val);\ + } +#define hv_set_synint_state(int_num, val) { \ + if (hv_isolation_type_snp()) \ + hv_set_synint_state_ghcb(int_num, val); \ + else \ + wrmsrl(HV_X64_MSR_SINT0 + int_num, val);\ + } + #define hv_get_raw_timer() rdtsc_ordered() =20 void hyperv_vector_handler(struct pt_regs *regs); @@ -193,6 +250,25 @@ int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv= _interrupt_entry *entry); int hv_mark_gpa_visibility(u16 count, const u64 pfn[], enum hv_mem_host_visibility visibility); int hv_set_mem_host_visibility(unsigned long addr, int numpages, bool visi= ble); +void hv_sint_wrmsrl_ghcb(u64 msr, u64 value); +void hv_sint_rdmsrl_ghcb(u64 msr, u64 *value); +void hv_signal_eom_ghcb(void); +void hv_ghcb_msr_write(u64 msr, u64 value); +void hv_ghcb_msr_read(u64 msr, u64 *value); + +#define hv_get_synint_state_ghcb(int_num, val) \ + hv_sint_rdmsrl_ghcb(HV_X64_MSR_SINT0 + int_num, val) +#define hv_set_synint_state_ghcb(int_num, val) \ + hv_sint_wrmsrl_ghcb(HV_X64_MSR_SINT0 + int_num, val) + +#define hv_get_SIMP_ghcb(val) hv_sint_rdmsrl_ghcb(HV_X64_MSR_SIMP, val) +#define hv_set_SIMP_ghcb(val) hv_sint_wrmsrl_ghcb(HV_X64_MSR_SIMP, val) + +#define hv_get_SIEFP_ghcb(val) hv_sint_rdmsrl_ghcb(HV_X64_MSR_SIEFP, val) +#define hv_set_SIEFP_ghcb(val) hv_sint_wrmsrl_ghcb(HV_X64_MSR_SIEFP, val) + +#define hv_get_synic_state_ghcb(val) hv_sint_rdmsrl_ghcb(HV_X64_MSR_SCONTR= OL, val) +#define hv_set_synic_state_ghcb(val) hv_sint_wrmsrl_ghcb(HV_X64_MSR_SCONTR= OL, val) #else /* CONFIG_HYPERV */ static inline void hyperv_init(void) {} static inline void hyperv_setup_mmu_ops(void) {} @@ -209,9 +285,9 @@ static inline int hyperv_flush_guest_mapping_range(u64 = as, { return -1; } +static inline void hv_signal_eom_ghcb(void) { }; #endif /* CONFIG_HYPERV */ =20 - #include =20 #endif diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index fa5cd05d3b5b..81beb2a8031b 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -81,6 +81,9 @@ static __always_inline void sev_es_nmi_complete(void) __sev_es_nmi_complete(); } extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd); +extern enum es_result sev_es_ghcb_hv_call_simple(struct ghcb *ghcb, + u64 exit_code, u64 exit_info_1, + u64 exit_info_2); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c index 2b7f396ef1a5..3633f871ac1e 100644 --- a/arch/x86/kernel/cpu/mshyperv.c +++ b/arch/x86/kernel/cpu/mshyperv.c @@ -318,6 +318,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/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..59f7173c4d9f 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); @@ -173,10 +181,17 @@ void hv_synic_free(void) for_each_present_cpu(cpu) { struct hv_per_cpu_context *hv_cpu =3D per_cpu_ptr(hv_context.cpu_context, cpu); + free_page((unsigned long)hv_cpu->post_msg_page); + + /* + * Synic message and event pages are allocated by paravisor. + * Skip free these pages here. + */ + if (hv_isolation_type_snp()) + continue; =20 free_page((unsigned long)hv_cpu->synic_event_page); free_page((unsigned long)hv_cpu->synic_message_page); - free_page((unsigned long)hv_cpu->post_msg_page); } =20 kfree(hv_context.hv_numa_map); @@ -199,26 +214,43 @@ void hv_synic_enable_regs(unsigned int cpu) union hv_synic_scontrol sctrl; =20 /* Setup the Synic's message page */ - simp.as_uint64 =3D hv_get_register(HV_REGISTER_SIMP); + hv_get_simp(simp.as_uint64); simp.simp_enabled =3D 1; - simp.base_simp_gpa =3D virt_to_phys(hv_cpu->synic_message_page) - >> HV_HYP_PAGE_SHIFT; =20 - hv_set_register(HV_REGISTER_SIMP, simp.as_uint64); + if (hv_isolation_type_snp()) { + hv_cpu->synic_message_page + =3D memremap(simp.base_simp_gpa << HV_HYP_PAGE_SHIFT, + HV_HYP_PAGE_SIZE, MEMREMAP_WB); + if (!hv_cpu->synic_message_page) + pr_err("Fail to map syinc message page.\n"); + } else { + simp.base_simp_gpa =3D virt_to_phys(hv_cpu->synic_message_page) + >> HV_HYP_PAGE_SHIFT; + } + + hv_set_simp(simp.as_uint64); =20 /* Setup the Synic's event page */ - siefp.as_uint64 =3D hv_get_register(HV_REGISTER_SIEFP); + hv_get_siefp(siefp.as_uint64); siefp.siefp_enabled =3D 1; - siefp.base_siefp_gpa =3D virt_to_phys(hv_cpu->synic_event_page) - >> HV_HYP_PAGE_SHIFT; =20 - hv_set_register(HV_REGISTER_SIEFP, siefp.as_uint64); + if (hv_isolation_type_snp()) { + hv_cpu->synic_event_page =3D + memremap(siefp.base_siefp_gpa << HV_HYP_PAGE_SHIFT, + HV_HYP_PAGE_SIZE, MEMREMAP_WB); + + if (!hv_cpu->synic_event_page) + pr_err("Fail to map syinc event page.\n"); + } else { + siefp.base_siefp_gpa =3D virt_to_phys(hv_cpu->synic_event_page) + >> HV_HYP_PAGE_SHIFT; + } + hv_set_siefp(siefp.as_uint64); =20 /* Setup the shared SINT. */ if (vmbus_irq !=3D -1) enable_percpu_irq(vmbus_irq, 0); - shared_sint.as_uint64 =3D hv_get_register(HV_REGISTER_SINT0 + - VMBUS_MESSAGE_SINT); + hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64); =20 shared_sint.vector =3D vmbus_interrupt; shared_sint.masked =3D false; @@ -233,14 +265,12 @@ void hv_synic_enable_regs(unsigned int cpu) #else shared_sint.auto_eoi =3D 0; #endif - hv_set_register(HV_REGISTER_SINT0 + VMBUS_MESSAGE_SINT, - shared_sint.as_uint64); + hv_set_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64); =20 /* Enable the global synic bit */ - sctrl.as_uint64 =3D hv_get_register(HV_REGISTER_SCONTROL); + hv_get_synic_state(sctrl.as_uint64); sctrl.enable =3D 1; - - hv_set_register(HV_REGISTER_SCONTROL, sctrl.as_uint64); + hv_set_synic_state(sctrl.as_uint64); } =20 int hv_synic_init(unsigned int cpu) @@ -257,37 +287,50 @@ int hv_synic_init(unsigned int cpu) */ void hv_synic_disable_regs(unsigned int cpu) { + struct hv_per_cpu_context *hv_cpu + =3D per_cpu_ptr(hv_context.cpu_context, cpu); union hv_synic_sint shared_sint; union hv_synic_simp simp; union hv_synic_siefp siefp; union hv_synic_scontrol sctrl; =20 - shared_sint.as_uint64 =3D hv_get_register(HV_REGISTER_SINT0 + - VMBUS_MESSAGE_SINT); - + hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64); shared_sint.masked =3D 1; + hv_set_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64); + =20 /* Need to correctly cleanup in the case of SMP!!! */ /* Disable the interrupt */ - hv_set_register(HV_REGISTER_SINT0 + VMBUS_MESSAGE_SINT, - shared_sint.as_uint64); + hv_get_simp(simp.as_uint64); =20 - simp.as_uint64 =3D hv_get_register(HV_REGISTER_SIMP); + /* + * In Isolation VM, sim and sief pages are allocated by + * paravisor. These pages also will be used by kdump + * kernel. So just reset enable bit here and keep page + * addresses. + */ simp.simp_enabled =3D 0; - simp.base_simp_gpa =3D 0; + if (hv_isolation_type_snp()) + memunmap(hv_cpu->synic_message_page); + else + simp.base_simp_gpa =3D 0; =20 - hv_set_register(HV_REGISTER_SIMP, simp.as_uint64); + hv_set_simp(simp.as_uint64); =20 - siefp.as_uint64 =3D hv_get_register(HV_REGISTER_SIEFP); + hv_get_siefp(siefp.as_uint64); siefp.siefp_enabled =3D 0; - siefp.base_siefp_gpa =3D 0; =20 - hv_set_register(HV_REGISTER_SIEFP, siefp.as_uint64); + if (hv_isolation_type_snp()) + memunmap(hv_cpu->synic_event_page); + else + siefp.base_siefp_gpa =3D 0; + + hv_set_siefp(siefp.as_uint64); =20 /* Disable the global synic bit */ - sctrl.as_uint64 =3D hv_get_register(HV_REGISTER_SCONTROL); + hv_get_synic_state(sctrl.as_uint64); sctrl.enable =3D 0; - hv_set_register(HV_REGISTER_SCONTROL, sctrl.as_uint64); + hv_set_synic_state(sctrl.as_uint64); =20 if (vmbus_irq !=3D -1) disable_percpu_irq(vmbus_irq); diff --git a/include/asm-generic/mshyperv.h b/include/asm-generic/mshyperv.h index 079988ed45b9..90dac369a2dc 100644 --- a/include/asm-generic/mshyperv.h +++ b/include/asm-generic/mshyperv.h @@ -23,9 +23,16 @@ #include #include #include +#include +#include #include +#include #include =20 +union hv_ghcb { + struct ghcb ghcb; +} __packed __aligned(PAGE_SIZE); + struct ms_hyperv_info { u32 features; u32 priv_high; @@ -45,7 +52,7 @@ struct ms_hyperv_info { u32 Reserved12 : 20; }; }; - void __percpu **ghcb_base; + union hv_ghcb __percpu **ghcb_base; u64 shared_gpa_boundary; }; extern struct ms_hyperv_info ms_hyperv; @@ -55,6 +62,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) @@ -149,7 +157,7 @@ static inline void vmbus_signal_eom(struct hv_message *= msg, u32 old_msg_type) * possibly deliver another msg from the * hypervisor */ - hv_set_register(HV_REGISTER_EOM, 0); + hv_signal_eom(); } } =20 --=20 2.25.1 From nobody Sun Apr 28 05:50:41 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=1628531831; cv=none; d=zohomail.com; s=zohoarc; b=XVUPp4soAfmeUikjcut9rx/ESjzqiTrJprBp8FM3ZD4XIi3zK9lpP/RoOK5FWcab8RXx/OLP7VwJEoZGrtIXSfm+S8T52tkSc//yG018u+ZTY2CzWM3xdDf8cG/2O+7XoWm75tNCKFqG/2nAYRejCd9uwxqny2AaXEfM+sRd1so= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628531831; 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=cBjjetB5scRrkX+A3+C6cOQnjDWDoYH3LrzmvOorAR4=; b=ICJiBofTuv6KV4+3CkwSh8mQVTSMgIrk2yIwCj7OgMv+iaNWmUyk6d15EUu/JWf6Tnaz2raawviLrh/5yo4agncFAtTNKnHMOYkIGXphlnrsoj/wV+JbgRcYjdlf1AcmtaeH6iAy+6ahzIwXsUlvVTkiAyYnDgyp3LJ0Gg4YEac= 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 162853183137330.467620987912596; Mon, 9 Aug 2021 10:57:11 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.165124.301822 (Exim 4.92) (envelope-from ) id 1mD9WF-0002uK-0O; Mon, 09 Aug 2021 17:56:59 +0000 Received: by outflank-mailman (output) from mailman id 165124.301822; Mon, 09 Aug 2021 17:56:58 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9WE-0002ts-ST; Mon, 09 Aug 2021 17:56:58 +0000 Received: by outflank-mailman (input) for mailman id 165124; Mon, 09 Aug 2021 17:56: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 1mD9WD-0000Aq-1q for xen-devel@lists.xenproject.org; Mon, 09 Aug 2021 17:56:57 +0000 Received: from mail-pj1-x102c.google.com (unknown [2607:f8b0:4864:20::102c]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 90c37b74-38c8-44e2-bff4-9bc476e03eed; Mon, 09 Aug 2021 17:56:39 +0000 (UTC) Received: by mail-pj1-x102c.google.com with SMTP id t7-20020a17090a5d87b029017807007f23so38266pji.5 for ; Mon, 09 Aug 2021 10:56:39 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:2:106e:6ed1:5da1:2ac4]) by smtp.gmail.com with ESMTPSA id x14sm20589708pfa.127.2021.08.09.10.56.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Aug 2021 10:56: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: 90c37b74-38c8-44e2-bff4-9bc476e03eed DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=cBjjetB5scRrkX+A3+C6cOQnjDWDoYH3LrzmvOorAR4=; b=sPPE5pg+476PNmoE/TVK9YzFe951Z9jTppBWPcaFrZmVNeun3j9z8RbruIahTChh5N hEInDTDrNiP/I/niKkzacWs6OLLAsHmcla70CCGlIJ1HQV9LroNONp4sx+CMWy/eqqvg h5G5inu5ABmtLeo48Wcp43Khu9GMprPM75F66YBgTsOCYpxFGFLEosc/WLMs/Erm2O1e YcNPoQ8b8lQdggxH7XMskaKzBFuDbJ8YzPlavBLK5sRnDl9Fj2ZNAa+Msa4ednJS24ED 5/zZg0s0B6Fz1VY7rV6Rio2ZEjpsAc8CQvNz1HGrOOEKtMbeB6z4xNpL74gadaGRvi5x 5TuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cBjjetB5scRrkX+A3+C6cOQnjDWDoYH3LrzmvOorAR4=; b=JsI3V8EGOE24+mXakjr3lHHJvct733de3evjaeY/XXOMpVjj0TJhhTiCJw2Ef78Vha seigOndfNTdPcMs3KouJTVRerv9y5CO/IRiQmB1a2sgTrwrpULfWBzwrjWu0cxSz4VmL 9s/jADkuxobr3TZinu5wBsfjMEUDk52OKNON6w1v9/kpqJD53M22qdekT4GA1pg9er37 mzAgDfsNmLHdUlmCvgd4MED5+iJJ4NizbsPV356TXeZTMxkaQhkzSZAnoARphgHUPHrV B+rgvthDCocpkse5e4iaJBgjDjJ26SP9B62eVDtQfU23bo1y67HDQmuTV/h+sbHOwBLH n/9w== X-Gm-Message-State: AOAM531GMnceov3kp9Ep5HmaW180/iamx+qMSzDSz6eBNHmm22gvq671 hk8gDXvy94yq8L5iFBhv/Y8= X-Google-Smtp-Source: ABdhPJwaFTKcmp8xUnfNsloH8PrR5NG3CCvOenHxNhKVdulQOVvanqltOPnNZ4LLYIrcN3poiszICw== X-Received: by 2002:a17:90b:1e05:: with SMTP id pg5mr365384pjb.12.1628531799300; Mon, 09 Aug 2021 10:56: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, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, ardb@kernel.org, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, saravanand@fb.com, krish.sadhukhan@oracle.com, aneesh.kumar@linux.ibm.com, xen-devel@lists.xenproject.org, rientjes@google.com, hannes@cmpxchg.org, tj@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V3 06/13] HV: Add ghcb hvcall support for SNP VM Date: Mon, 9 Aug 2021 13:56:10 -0400 Message-Id: <20210809175620.720923-7-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210809175620.720923-1-ltykernel@gmail.com> References: <20210809175620.720923-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628531832909100005 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan Hyper-V 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 --- arch/x86/hyperv/ivm.c | 43 +++++++++++++++++++++++++++++++++ arch/x86/include/asm/mshyperv.h | 1 + drivers/hv/connection.c | 6 ++++- drivers/hv/hv.c | 8 +++++- include/asm-generic/mshyperv.h | 29 ++++++++++++++++++++++ 5 files changed, 85 insertions(+), 2 deletions(-) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index ec0e5c259740..c13ec5560d73 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -15,6 +15,49 @@ #include #include =20 +#define GHCB_USAGE_HYPERV_CALL 1 + +u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_si= ze) +{ + union hv_ghcb *hv_ghcb; + void **ghcb_base; + unsigned long flags; + + if (!ms_hyperv.ghcb_base) + return -EFAULT; + + WARN_ON(in_nmi()); + + local_irq_save(flags); + ghcb_base =3D (void **)this_cpu_ptr(ms_hyperv.ghcb_base); + hv_ghcb =3D (union hv_ghcb *)*ghcb_base; + if (!hv_ghcb) { + local_irq_restore(flags); + return -EFAULT; + } + + hv_ghcb->ghcb.protocol_version =3D GHCB_PROTOCOL_MAX; + hv_ghcb->ghcb.ghcb_usage =3D GHCB_USAGE_HYPERV_CALL; + + hv_ghcb->hypercall.outputgpa =3D (u64)output; + hv_ghcb->hypercall.hypercallinput.asuint64 =3D 0; + hv_ghcb->hypercall.hypercallinput.callcode =3D control; + + if (input_size) + memcpy(hv_ghcb->hypercall.hypercalldata, input, input_size); + + VMGEXIT(); + + hv_ghcb->ghcb.ghcb_usage =3D 0xffffffff; + memset(hv_ghcb->ghcb.save.valid_bitmap, 0, + sizeof(hv_ghcb->ghcb.save.valid_bitmap)); + + local_irq_restore(flags); + + return hv_ghcb->hypercall.hypercalloutput.callstatus; +} +EXPORT_SYMBOL_GPL(hv_ghcb_hypercall); + void hv_ghcb_msr_write(u64 msr, u64 value) { union hv_ghcb *hv_ghcb; diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyper= v.h index 730985676ea3..a30c60f189a3 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -255,6 +255,7 @@ void hv_sint_rdmsrl_ghcb(u64 msr, u64 *value); void hv_signal_eom_ghcb(void); void hv_ghcb_msr_write(u64 msr, u64 value); void hv_ghcb_msr_read(u64 msr, u64 *value); +u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_si= ze); =20 #define hv_get_synint_state_ghcb(int_num, val) \ hv_sint_rdmsrl_ghcb(HV_X64_MSR_SINT0 + int_num, val) diff --git a/drivers/hv/connection.c b/drivers/hv/connection.c index 5e479d54918c..6d315c1465e0 100644 --- a/drivers/hv/connection.c +++ b/drivers/hv/connection.c @@ -447,6 +447,10 @@ void vmbus_set_event(struct vmbus_channel *channel) =20 ++channel->sig_events; =20 - hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event); + if (hv_isolation_type_snp()) + hv_ghcb_hypercall(HVCALL_SIGNAL_EVENT, &channel->sig_event, + NULL, sizeof(u64)); + else + hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event); } EXPORT_SYMBOL_GPL(vmbus_set_event); diff --git a/drivers/hv/hv.c b/drivers/hv/hv.c index 59f7173c4d9f..e5c9fc467893 100644 --- a/drivers/hv/hv.c +++ b/drivers/hv/hv.c @@ -98,7 +98,13 @@ int hv_post_message(union hv_connection_id connection_id, aligned_msg->payload_size =3D payload_size; memcpy((void *)aligned_msg->payload, payload, payload_size); =20 - status =3D hv_do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL); + if (hv_isolation_type_snp()) + status =3D hv_ghcb_hypercall(HVCALL_POST_MESSAGE, + (void *)aligned_msg, NULL, + sizeof(struct hv_input_post_message)); + else + status =3D hv_do_hypercall(HVCALL_POST_MESSAGE, + aligned_msg, NULL); =20 /* Preemption must remain disabled until after the hypercall * so some other thread can't get scheduled onto this cpu and diff --git a/include/asm-generic/mshyperv.h b/include/asm-generic/mshyperv.h index 90dac369a2dc..400181b855c1 100644 --- a/include/asm-generic/mshyperv.h +++ b/include/asm-generic/mshyperv.h @@ -31,6 +31,35 @@ =20 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(PAGE_SIZE); =20 struct ms_hyperv_info { --=20 2.25.1 From nobody Sun Apr 28 05:50:41 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=1628531837; cv=none; d=zohomail.com; s=zohoarc; b=WrOkED3XzUr8r6ddPpmQzURc3aHo3+iIlFCczNG+ZXQwgsa+10tIHhTfLF2vP+DsBRYaOAbwUu7AyVQS0Q+jEITi4+OUMybey9cgk3xsoVxRvXB5H/VNn7w3titk7GRdwVPUZTPXdThUgAa1qPtWe5zKxBK9BwThso43xZt8JT0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628531837; 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=xXQzWRnOQGUWqB+ZM33hvekRvxcc1wpDyoNFD4aToCk=; b=iR2cb/dsfQ/eSVZYIs5tSoyEbGbgrf9+PQ4SCSuCEvximoZa6UtlxORmCZhCpWIa9smut/Edm+noX6mIr9hDhlB6G/q/p1rytoY10oTOaST+8HtaVEtBUfWDa7jUm73gqqoxFJgRLemgcfxH2bxjFW4HMz4tgfzE+oP7y+0hnM8= 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 1628531837645663.7537142959067; Mon, 9 Aug 2021 10:57:17 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.165127.301833 (Exim 4.92) (envelope-from ) id 1mD9WJ-0003Tk-B8; Mon, 09 Aug 2021 17:57:03 +0000 Received: by outflank-mailman (output) from mailman id 165127.301833; Mon, 09 Aug 2021 17:57:03 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9WJ-0003TZ-7E; Mon, 09 Aug 2021 17:57:03 +0000 Received: by outflank-mailman (input) for mailman id 165127; Mon, 09 Aug 2021 17:57:02 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9WI-0000Aq-1t for xen-devel@lists.xenproject.org; Mon, 09 Aug 2021 17:57:02 +0000 Received: from mail-pl1-x630.google.com (unknown [2607:f8b0:4864:20::630]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 957cd112-b750-4318-900f-9c4e069bf4ba; Mon, 09 Aug 2021 17:56:42 +0000 (UTC) Received: by mail-pl1-x630.google.com with SMTP id d1so17335556pll.1 for ; Mon, 09 Aug 2021 10:56:42 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:2:106e:6ed1:5da1:2ac4]) by smtp.gmail.com with ESMTPSA id x14sm20589708pfa.127.2021.08.09.10.56.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Aug 2021 10:56: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: 957cd112-b750-4318-900f-9c4e069bf4ba DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xXQzWRnOQGUWqB+ZM33hvekRvxcc1wpDyoNFD4aToCk=; b=LurLNOZQESLgb2dqtw6YoKkpQTuonm6QicLB/Fe2mtwglsCTfoHOf3uEMdepR4rXd5 BxD3P8E7JwFvZ647syAEXZk5Beq3I58qbGMIepNAc4WOW/M1Hta/7k2gUy1uMfivQN1B hQikaNlNZUEg/opx9pXNvQYoOdVZB9fr8hyM49Mzvuma62UGAHYNbFJ4x23T2dGCfLQb gOxnFM+3C1+9HtmdIsW1iwkVXvSWf9rPSJptmTfJ+XU4dWLl6/oRuePqUhxuy9sWbDNu DSF0RRyziVdvK9QoFK0wSFcerAzOXu0vhd9ifHwf+dbhjHbAL/IOsCh8Rcly7h5kmTpK I2xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xXQzWRnOQGUWqB+ZM33hvekRvxcc1wpDyoNFD4aToCk=; b=IFkDPbhR2uXOhgyoPgPnx9CiGTCA05jI3hsBRTDIZNsD9EjHQdeGU2uAejf/OdI2DA Y5CEMTfMRBjzqc9gygWZS4qrqD7LjGMYBfiLg6TkHuJ6nY0cU7UGEJAtKD/fCHpWq6FN oU4Dz1oVSrp0zz8Gmgt+EGykgA/mNr4aCVnXeqE8rHqRkRi/IJPx0GPrHpZSGGwuY3ob Bgu6P4mid3N98uGIkHhqMV6lUyXTttPTwGm2TBrW8D8ofRMppcR2jfBa37b6k4pyNjC1 3Wf/p/AK3RQ2VpQ8EcldxBLViqffEMB8PMHZZuW+lSdOvKRcG8cE7ntlPuL8JZlKwhvY 43fA== X-Gm-Message-State: AOAM533BCnV07NVGtjk9CMYF4fBRy1F/OgulH/+n8Z/qDC9PQ4RcwvWI tzQfzFr/XfQi4Xru2oFtyB8= X-Google-Smtp-Source: ABdhPJwgFDEkqyIiQx3welTNcCdKRzJlcPxzyOKGPnHMbJ+p2CRecMC5lj4V8nqr3JilPwbucKAnLA== X-Received: by 2002:a63:b950:: with SMTP id v16mr73150pgo.328.1628531801529; Mon, 09 Aug 2021 10:56:41 -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, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, ardb@kernel.org, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, saravanand@fb.com, krish.sadhukhan@oracle.com, aneesh.kumar@linux.ibm.com, xen-devel@lists.xenproject.org, rientjes@google.com, hannes@cmpxchg.org, tj@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V3 07/13] HV/Vmbus: Add SNP support for VMbus channel initiate message Date: Mon, 9 Aug 2021 13:56:11 -0400 Message-Id: <20210809175620.720923-8-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210809175620.720923-1-ltykernel@gmail.com> References: <20210809175620.720923-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628531839118100003 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_va to store the remap address and unmap these va when disconnect vmbus. Signed-off-by: Tianyu Lan --- Change since v1: * Not remap monitor pages in the non-SNP isolation VM. --- drivers/hv/connection.c | 65 +++++++++++++++++++++++++++++++++++++++ drivers/hv/hyperv_vmbus.h | 1 + 2 files changed, 66 insertions(+) diff --git a/drivers/hv/connection.c b/drivers/hv/connection.c index 6d315c1465e0..bf0ac3167bd2 100644 --- a/drivers/hv/connection.c +++ b/drivers/hv/connection.c @@ -19,6 +19,7 @@ #include #include #include +#include #include =20 #include "hyperv_vmbus.h" @@ -104,6 +105,12 @@ int vmbus_negotiate_version(struct vmbus_channel_msgin= fo *msginfo, u32 version) =20 msg->monitor_page1 =3D virt_to_phys(vmbus_connection.monitor_pages[0]); msg->monitor_page2 =3D virt_to_phys(vmbus_connection.monitor_pages[1]); + + if (hv_isolation_type_snp()) { + msg->monitor_page1 +=3D ms_hyperv.shared_gpa_boundary; + msg->monitor_page2 +=3D ms_hyperv.shared_gpa_boundary; + } + msg->target_vcpu =3D hv_cpu_number_to_vp_number(VMBUS_CONNECT_CPU); =20 /* @@ -148,6 +155,31 @@ int vmbus_negotiate_version(struct vmbus_channel_msgin= fo *msginfo, u32 version) return -ECONNREFUSED; } =20 + if (hv_isolation_type_snp()) { + vmbus_connection.monitor_pages_va[0] + =3D vmbus_connection.monitor_pages[0]; + vmbus_connection.monitor_pages[0] + =3D memremap(msg->monitor_page1, HV_HYP_PAGE_SIZE, + MEMREMAP_WB); + if (!vmbus_connection.monitor_pages[0]) + return -ENOMEM; + + vmbus_connection.monitor_pages_va[1] + =3D vmbus_connection.monitor_pages[1]; + vmbus_connection.monitor_pages[1] + =3D memremap(msg->monitor_page2, HV_HYP_PAGE_SIZE, + MEMREMAP_WB); + if (!vmbus_connection.monitor_pages[1]) { + memunmap(vmbus_connection.monitor_pages[0]); + return -ENOMEM; + } + + memset(vmbus_connection.monitor_pages[0], 0x00, + HV_HYP_PAGE_SIZE); + memset(vmbus_connection.monitor_pages[1], 0x00, + HV_HYP_PAGE_SIZE); + } + return ret; } =20 @@ -159,6 +191,7 @@ int vmbus_connect(void) struct vmbus_channel_msginfo *msginfo =3D NULL; int i, ret =3D 0; __u32 version; + u64 pfn[2]; =20 /* Initialize the vmbus connection */ vmbus_connection.conn_state =3D CONNECTING; @@ -216,6 +249,16 @@ int vmbus_connect(void) goto cleanup; } =20 + if (hv_is_isolation_supported()) { + pfn[0] =3D virt_to_hvpfn(vmbus_connection.monitor_pages[0]); + pfn[1] =3D virt_to_hvpfn(vmbus_connection.monitor_pages[1]); + if (hv_mark_gpa_visibility(2, pfn, + VMBUS_PAGE_VISIBLE_READ_WRITE)) { + ret =3D -EFAULT; + goto cleanup; + } + } + msginfo =3D kzalloc(sizeof(*msginfo) + sizeof(struct vmbus_channel_initiate_contact), GFP_KERNEL); @@ -284,6 +327,8 @@ int vmbus_connect(void) =20 void vmbus_disconnect(void) { + u64 pfn[2]; + /* * First send the unload request to the host. */ @@ -303,6 +348,26 @@ void vmbus_disconnect(void) vmbus_connection.int_page =3D NULL; } =20 + if (hv_is_isolation_supported()) { + if (vmbus_connection.monitor_pages_va[0]) { + memunmap(vmbus_connection.monitor_pages[0]); + vmbus_connection.monitor_pages[0] + =3D vmbus_connection.monitor_pages_va[0]; + vmbus_connection.monitor_pages_va[0] =3D NULL; + } + + if (vmbus_connection.monitor_pages_va[1]) { + memunmap(vmbus_connection.monitor_pages[1]); + vmbus_connection.monitor_pages[1] + =3D vmbus_connection.monitor_pages_va[1]; + vmbus_connection.monitor_pages_va[1] =3D NULL; + } + + pfn[0] =3D virt_to_hvpfn(vmbus_connection.monitor_pages[0]); + pfn[1] =3D virt_to_hvpfn(vmbus_connection.monitor_pages[1]); + hv_mark_gpa_visibility(2, pfn, VMBUS_PAGE_NOT_VISIBLE); + } + hv_free_hyperv_page((unsigned long)vmbus_connection.monitor_pages[0]); hv_free_hyperv_page((unsigned long)vmbus_connection.monitor_pages[1]); vmbus_connection.monitor_pages[0] =3D NULL; diff --git a/drivers/hv/hyperv_vmbus.h b/drivers/hv/hyperv_vmbus.h index 42f3d9d123a1..40bc0eff6665 100644 --- a/drivers/hv/hyperv_vmbus.h +++ b/drivers/hv/hyperv_vmbus.h @@ -240,6 +240,7 @@ struct vmbus_connection { * is child->parent notification */ struct hv_monitor_page *monitor_pages[2]; + void *monitor_pages_va[2]; struct list_head chn_msg_list; spinlock_t channelmsg_lock; =20 --=20 2.25.1 From nobody Sun Apr 28 05:50:41 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=1628531840; cv=none; d=zohomail.com; s=zohoarc; b=BXZp9/PfWMwvrgntnVqkaR8DdJuSfBZ/5hq6uCOlVDcjqOSjitY6IrJMOUEP/l3OqRbXpKN1DsEMPbcJt+GQi4e8ixvOTl4VPkG7c0JNA0UoapGpm7FaMhX5/5vXJiEogQRnkhkp+vgsbVSew/X+ri6Jb+TZhWHSLzp9+FJ4ejc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628531840; 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=+b7Gns6iR6iBk57ZYV5BIIcLDAAVsJ8v7NH3zL5Mg1I=; b=GyXzzFWxg0hI9wTuxuaRLtkiWT7ub2eWFxL8O7827tuni74k7Jm0hdwn7bxHBta22aMDMmM0KDJyBkgWVKOCJwQ1dINjzDkpuqZyxjDi6tIbOKc/6SMpZJFg3cwY2SsNxy6AfW3KcWSNsDnNLwfsh2GkK1dU5ecCODLvauQovFg= 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 1628531840884188.58959195184832; Mon, 9 Aug 2021 10:57:20 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.165133.301844 (Exim 4.92) (envelope-from ) id 1mD9WO-00046S-KY; Mon, 09 Aug 2021 17:57:08 +0000 Received: by outflank-mailman (output) from mailman id 165133.301844; Mon, 09 Aug 2021 17:57:08 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9WO-00046J-Ft; Mon, 09 Aug 2021 17:57:08 +0000 Received: by outflank-mailman (input) for mailman id 165133; Mon, 09 Aug 2021 17:57:07 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9WN-0000Aq-22 for xen-devel@lists.xenproject.org; Mon, 09 Aug 2021 17:57:07 +0000 Received: from mail-pj1-x102a.google.com (unknown [2607:f8b0:4864:20::102a]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id d799969d-565d-4d8b-bb4c-072978d5fdb9; Mon, 09 Aug 2021 17:56:44 +0000 (UTC) Received: by mail-pj1-x102a.google.com with SMTP id ca5so29226119pjb.5 for ; Mon, 09 Aug 2021 10:56:44 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:2:106e:6ed1:5da1:2ac4]) by smtp.gmail.com with ESMTPSA id x14sm20589708pfa.127.2021.08.09.10.56.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Aug 2021 10:56:43 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: d799969d-565d-4d8b-bb4c-072978d5fdb9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+b7Gns6iR6iBk57ZYV5BIIcLDAAVsJ8v7NH3zL5Mg1I=; b=aU1nKcQ+nDmPFPy36XWpKTjwCuvMuOeOtKNuk1xPVGyxoxuMnaSYJneUW5xBdbS4mF EZRJlDW66/oI7E2Wx1IyV4mhOHU6VQLaysOdIZbmSWHfd5VhmRrwCUwrJRyfkyOnvk8c DABDw5o5QtCXWs7FVD6Yq7Noy7Bsts5MhfH7PiPCCCJ7RsIqAKrZykyMiDF/7fbZuHNb 1vMOIuVj+np2mkftpmkIY+gc57uZn61TrGeij/c4Wr9ZmZ2meUgtSPOJJW3jk0oHauD7 kGA8EGt3TprQFQB2ozu34RJbI1rKaUnojprdLwAyt6vGvk8hoDFLsG1rF5/di6hAastF RUxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+b7Gns6iR6iBk57ZYV5BIIcLDAAVsJ8v7NH3zL5Mg1I=; b=OjffJrDc8BSVBDc6whn8cPD+AX5gbXkNKU5A216erj7RzBDhSSflQnXpcXgsYJ9D5u YKJghZyx5i9j3rG6DfcKenkl6zO6dGwOzIfjSllWxKqM/55dAvBBXUuCeeD1mBFLLRlb 9dm3nNXT/SX+rxyUXWVPsUJ2VldCJt/NLw+PKAdhBdxee11d16mt0gWCa36LL99dJCXL bV84Knh6Mi7f2q0VAeU+I8hwBtahMihzHkQkJKGFio+C3i38KO/4FYdYf/tSF+Ek6G5b VCs1poq7H0auQi2s/P+dYXl7Q53otiwUbZ8p1aa9m+fWZYv3Co8axSHk8hvVtEXIcHmK VvDw== X-Gm-Message-State: AOAM531YxwolD+/h/qfOT4f5oNn2hkA7oaOd8qG3rT2q9AKm+sZf/KFi YTs00L5cVOHPRvu/ssqYOIM= X-Google-Smtp-Source: ABdhPJyE8zgrTkf5TWCMZX5Y6wi/PvPkIAsiw37aMcRj/e7Da+SgSl/RYbFUmBp26CFz8LzJX3R2bw== X-Received: by 2002:a63:ff4d:: with SMTP id s13mr397127pgk.237.1628531803843; Mon, 09 Aug 2021 10:56:43 -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, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, ardb@kernel.org, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, saravanand@fb.com, krish.sadhukhan@oracle.com, aneesh.kumar@linux.ibm.com, xen-devel@lists.xenproject.org, rientjes@google.com, hannes@cmpxchg.org, tj@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V3 08/13] HV/Vmbus: Initialize VMbus ring buffer for Isolation VM Date: Mon, 9 Aug 2021 13:56:12 -0400 Message-Id: <20210809175620.720923-9-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210809175620.720923-1-ltykernel@gmail.com> References: <20210809175620.720923-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628531841645100001 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 SNP support. This patch is to map the ring buffer address in extra address space via ioremap(). HV host visibility hvcall smears data in the ring buffer and so reset the ring buffer memory to zero after calling visibility hvcall. Signed-off-by: Tianyu Lan --- drivers/hv/Kconfig | 1 + drivers/hv/channel.c | 10 +++++ drivers/hv/hyperv_vmbus.h | 2 + drivers/hv/ring_buffer.c | 84 ++++++++++++++++++++++++++++++--------- 4 files changed, 79 insertions(+), 18 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 4c4717c26240..60ef881a700c 100644 --- a/drivers/hv/channel.c +++ b/drivers/hv/channel.c @@ -712,6 +712,16 @@ static int __vmbus_open(struct vmbus_channel *newchann= el, if (err) goto error_clean_ring; =20 + err =3D hv_ringbuffer_post_init(&newchannel->outbound, + page, send_pages); + if (err) + goto error_free_gpadl; + + err =3D hv_ringbuffer_post_init(&newchannel->inbound, + &page[send_pages], recv_pages); + 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/hyperv_vmbus.h b/drivers/hv/hyperv_vmbus.h index 40bc0eff6665..15cd23a561f3 100644 --- a/drivers/hv/hyperv_vmbus.h +++ b/drivers/hv/hyperv_vmbus.h @@ -172,6 +172,8 @@ extern int hv_synic_cleanup(unsigned int cpu); /* Interface */ =20 void hv_ringbuffer_pre_init(struct vmbus_channel *channel); +int hv_ringbuffer_post_init(struct hv_ring_buffer_info *ring_info, + struct page *pages, u32 page_cnt); =20 int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, struct page *pages, u32 pagecnt, u32 max_pkt_size); diff --git a/drivers/hv/ring_buffer.c b/drivers/hv/ring_buffer.c index 2aee356840a2..d4f93fca1108 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 @@ -179,43 +181,89 @@ void hv_ringbuffer_pre_init(struct vmbus_channel *cha= nnel) mutex_init(&channel->outbound.ring_buffer_mutex); } =20 -/* Initialize the ring buffer. */ -int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, - struct page *pages, u32 page_cnt, u32 max_pkt_size) +int hv_ringbuffer_post_init(struct hv_ring_buffer_info *ring_info, + struct page *pages, u32 page_cnt) { + u64 physic_addr =3D page_to_pfn(pages) << PAGE_SHIFT; + unsigned long *pfns_wraparound; + void *vaddr; int i; - struct page **pages_wraparound; =20 - BUILD_BUG_ON((sizeof(struct hv_ring_buffer) !=3D PAGE_SIZE)); + if (!hv_isolation_type_snp()) + return 0; + + physic_addr +=3D ms_hyperv.shared_gpa_boundary; =20 /* * First page holds struct hv_ring_buffer, do wraparound mapping for * the rest. */ - pages_wraparound =3D kcalloc(page_cnt * 2 - 1, sizeof(struct page *), + pfns_wraparound =3D kcalloc(page_cnt * 2 - 1, sizeof(unsigned long), GFP_KERNEL); - if (!pages_wraparound) + if (!pfns_wraparound) return -ENOMEM; =20 - pages_wraparound[0] =3D pages; + pfns_wraparound[0] =3D physic_addr >> PAGE_SHIFT; for (i =3D 0; i < 2 * (page_cnt - 1); i++) - pages_wraparound[i + 1] =3D &pages[i % (page_cnt - 1) + 1]; - - ring_info->ring_buffer =3D (struct hv_ring_buffer *) - vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, PAGE_KERNEL); - - kfree(pages_wraparound); + pfns_wraparound[i + 1] =3D (physic_addr >> PAGE_SHIFT) + + i % (page_cnt - 1) + 1; =20 - - if (!ring_info->ring_buffer) + vaddr =3D vmap_pfn(pfns_wraparound, page_cnt * 2 - 1, PAGE_KERNEL_IO); + kfree(pfns_wraparound); + if (!vaddr) return -ENOMEM; =20 - ring_info->ring_buffer->read_index =3D - ring_info->ring_buffer->write_index =3D 0; + /* Clean memory after setting host visibility. */ + memset((void *)vaddr, 0x00, page_cnt * PAGE_SIZE); + + ring_info->ring_buffer =3D (struct hv_ring_buffer *)vaddr; + ring_info->ring_buffer->read_index =3D 0; + ring_info->ring_buffer->write_index =3D 0; =20 /* Set the feature bit for enabling flow control. */ ring_info->ring_buffer->feature_bits.value =3D 1; =20 + return 0; +} + +/* Initialize the ring buffer. */ +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; + + BUILD_BUG_ON((sizeof(struct hv_ring_buffer) !=3D PAGE_SIZE)); + + if (!hv_isolation_type_snp()) { + /* + * 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; + + pages_wraparound[0] =3D pages; + for (i =3D 0; i < 2 * (page_cnt - 1); i++) + pages_wraparound[i + 1] =3D &pages[i % (page_cnt - 1) + 1]; + + ring_info->ring_buffer =3D (struct hv_ring_buffer *) + vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, PAGE_KERNEL); + + kfree(pages_wraparound); + + if (!ring_info->ring_buffer) + return -ENOMEM; + + ring_info->ring_buffer->read_index =3D + ring_info->ring_buffer->write_index =3D 0; + + /* Set the feature bit for enabling flow control. */ + ring_info->ring_buffer->feature_bits.value =3D 1; + } + ring_info->ring_size =3D page_cnt << PAGE_SHIFT; ring_info->ring_size_div10_reciprocal =3D reciprocal_value(ring_info->ring_size / 10); --=20 2.25.1 From nobody Sun Apr 28 05:50:41 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=1628531846; cv=none; d=zohomail.com; s=zohoarc; b=R+iva6lpFYbSEkZ0KOcWH80K+jDYTLoQQ1QSs/pEid7ue4g4+q3a2vK3lO5Y4Z11BoH/1ZhegfOLXWjWV+hW0dYrJWgcnmdvKiCud51BdmcJBrV3stKmFcscna1twsYdNBUhbxTWBRav+5whQKFdE+lOWmTlAqZt+eMHwYVJgb0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628531846; 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=PA42IhHl11PQF+ziOZyqqkyOZi1yWIuNrwvqLKrflPI=; b=jjOr8xocGa6lFu/fBWXrRthKWeozkRHcjw3rC6KZTP1jmcMbxW/vCFR5bA2n1clqCKGhoGE0+T2StpiCXjco+xeZlvO2qksku6IFzV/stPnugr12HGD+MZxSZm6PtU/xb2NNtgPUhOwLB7r/pWPCOuchXlNs2H1agvA9a67WRSc= 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 1628531846371580.3048368030479; Mon, 9 Aug 2021 10:57:26 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.165136.301855 (Exim 4.92) (envelope-from ) id 1mD9WU-0004hs-1r; Mon, 09 Aug 2021 17:57:14 +0000 Received: by outflank-mailman (output) from mailman id 165136.301855; Mon, 09 Aug 2021 17:57:14 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9WT-0004hf-Sx; Mon, 09 Aug 2021 17:57:13 +0000 Received: by outflank-mailman (input) for mailman id 165136; Mon, 09 Aug 2021 17:57:12 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9WS-0000Aq-2T for xen-devel@lists.xenproject.org; Mon, 09 Aug 2021 17:57:12 +0000 Received: from mail-pj1-x1034.google.com (unknown [2607:f8b0:4864:20::1034]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 60695c2f-9ef7-4655-bef2-fdc8136d411f; Mon, 09 Aug 2021 17:56:46 +0000 (UTC) Received: by mail-pj1-x1034.google.com with SMTP id j1so29235990pjv.3 for ; Mon, 09 Aug 2021 10:56:46 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:2:106e:6ed1:5da1:2ac4]) by smtp.gmail.com with ESMTPSA id x14sm20589708pfa.127.2021.08.09.10.56.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Aug 2021 10:56: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: 60695c2f-9ef7-4655-bef2-fdc8136d411f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PA42IhHl11PQF+ziOZyqqkyOZi1yWIuNrwvqLKrflPI=; b=h/75TBkZzgYxlqy+5bEldf+iKBjJjuhF9/Luw6D8+gOXVqnAuffjZyZ8csnCHDNkaP TBTvI5UGDLYZrsK4GmYd4KQc+RdWgrfqYrfbFj4qldX8+cUJjwg+jP8IXuDHZJNZVkMx dqt2tJ9id7Atn6XSeMLFGDJd1i/ItPabnc2w1Ou3dfKiAut9APiCcAttSlaxhKd7OD/O LOEDuYsC/sv3oUNGHz6aktEyuu3eBzTJBZiHW3J5x/IjafBs1g2Y/KGYNhEkna86o9xN FAWDsRYmSWvZzVnsYGN53JtMkrKJ/gW510NPFotXeN7Wa7z/uT9Mf7pgcf/x7yOe62Mi h8Cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PA42IhHl11PQF+ziOZyqqkyOZi1yWIuNrwvqLKrflPI=; b=XCmUvGKUZfSWQEvCA6K+UY0VSl9F1n3WD9qdz2BEILH0ToJUSBW1Tg3tO+XKbnzAu1 zedmeI5qGQTjMO1pW6vczUpd3/nrhxP9Xf6eZNeGJsi2sHs1GbWiooTgL3V/zEI/1v3r WVkbZVsJM8b27x7X1yRowcfMTNJklLsXiVLlMgFQDbpYeMEl+DLG0cogN5Ij02LA3CpD M10AvcrTY950D3VyoNQOtkRBmfITAC87ltiY91wFYOmDrtIfg3/Ykt7ynL+Z5IGl3AWh byvkTBJk5kyx4kwl0AXi0q+ovo5PCtdYK9KX9RY1C1sRPON+LEQV4sD5fvJz+dVNDxKC 8iug== X-Gm-Message-State: AOAM531KqHsjj0njPjIV+2b3am3D3LrSSrzZTkoUWRwIf26VOtoBP+LO QW0dE4VWH0r6BVRrR3RBlUo= X-Google-Smtp-Source: ABdhPJz2DoChBqY2+CqrU5gCYpuDqXcBgTbb8ssn2WkHPgcTXqW+Ddha4uJTbpahTSBY5KgKQGNxrQ== X-Received: by 2002:a17:902:8f87:b029:12c:dda2:39bf with SMTP id z7-20020a1709028f87b029012cdda239bfmr21283718plo.59.1628531806316; Mon, 09 Aug 2021 10:56: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, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, ardb@kernel.org, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, saravanand@fb.com, krish.sadhukhan@oracle.com, aneesh.kumar@linux.ibm.com, xen-devel@lists.xenproject.org, rientjes@google.com, hannes@cmpxchg.org, tj@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V3 09/13] DMA: Add dma_map_decrypted/dma_unmap_encrypted() function Date: Mon, 9 Aug 2021 13:56:13 -0400 Message-Id: <20210809175620.720923-10-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210809175620.720923-1-ltykernel@gmail.com> References: <20210809175620.720923-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628531848302100001 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan In Hyper-V Isolation VM with AMD SEV, swiotlb boucne buffer needs to be mapped into address space above vTOM and so introduce dma_map_decrypted/dma_unmap_encrypted() to map/unmap bounce buffer memory. The platform can populate man/unmap callback in the dma memory decrypted ops. --- include/linux/dma-map-ops.h | 9 +++++++++ kernel/dma/mapping.c | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index 0d53a96a3d64..01d60a024e45 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -71,6 +71,11 @@ struct dma_map_ops { unsigned long (*get_merge_boundary)(struct device *dev); }; =20 +struct dma_memory_decrypted_ops { + void *(*map)(void *addr, unsigned long size); + void (*unmap)(void *addr); +}; + #ifdef CONFIG_DMA_OPS #include =20 @@ -374,6 +379,10 @@ static inline void debug_dma_dump_mappings(struct devi= ce *dev) } #endif /* CONFIG_DMA_API_DEBUG */ =20 +void *dma_map_decrypted(void *addr, unsigned long size); +int dma_unmap_decrypted(void *addr, unsigned long size); + extern const struct dma_map_ops dma_dummy_ops; +extern struct dma_memory_decrypted_ops dma_memory_generic_decrypted_ops; =20 #endif /* _LINUX_DMA_MAP_OPS_H */ diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c index 2b06a809d0b9..6fb150dc1750 100644 --- a/kernel/dma/mapping.c +++ b/kernel/dma/mapping.c @@ -13,11 +13,13 @@ #include #include #include +#include #include "debug.h" #include "direct.h" =20 bool dma_default_coherent; =20 +struct dma_memory_decrypted_ops dma_memory_generic_decrypted_ops; /* * Managed DMA API */ @@ -736,3 +738,23 @@ unsigned long dma_get_merge_boundary(struct device *de= v) return ops->get_merge_boundary(dev); } EXPORT_SYMBOL_GPL(dma_get_merge_boundary); + +void *dma_map_decrypted(void *addr, unsigned long size) +{ + if (set_memory_decrypted((unsigned long)addr, + size / PAGE_SIZE)) + return NULL; + + if (dma_memory_generic_decrypted_ops.map) + return dma_memory_generic_decrypted_ops.map(addr, size); + else + return addr; +} + +int dma_unmap_encrypted(void *addr, unsigned long size) +{ + if (dma_memory_generic_decrypted_ops.unmap) + dma_memory_generic_decrypted_ops.unmap(addr); + + return set_memory_encrypted((unsigned long)addr, size / PAGE_SIZE); +} --=20 2.25.1 From nobody Sun Apr 28 05:50:41 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=1628532117; cv=none; d=zohomail.com; s=zohoarc; b=Q6R3JJj8vUgdxl+f+DPCXk2zwqIQ69fX3wv2DFN12eQmKyq3vpFXrBgljAAalBDL2hXJULKETXcczMyLiUnhSc+0il9vTjhTkGc1HemX+a4TzZu+coFXW4BXUlmHA2ESOMDh9cUal6p+PJXLayCZjFR6ddEIcIOY3oZHtMGE59Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628532117; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=6MHi30R96t1tpcfv0VvlaJ3ckWN83uvRHnkjJOo1l30=; b=K1txx3I9TbtdAym5uJxCB4TD7/jPOS1LQgFawSPK+4WYfU9dyEz624LBv0Kp6qL/bHF+0wUkkE9fMYeQtU3szxNwEIRC/22ec41u5ti89Tz8WZaDwEivUWNxEbDdlnr4olvfLPV+A4aBWmsTr4RKTGGcmIkyN0AagcrT1+U0UCk= 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 1628532117550233.75738564598987; Mon, 9 Aug 2021 11:01:57 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.165166.301877 (Exim 4.92) (envelope-from ) id 1mD9ai-0007fG-3a; Mon, 09 Aug 2021 18:01:36 +0000 Received: by outflank-mailman (output) from mailman id 165166.301877; Mon, 09 Aug 2021 18:01:36 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9ah-0007f5-Vp; Mon, 09 Aug 2021 18:01:35 +0000 Received: by outflank-mailman (input) for mailman id 165166; Mon, 09 Aug 2021 18:01: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 1mD9WX-0000Aq-2d for xen-devel@lists.xenproject.org; Mon, 09 Aug 2021 17:57:17 +0000 Received: from mail-pj1-x1033.google.com (unknown [2607:f8b0:4864:20::1033]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id e3b1ed7a-741a-403d-9beb-08f82012ffc8; Mon, 09 Aug 2021 17:56:49 +0000 (UTC) Received: by mail-pj1-x1033.google.com with SMTP id lw7-20020a17090b1807b029017881cc80b7so56261pjb.3 for ; Mon, 09 Aug 2021 10:56:49 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:2:106e:6ed1:5da1:2ac4]) by smtp.gmail.com with ESMTPSA id x14sm20589708pfa.127.2021.08.09.10.56.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Aug 2021 10:56: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: e3b1ed7a-741a-403d-9beb-08f82012ffc8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6MHi30R96t1tpcfv0VvlaJ3ckWN83uvRHnkjJOo1l30=; b=f2d7PoNR8S7BEFglYtlqZtTOq+7/Zs/CVzt8i5RvVJDSFyxSwvYW1cOzneFqHOp8lD YMayERjWFFRhQeM38VR0PnUgeZ4+MajNuTyUjodTxOjIuzEiz+L+TWPs2rA8SMr1Ves2 yOXnyzfpVfIywNSDtsAM8unnE5zMtaNsA4EvH6Y2v9ClOXNrj9xcmYJgXlAuLbm1bK5w FLQDvym+rVXEDkiCLuW7wIcGd8BSk3ksYaG10QtZ6PF40JGfna+XstaK9mwW6X+rF7wQ r9Jmq1n1T1qx8fU02X6Mw1Fol3fZN+g8Y5pzN1daGR8DFMGa+fGiIrtwkT/EtwugpgeD 85AQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6MHi30R96t1tpcfv0VvlaJ3ckWN83uvRHnkjJOo1l30=; b=PmqbNAQzZd78gZVyDOgmUn0+bwkbW9f4SFDsgWtfSk4RAY30nqyB/tXtuZEyglsfCE MSWESAhS2aJERmNQNBdJzdCjduh4kjGlMgwX8iHPn5/W59+I66gRMvYVXwe0HqxX7ma2 V1pTBS/pEuXbyQHNY7yXbXSMjLY+JyjCqwS0ayxUs+Y7Zdx+vpGl+YrLof708miZ5+3F rurFY4EMDOgHOA1g9sDaiHTNKStTl7PnhCny42lubVFah9v5dCwDosG4gTL/3KD6lqtm A7zHkakacOFexHNvc5/hZ8zurueGbLWnj/93PKoq3oozVUL7p07ov3/bAyS4gtnWU+3v 1wYg== X-Gm-Message-State: AOAM532HSpNbkjze/L7aXmG+m5zEPexnBqzTy9Ekv1yqn3oAYfOZBr5V m+Rdi14D2mf3iH6FDfGMY1M= X-Google-Smtp-Source: ABdhPJwZPb3xQnBafX3Cc2UkFC3rKhBp0t6gAaOR3aj31uj5J0E3RaO5UtzNWHxWMFeWPLraJz/3VA== X-Received: by 2002:a63:e605:: with SMTP id g5mr687714pgh.124.1628531808948; Mon, 09 Aug 2021 10:56:48 -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, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, ardb@kernel.org, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, saravanand@fb.com, krish.sadhukhan@oracle.com, aneesh.kumar@linux.ibm.com, xen-devel@lists.xenproject.org, rientjes@google.com, hannes@cmpxchg.org, tj@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V3 10/13] x86/Swiotlb: Add Swiotlb bounce buffer remap function for HV IVM Date: Mon, 9 Aug 2021 13:56:14 -0400 Message-Id: <20210809175620.720923-11-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210809175620.720923-1-ltykernel@gmail.com> References: <20210809175620.720923-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628532118305100003 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan In Isolation VM with AMD SEV, bounce buffer needs to be accessed via extra address space which is above shared_gpa_boundary (E.G 39 bit address line) reported by Hyper-V CPUID ISOLATION_CONFIG. The access physical address will be original physical address + shared_gpa_boundary. The shared_gpa_boundary in the AMD SEV SNP spec is called virtual top of memory(vTOM). Memory addresses below vTOM are automatically treated as private while memory above vTOM is treated as shared. Use dma_map_decrypted() in the swiotlb code, store remap address returned and use the remap address to copy data from/to swiotlb bounce buffer. Signed-off-by: Tianyu Lan --- Change since v1: * Make swiotlb_init_io_tlb_mem() return error code and return error when dma_map_decrypted() fails. --- include/linux/swiotlb.h | 4 ++++ kernel/dma/swiotlb.c | 32 ++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index f507e3eacbea..584560ecaa8e 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -72,6 +72,9 @@ extern enum swiotlb_force swiotlb_force; * @end: The end address of the swiotlb memory pool. Used to do a quick * range check to see if the memory was in fact allocated by this * API. + * @vaddr: The vaddr of the swiotlb memory pool. The swiotlb + * memory pool may be remapped in the memory encrypted case and store + * virtual address for bounce buffer operation. * @nslabs: The number of IO TLB blocks (in groups of 64) between @start a= nd * @end. For default swiotlb, this is command line adjustable via * setup_io_tlb_npages. @@ -89,6 +92,7 @@ extern enum swiotlb_force swiotlb_force; struct io_tlb_mem { phys_addr_t start; phys_addr_t end; + void *vaddr; unsigned long nslabs; unsigned long used; unsigned int index; diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 1fa81c096c1d..29b6d888ef3b 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -176,7 +176,7 @@ void __init swiotlb_update_mem_attributes(void) memset(vaddr, 0, bytes); } =20 -static void swiotlb_init_io_tlb_mem(struct io_tlb_mem *mem, phys_addr_t st= art, +static int swiotlb_init_io_tlb_mem(struct io_tlb_mem *mem, phys_addr_t sta= rt, unsigned long nslabs, bool late_alloc) { void *vaddr =3D phys_to_virt(start); @@ -194,14 +194,21 @@ static void swiotlb_init_io_tlb_mem(struct io_tlb_mem= *mem, phys_addr_t start, mem->slots[i].alloc_size =3D 0; } =20 - set_memory_decrypted((unsigned long)vaddr, bytes >> PAGE_SHIFT); - memset(vaddr, 0, bytes); + mem->vaddr =3D dma_map_decrypted(vaddr, bytes); + if (!mem->vaddr) { + pr_err("Failed to decrypt memory.\n"); + return -ENOMEM; + } + + memset(mem->vaddr, 0, bytes); + return 0; } =20 int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verb= ose) { struct io_tlb_mem *mem; size_t alloc_size; + int ret; =20 if (swiotlb_force =3D=3D SWIOTLB_NO_FORCE) return 0; @@ -216,7 +223,11 @@ int __init swiotlb_init_with_tbl(char *tlb, unsigned l= ong nslabs, int verbose) panic("%s: Failed to allocate %zu bytes align=3D0x%lx\n", __func__, alloc_size, PAGE_SIZE); =20 - swiotlb_init_io_tlb_mem(mem, __pa(tlb), nslabs, false); + ret =3D swiotlb_init_io_tlb_mem(mem, __pa(tlb), nslabs, false); + if (ret) { + memblock_free(__pa(mem), alloc_size); + return ret; + } =20 io_tlb_default_mem =3D mem; if (verbose) @@ -304,6 +315,8 @@ int swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs) { struct io_tlb_mem *mem; + int size =3D get_order(struct_size(mem, slots, nslabs)); + int ret; =20 if (swiotlb_force =3D=3D SWIOTLB_NO_FORCE) return 0; @@ -312,12 +325,15 @@ swiotlb_late_init_with_tbl(char *tlb, unsigned long n= slabs) if (WARN_ON_ONCE(io_tlb_default_mem)) return -ENOMEM; =20 - mem =3D (void *)__get_free_pages(GFP_KERNEL, - get_order(struct_size(mem, slots, nslabs))); + mem =3D (void *)__get_free_pages(GFP_KERNEL, size); if (!mem) return -ENOMEM; =20 - swiotlb_init_io_tlb_mem(mem, virt_to_phys(tlb), nslabs, true); + ret =3D swiotlb_init_io_tlb_mem(mem, virt_to_phys(tlb), nslabs, true); + if (ret) { + free_pages((unsigned long)mem, size); + return ret; + } =20 io_tlb_default_mem =3D mem; swiotlb_print_info(); @@ -360,7 +376,7 @@ static void swiotlb_bounce(struct device *dev, phys_add= r_t tlb_addr, size_t size phys_addr_t orig_addr =3D mem->slots[index].orig_addr; size_t alloc_size =3D mem->slots[index].alloc_size; unsigned long pfn =3D PFN_DOWN(orig_addr); - unsigned char *vaddr =3D phys_to_virt(tlb_addr); + unsigned char *vaddr =3D mem->vaddr + tlb_addr - mem->start; unsigned int tlb_offset; =20 if (orig_addr =3D=3D INVALID_PHYS_ADDR) --=20 2.25.1 From nobody Sun Apr 28 05:50:41 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=1628532141; cv=none; d=zohomail.com; s=zohoarc; b=hUqDZc2l6S7av3WlvYL5yP1PSzrliWrVnfmf1pp7/vI+9taScemodZSPft5IvOv2Oim5ZO6g0Oz++2ZJN4/GV9Ix815YaQixT3HXY/AeOLN2CqmygF6j1irW4I/Wc2Ih1VM0N0/laVQ7QJIm73GHpPvzAZ/lLSezd9KLbLQEFx8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628532141; 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=wE1oyJvMZrxsK10mJTBwWYCUb1GB6i5zG9Yu/v/zgdM=; b=GS/xxo1BYTAMvB1IwJbDlkoSVBbMVQLHKBF7TWbPRi+NptkcsP0SiQyWvT4WAYPqtYU8+kXGm3IBBeovHZ0Fui1BvdMfCPb31ZPVNKsxUBJUqKpGFFy5EBTKHYfrp7KOaziQnR5eHrgBvnBIrxzMDNtShtpKCFsnUSG8c9K74hA= 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 1628532141732802.1818327752248; Mon, 9 Aug 2021 11:02:21 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.165184.301899 (Exim 4.92) (envelope-from ) id 1mD9bC-0000XM-Ov; Mon, 09 Aug 2021 18:02:06 +0000 Received: by outflank-mailman (output) from mailman id 165184.301899; Mon, 09 Aug 2021 18:02:06 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9bC-0000X4-Lh; Mon, 09 Aug 2021 18:02:06 +0000 Received: by outflank-mailman (input) for mailman id 165184; Mon, 09 Aug 2021 18:02:04 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9Wc-0000Aq-2s for xen-devel@lists.xenproject.org; Mon, 09 Aug 2021 17:57:22 +0000 Received: from mail-pj1-x1034.google.com (unknown [2607:f8b0:4864:20::1034]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 61c73651-940a-44e3-a734-02fbc5509dc4; Mon, 09 Aug 2021 17:56:51 +0000 (UTC) Received: by mail-pj1-x1034.google.com with SMTP id mq2-20020a17090b3802b0290178911d298bso1170878pjb.1 for ; Mon, 09 Aug 2021 10:56:51 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:2:106e:6ed1:5da1:2ac4]) by smtp.gmail.com with ESMTPSA id x14sm20589708pfa.127.2021.08.09.10.56.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Aug 2021 10:56:50 -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: 61c73651-940a-44e3-a734-02fbc5509dc4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wE1oyJvMZrxsK10mJTBwWYCUb1GB6i5zG9Yu/v/zgdM=; b=AR4iP5ZYBtCq95LlWtYYCyS910itq7pZDn/QCC4MRpb0ABoxb3+fdeU/8WwTtjQf5T wt6+yYw0C62DDMzkEhJ9TDNdHBUTRApK+U+iZJFsySVBxYlvJdujXMreaUZpdeFbAf4U vcTLAkPbqvTQqsnFJFAZNUdAmNtsbeiS2AvHxvoHY2vX7tqjY8VLvRu69UVWkdW6uPQm r6s0/vu2Yk4blkUZaI2VpcM4gDNFSYqRbOR/JAU6+351oCLyhaCizCEZRRrgZUN6m1Z1 2B1uH9soEdbo/8BjQeOdRlT9ADfhGl+oSC9LxDUyE/wPt7X/V+eJurB8URppuSrIeyYy 4zIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wE1oyJvMZrxsK10mJTBwWYCUb1GB6i5zG9Yu/v/zgdM=; b=GSrq0crIaOrf6EhRiPjbpBcw4pxIJR8q0U01PwoFkl9/3I9z58bREgGbqG4hlnj9t3 XWpdb6iEY8gGyNqBrYqOlNvxAloHuh2dvfqDA/q7MhP2qnhW52cXpEcy9OqnxxMNrXh8 WI9k+u4rWniWnyjBucVuhjOdeOYnJM4a6HG8qRNLOXh7Gxv0fSUrHXpLXr6Ee85qiSMC 9TTxgDjh/70S1cR4BPf3xifTqisJim/xKE2LA0xJkQnuMggHyYfsogkqL65fbtbEXpyq 47YNLi/kqdawmJJjhHL6+2FckKxHy8kj//xmh3AH0OLIlKDZMa00mYsKh3AKprN1zl0m y0ng== X-Gm-Message-State: AOAM533nu561n1f5ey+UO/fBIbEfMElTy9OwSrRbqfxdVaO9NSDVYczZ eP5P2aTRuNBO5macYhVrH7E= X-Google-Smtp-Source: ABdhPJy2FZfXH3NbVL2EBYwcf8LiETMKocZ9CFOcqjbqCapsAKuw9uzjOCEJ0P+M1z5RyBMo3xqDGw== X-Received: by 2002:aa7:838a:0:b029:3b7:31a5:649c with SMTP id u10-20020aa7838a0000b02903b731a5649cmr19142980pfm.44.1628531811197; Mon, 09 Aug 2021 10:56: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, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, ardb@kernel.org, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, saravanand@fb.com, krish.sadhukhan@oracle.com, aneesh.kumar@linux.ibm.com, xen-devel@lists.xenproject.org, rientjes@google.com, hannes@cmpxchg.org, tj@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V3 11/13] HV/IOMMU: Enable swiotlb bounce buffer for Isolation VM Date: Mon, 9 Aug 2021 13:56:15 -0400 Message-Id: <20210809175620.720923-12-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210809175620.720923-1-ltykernel@gmail.com> References: <20210809175620.720923-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628532142915100001 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan Hyper-V Isolation VM requires bounce buffer support to copy data from/to encrypted memory and so enable swiotlb force mode to use swiotlb bounce buffer for DMA transaction. In Isolation VM with AMD SEV, the bounce buffer needs to be accessed via extra address space which is above shared_gpa_boundary (E.G 39 bit address line) reported by Hyper-V CPUID ISOLATION_CONFIG. The access physical address will be original physical address + shared_gpa_boundary. The shared_gpa_boundary in the AMD SEV SNP spec is called virtual top of memory(vTOM). Memory addresses below vTOM are automatically treated as private while memory above vTOM is treated as shared. Swiotlb bounce buffer code calls dma_map_decrypted() to mark bounce buffer visible to host and map it in extra address space. Populate dma memory decrypted ops with hv map/unmap function. Hyper-V initalizes swiotlb bounce buffer and default swiotlb needs to be disabled. pci_swiotlb_detect_override() and pci_swiotlb_detect_4gb() enable the default one. To override the setting, hyperv_swiotlb_detect() needs to run before these detect functions which depends on the pci_xen_swiotlb_ init(). Make pci_xen_swiotlb_init() depends on the hyperv_swiotlb _detect() to keep the order. The map function vmap_pfn() can't work in the early place hyperv_iommu_swiotlb_init() and so initialize swiotlb bounce buffer in the hyperv_iommu_swiotlb_later_init(). Signed-off-by: Tianyu Lan --- arch/x86/hyperv/ivm.c | 28 ++++++++++++++ arch/x86/include/asm/mshyperv.h | 2 + arch/x86/xen/pci-swiotlb-xen.c | 3 +- drivers/hv/vmbus_drv.c | 3 ++ drivers/iommu/hyperv-iommu.c | 65 +++++++++++++++++++++++++++++++++ include/linux/hyperv.h | 1 + 6 files changed, 101 insertions(+), 1 deletion(-) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index c13ec5560d73..0f05e4d6fc62 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -265,3 +265,31 @@ int hv_set_mem_host_visibility(unsigned long addr, int= numpages, bool visible) =20 return __hv_set_mem_host_visibility((void *)addr, numpages, visibility); } + +/* + * hv_map_memory - map memory to extra space in the AMD SEV-SNP Isolation = VM. + */ +void *hv_map_memory(void *addr, unsigned long size) +{ + unsigned long *pfns =3D kcalloc(size / HV_HYP_PAGE_SIZE, + sizeof(unsigned long), GFP_KERNEL); + void *vaddr; + int i; + + if (!pfns) + return NULL; + + for (i =3D 0; i < size / HV_HYP_PAGE_SIZE; i++) + pfns[i] =3D virt_to_hvpfn(addr + i * HV_HYP_PAGE_SIZE) + + (ms_hyperv.shared_gpa_boundary >> HV_HYP_PAGE_SHIFT); + + vaddr =3D vmap_pfn(pfns, size / HV_HYP_PAGE_SIZE, PAGE_KERNEL_IO); + kfree(pfns); + + return vaddr; +} + +void hv_unmap_memory(void *addr) +{ + vunmap(addr); +} diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyper= v.h index a30c60f189a3..b247739f57ac 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -250,6 +250,8 @@ int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_= interrupt_entry *entry); int hv_mark_gpa_visibility(u16 count, const u64 pfn[], enum hv_mem_host_visibility visibility); int hv_set_mem_host_visibility(unsigned long addr, int numpages, bool visi= ble); +void *hv_map_memory(void *addr, unsigned long size); +void hv_unmap_memory(void *addr); void hv_sint_wrmsrl_ghcb(u64 msr, u64 value); void hv_sint_rdmsrl_ghcb(u64 msr, u64 *value); void hv_signal_eom_ghcb(void); diff --git a/arch/x86/xen/pci-swiotlb-xen.c b/arch/x86/xen/pci-swiotlb-xen.c index 54f9aa7e8457..43bd031aa332 100644 --- a/arch/x86/xen/pci-swiotlb-xen.c +++ b/arch/x86/xen/pci-swiotlb-xen.c @@ -4,6 +4,7 @@ =20 #include #include +#include #include =20 #include @@ -91,6 +92,6 @@ int pci_xen_swiotlb_init_late(void) EXPORT_SYMBOL_GPL(pci_xen_swiotlb_init_late); =20 IOMMU_INIT_FINISH(pci_xen_swiotlb_detect, - NULL, + hyperv_swiotlb_detect, pci_xen_swiotlb_init, NULL); diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c index 57bbbaa4e8f7..f068e22a5636 100644 --- a/drivers/hv/vmbus_drv.c +++ b/drivers/hv/vmbus_drv.c @@ -23,6 +23,7 @@ #include #include =20 +#include #include #include #include @@ -2081,6 +2082,7 @@ struct hv_device *vmbus_device_create(const guid_t *t= ype, return child_device_obj; } =20 +static u64 vmbus_dma_mask =3D DMA_BIT_MASK(64); /* * vmbus_device_register - Register the child device */ @@ -2121,6 +2123,7 @@ int vmbus_device_register(struct hv_device *child_dev= ice_obj) } hv_debug_add_dev_dir(child_device_obj); =20 + child_device_obj->device.dma_mask =3D &vmbus_dma_mask; return 0; =20 err_kset_unregister: diff --git a/drivers/iommu/hyperv-iommu.c b/drivers/iommu/hyperv-iommu.c index e285a220c913..01e874b3b43a 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,58 @@ static const struct irq_domain_ops hyperv_root_ir_doma= in_ops =3D { .free =3D hyperv_root_irq_remapping_free, }; =20 +void __init hyperv_iommu_swiotlb_init(void) +{ + unsigned long bytes; + + /* + * Allocate Hyper-V swiotlb bounce buffer at early place + * to reserve large contiguous memory. + */ + hyperv_io_tlb_size =3D 256 * 1024 * 1024; + hyperv_io_tlb_start =3D + memblock_alloc_low( + PAGE_ALIGN(hyperv_io_tlb_size), + HV_HYP_PAGE_SIZE); + + if (!hyperv_io_tlb_start) { + pr_warn("Fail to allocate Hyper-V swiotlb buffer.\n"); + return; + } +} + +int __init hyperv_swiotlb_detect(void) +{ + if (hypervisor_is_type(X86_HYPER_MS_HYPERV) + && hv_is_isolation_supported()) { + /* + * Enable swiotlb force mode in Isolation VM to + * use swiotlb bounce buffer for dma transaction. + */ + swiotlb_force =3D SWIOTLB_FORCE; + + dma_memory_generic_decrypted_ops.map =3D hv_map_memory; + dma_memory_generic_decrypted_ops.unmap =3D hv_unmap_memory; + return 1; + } + + return 0; +} + +void __init hyperv_iommu_swiotlb_later_init(void) +{ + /* + * Swiotlb bounce buffer needs to be mapped in extra address + * space. Map function doesn't work in the early place and so + * call swiotlb_late_init_with_tbl() here. + */ + if (swiotlb_late_init_with_tbl(hyperv_io_tlb_start, + hyperv_io_tlb_size >> IO_TLB_SHIFT)) + panic("Fail to initialize hyperv swiotlb.\n"); +} + +IOMMU_INIT_FINISH(hyperv_swiotlb_detect, + NULL, hyperv_iommu_swiotlb_init, + hyperv_iommu_swiotlb_later_init); + #endif diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h index 90b542597143..83fa567ad594 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -1744,6 +1744,7 @@ int hyperv_write_cfg_blk(struct pci_dev *dev, void *b= uf, unsigned int len, int hyperv_reg_block_invalidate(struct pci_dev *dev, void *context, void (*block_invalidate)(void *context, u64 block_mask)); +int __init hyperv_swiotlb_detect(void); =20 struct hyperv_pci_block_ops { int (*read_block)(struct pci_dev *dev, void *buf, unsigned int buf_len, --=20 2.25.1 From nobody Sun Apr 28 05:50:41 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=1628532116; cv=none; d=zohomail.com; s=zohoarc; b=LisjHkk/eA7yUpdPmkoXGWuHSmqDcOr1rrumsLMHF3xSIlBbSzxVslRYyeJIfwJQhmLZhA9npb/eF8qF0bLhPv1SHLhW5snRBCwwov4Y9I/Bs+SNkU22WuvUqS2apBDvUh6hU+F9gMMGpDIBXwVhndi8vE09rn/otsG/abaMGwA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628532116; 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=4Z8g+6aX8dcjduBIedx4CzSMH7i8+Bmwle6K2FcmT9E=; b=L8JAsEDLrPWLAaTIyMfXp8yLBnAxtu9kI6mcUUYts9SISorGLtNBEWWzCQY2KFH4xX94+d1poPqqfLUiLbJG/u5b+BLRs6T8D8ZrKIdXNRiZQNsuZFsHSL/EUeb8h6AOZv/Nc4s1OXXpCwEmOgBMI3baTScITMOq4q2X0WXZJ/o= 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 162853211659933.25861871730763; Mon, 9 Aug 2021 11:01:56 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.165171.301888 (Exim 4.92) (envelope-from ) id 1mD9ao-00082K-BA; Mon, 09 Aug 2021 18:01:42 +0000 Received: by outflank-mailman (output) from mailman id 165171.301888; Mon, 09 Aug 2021 18:01:42 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9ao-000829-7e; Mon, 09 Aug 2021 18:01:42 +0000 Received: by outflank-mailman (input) for mailman id 165171; Mon, 09 Aug 2021 18:01:40 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9Wh-0000Aq-39 for xen-devel@lists.xenproject.org; Mon, 09 Aug 2021 17:57:27 +0000 Received: from mail-pj1-x1030.google.com (unknown [2607:f8b0:4864:20::1030]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id e41550fc-f334-4fbd-b972-84287f37b8c8; Mon, 09 Aug 2021 17:56:54 +0000 (UTC) Received: by mail-pj1-x1030.google.com with SMTP id w13-20020a17090aea0db029017897a5f7bcso1127337pjy.5 for ; Mon, 09 Aug 2021 10:56:54 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:2:106e:6ed1:5da1:2ac4]) by smtp.gmail.com with ESMTPSA id x14sm20589708pfa.127.2021.08.09.10.56.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Aug 2021 10:56: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: e41550fc-f334-4fbd-b972-84287f37b8c8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4Z8g+6aX8dcjduBIedx4CzSMH7i8+Bmwle6K2FcmT9E=; b=ZlXUD+lz2Gp//Dz/C2mpFxdMZL25BC0hOR/t6BoEQ1bTli+Xl1CRYKEdG7IXN+dwoG wbh0UypB+Lr3qyRvhWzIxBGbx+fYykom3dWlYcGq/YT5Z7InstX7/TdXYhZGzB339ev+ tHSZuw6Vd7eCR9FA7MeNIRBHcH/DDufvpmF0emL/4l+cq1ClvbCHwkpEXvaZ3HakmnXk 6/KPgjsie5xVLEjCSCVIa0p5LmbrLf873Vd0wi6+te3E9tmIpBMx9GS5qhPWPQtWsi07 Ihg1rnh6z0Q2aSrWmH0khliF6jGGDsOs5y3kGm6YRdctvUuNOLw99duYdp1PU3dRZUrG bYsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4Z8g+6aX8dcjduBIedx4CzSMH7i8+Bmwle6K2FcmT9E=; b=ks8Kys7IUlgr2sBOIvVPWXZxdVSUID0ruSwu9UpU5aoW13KmpLtQug5xc0pn8JlEkA aeVUMCI+vWq9XNurcahi3VgMHah7as9x/w5bsasoAJZr8ziVXRYWZ5Mn6kQ0uWC5YUzC BnJ+vuF4mJOSBL7IKkpaJePEcPz4HNgV5AAGyGJaWhqPGyvld/1Fg7/oyk4DZRiPitg+ 0rZ58Iy3JN1FZXQxoOMDt+D8mx+AJR2decHCuRoliJX3nkW3cqzDZRDhX5Mtf5Wayi05 1gucc/mYBY0/5tgDk2cu7omTHNpnb61CPwL0V9hAFuVYunQCjeHCZj1ww2sRL2O2XjdD 0/kA== X-Gm-Message-State: AOAM531tcJX5nX4fTFN0sv9KRnIpd8EbDWAB2z7pigHnq5orPENlQRKN ra7b5ZyEmGp69YJW3h+v5BQ= X-Google-Smtp-Source: ABdhPJwGCSit9y4xVfsX3Nhv/HsWm5r+OyVOoXyi49YThgzy98AeV/wUZJctdeptVPFNfzomXk8fzA== X-Received: by 2002:a17:902:7781:b029:12d:1887:87ca with SMTP id o1-20020a1709027781b029012d188787camr7799388pll.19.1628531813404; Mon, 09 Aug 2021 10:56:53 -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, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, ardb@kernel.org, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, saravanand@fb.com, krish.sadhukhan@oracle.com, aneesh.kumar@linux.ibm.com, xen-devel@lists.xenproject.org, rientjes@google.com, hannes@cmpxchg.org, tj@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V3 12/13] HV/Netvsc: Add Isolation VM support for netvsc driver Date: Mon, 9 Aug 2021 13:56:16 -0400 Message-Id: <20210809175620.720923-13-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210809175620.720923-1-ltykernel@gmail.com> References: <20210809175620.720923-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628532118030100001 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() still need to handle. Use DMA API to map/umap these memory during sending/receiving packet and Hyper-V DMA ops callback will use swiotlb function to allocate bounce buffer and copy data from/to bounce buffer. Signed-off-by: Tianyu Lan --- drivers/net/hyperv/hyperv_net.h | 6 ++ drivers/net/hyperv/netvsc.c | 144 +++++++++++++++++++++++++++++- drivers/net/hyperv/rndis_filter.c | 2 + include/linux/hyperv.h | 5 ++ 4 files changed, 154 insertions(+), 3 deletions(-) diff --git a/drivers/net/hyperv/hyperv_net.h b/drivers/net/hyperv/hyperv_ne= t.h index bc48855dff10..862419912bfb 100644 --- a/drivers/net/hyperv/hyperv_net.h +++ b/drivers/net/hyperv/hyperv_net.h @@ -164,6 +164,7 @@ struct hv_netvsc_packet { u32 total_bytes; u32 send_buf_index; u32 total_data_buflen; + struct hv_dma_range *dma_range; }; =20 #define NETVSC_HASH_KEYLEN 40 @@ -1074,6 +1075,7 @@ struct netvsc_device { =20 /* Receive buffer allocated by us but manages by NetVSP */ void *recv_buf; + void *recv_original_buf; u32 recv_buf_size; /* allocated bytes */ u32 recv_buf_gpadl_handle; u32 recv_section_cnt; @@ -1082,6 +1084,8 @@ struct netvsc_device { =20 /* Send buffer allocated by us */ void *send_buf; + void *send_original_buf; + u32 send_buf_size; u32 send_buf_gpadl_handle; u32 send_section_cnt; u32 send_section_size; @@ -1730,4 +1734,6 @@ struct rndis_message { #define RETRY_US_HI 10000 #define RETRY_MAX 2000 /* >10 sec */ =20 +void netvsc_dma_unmap(struct hv_device *hv_dev, + struct hv_netvsc_packet *packet); #endif /* _HYPERV_NET_H */ diff --git a/drivers/net/hyperv/netvsc.c b/drivers/net/hyperv/netvsc.c index 7bd935412853..fc312e5db4d5 100644 --- a/drivers/net/hyperv/netvsc.c +++ b/drivers/net/hyperv/netvsc.c @@ -153,8 +153,21 @@ static void free_netvsc_device(struct rcu_head *head) int i; =20 kfree(nvdev->extension); - vfree(nvdev->recv_buf); - vfree(nvdev->send_buf); + + if (nvdev->recv_original_buf) { + vunmap(nvdev->recv_buf); + vfree(nvdev->recv_original_buf); + } else { + vfree(nvdev->recv_buf); + } + + if (nvdev->send_original_buf) { + vunmap(nvdev->send_buf); + vfree(nvdev->send_original_buf); + } else { + vfree(nvdev->send_buf); + } + kfree(nvdev->send_section_map); =20 for (i =3D 0; i < VRSS_CHANNEL_MAX; i++) { @@ -330,6 +343,27 @@ int netvsc_alloc_recv_comp_ring(struct netvsc_device *= net_device, u32 q_idx) return nvchan->mrc.slots ? 0 : -ENOMEM; } =20 +static void *netvsc_remap_buf(void *buf, unsigned long size) +{ + unsigned long *pfns; + void *vaddr; + int i; + + pfns =3D kcalloc(size / HV_HYP_PAGE_SIZE, sizeof(unsigned long), + GFP_KERNEL); + if (!pfns) + return NULL; + + for (i =3D 0; i < size / HV_HYP_PAGE_SIZE; i++) + pfns[i] =3D virt_to_hvpfn(buf + i * HV_HYP_PAGE_SIZE) + + (ms_hyperv.shared_gpa_boundary >> HV_HYP_PAGE_SHIFT); + + vaddr =3D vmap_pfn(pfns, size / HV_HYP_PAGE_SIZE, 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) @@ -340,6 +374,7 @@ static int netvsc_init_buf(struct hv_device *device, unsigned int buf_size; size_t map_words; int i, ret =3D 0; + void *vaddr; =20 /* Get receive buffer area. */ buf_size =3D device_info->recv_sections * device_info->recv_section_size; @@ -375,6 +410,15 @@ static int netvsc_init_buf(struct hv_device *device, goto cleanup; } =20 + if (hv_isolation_type_snp()) { + vaddr =3D netvsc_remap_buf(net_device->recv_buf, buf_size); + if (!vaddr) + goto cleanup; + + net_device->recv_original_buf =3D net_device->recv_buf; + net_device->recv_buf =3D vaddr; + } + /* Notify the NetVsp of the gpadl handle */ init_packet =3D &net_device->channel_init_pkt; memset(init_packet, 0, sizeof(struct nvsp_message)); @@ -477,6 +521,15 @@ static int netvsc_init_buf(struct hv_device *device, goto cleanup; } =20 + if (hv_isolation_type_snp()) { + vaddr =3D netvsc_remap_buf(net_device->send_buf, buf_size); + if (!vaddr) + goto cleanup; + + net_device->send_original_buf =3D net_device->send_buf; + net_device->send_buf =3D vaddr; + } + /* Notify the NetVsp of the gpadl handle */ init_packet =3D &net_device->channel_init_pkt; memset(init_packet, 0, sizeof(struct nvsp_message)); @@ -767,7 +820,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; @@ -783,6 +836,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 @@ -947,6 +1001,82 @@ 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 and so not + * use dma_map_sg() here. + */ +int netvsc_dma_map(struct hv_device *hv_dev, + struct hv_netvsc_packet *packet, + struct hv_page_buffer *pb) +{ + u32 page_count =3D packet->cp_partial ? + packet->page_buf_cnt - packet->rmsg_pgcnt : + packet->page_buf_cnt; + dma_addr_t dma; + int i; + + if (!hv_is_isolation_supported()) + return 0; + + packet->dma_range =3D kcalloc(page_count, + sizeof(*packet->dma_range), + GFP_KERNEL); + if (!packet->dma_range) + return -ENOMEM; + + for (i =3D 0; i < page_count; i++) { + char *src =3D phys_to_virt((pb[i].pfn << HV_HYP_PAGE_SHIFT) + + pb[i].offset); + u32 len =3D pb[i].len; + + dma =3D dma_map_single(&hv_dev->device, src, len, + DMA_TO_DEVICE); + if (dma_mapping_error(&hv_dev->device, dma)) { + kfree(packet->dma_range); + return -ENOMEM; + } + + packet->dma_range[i].dma =3D dma; + packet->dma_range[i].mapping_size =3D len; + pb[i].pfn =3D dma >> HV_HYP_PAGE_SHIFT; + pb[i].offset =3D offset_in_hvpage(dma); + pb[i].len =3D len; + } + + return 0; +} + static inline int netvsc_send_pkt( struct hv_device *device, struct hv_netvsc_packet *packet, @@ -987,14 +1117,22 @@ 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) + return ret; + 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), 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 83fa567ad594..2ea638101645 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -1601,6 +1601,11 @@ struct hyperv_service_callback { void (*callback)(void *context); }; =20 +struct hv_dma_range { + dma_addr_t dma; + u32 mapping_size; +}; + #define MAX_SRV_VER 0x7ffffff extern bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp, u8 *buf= , u32 buflen, const int *fw_version, int fw_vercnt, --=20 2.25.1 From nobody Sun Apr 28 05:50:41 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=1628532092; cv=none; d=zohomail.com; s=zohoarc; b=FQhSRgL0FFy8MM3YesWYMYutsIzvp0epAN2wCsOcpfFa/OqnpkZWhCW58haN04M+4yEIpO/lcpH+gt5an1hNwKYJOx1L7JDBZjxlj7d3XgbsFYFWOQxfIc+YUUyuj2DyO5OUAdBeQZ8vfmayMtr8HEVZMFj5NQZFWrF8aOAE0Sk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628532092; 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=29igml5xJQT7NZ/zHELD608zwBtMw+CBZNUu6aD3TXw=; b=Ui5y8mzr9NAxFIYXt658qTyZmOTYJepMu+lBthkU5PenspGpP1fHZ/+WPRcj6VGZ5JyZBAWF/HoYKOnfKnokJYuKduQkxzSIhmChNEb8BzurSEcptbMQb3ZtYoiL7gqYzq5xk01z1MqtEaw2fSxBv9U/A2L1WwJ2l1ycySQnQCs= 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 1628532092171447.4662327352105; Mon, 9 Aug 2021 11:01:32 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.165162.301866 (Exim 4.92) (envelope-from ) id 1mD9aO-0007Fu-Om; Mon, 09 Aug 2021 18:01:16 +0000 Received: by outflank-mailman (output) from mailman id 165162.301866; Mon, 09 Aug 2021 18:01:16 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9aO-0007Fn-Lf; Mon, 09 Aug 2021 18:01:16 +0000 Received: by outflank-mailman (input) for mailman id 165162; Mon, 09 Aug 2021 18:01:16 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mD9Wm-0000Aq-3C for xen-devel@lists.xenproject.org; Mon, 09 Aug 2021 17:57:32 +0000 Received: from mail-pj1-x1036.google.com (unknown [2607:f8b0:4864:20::1036]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id ad856dce-eb78-484d-8b5e-4f59ea3fe2ee; Mon, 09 Aug 2021 17:56:56 +0000 (UTC) Received: by mail-pj1-x1036.google.com with SMTP id u21-20020a17090a8915b02901782c36f543so47106pjn.4 for ; Mon, 09 Aug 2021 10:56:56 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:2:106e:6ed1:5da1:2ac4]) by smtp.gmail.com with ESMTPSA id x14sm20589708pfa.127.2021.08.09.10.56.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Aug 2021 10:56:55 -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: ad856dce-eb78-484d-8b5e-4f59ea3fe2ee DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=29igml5xJQT7NZ/zHELD608zwBtMw+CBZNUu6aD3TXw=; b=ZCoB9o0cvriAk3/PPGeEVPrIAnIqymJpX+yMC18ujzQkba5lyQWncuyzod11hNntU1 1bk6U1EvfSLpZGhLCGAu8NGIa9wsSZRoZ9K8ij4t9yrTzMssHz4c12UmzGHuYx3Qqwqe 5euceZzMuwpmGsvwlu4n571z1nUyoA5EMVamLrK2Fv4kQgex9w+3kQMyU5XErr3GN4CH 2w3uSc7qIzutfvO/NUn4XoFUmpELqTdpw+eF5ttK+V/67fPZ9sLruj8gW2s9Uld8o0/F kbW1vumljR+S/wuC/e/qZtU3IDoreR6eEJ755vt4a2DdwykGjIka2gxZnJUQ0sS84g+E rg5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=29igml5xJQT7NZ/zHELD608zwBtMw+CBZNUu6aD3TXw=; b=Af3Hf9mtrckLn/CVTYZ0bjS/EDKfHCYRPC8SHdxozkYYJHUKhHk25s4XPT7GxhzPFk vyxlzxG7iIQXcdxIigEHGrMKKF6amciyNPk0cmoBg7eXWBGqDGGv8ezkAtn6kIiTN1np SF6h1K2NHasRNQsCZan35cRUV3X2oS+f0N9qw0pYu/OW974aLApnYy6WUa47l+nQumgt /NknT+Mmn7QpJmyXpd0j7VQwX/LuPz7HUdGvNan3y0v+BEmIOcGvStWNnd0ppk0YE5vh a1XDkhBHXgy8alPnr9gScbA/uE0BtRKM1rxAgPxqzdVVc8OaNTawBgBSbSgbZhz+mC+8 sDhQ== X-Gm-Message-State: AOAM5323tGikL982++UB8D8eeof6GjWfAVMEaIC5hdLh/mt/nPlDzYcI byL/o8+yORjOMhDl4vhrpcc= X-Google-Smtp-Source: ABdhPJyrAc9rzm4dB2lw3/nEgFaiXcijpmPD2rziJMns8K4uV1C97emxRFnkmN6KdkTyhlwj4rqtkA== X-Received: by 2002:a17:90a:a112:: with SMTP id s18mr323042pjp.27.1628531815828; Mon, 09 Aug 2021 10:56:55 -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, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, ardb@kernel.org, Tianyu.Lan@microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, sfr@canb.auug.org.au, saravanand@fb.com, krish.sadhukhan@oracle.com, aneesh.kumar@linux.ibm.com, xen-devel@lists.xenproject.org, rientjes@google.com, hannes@cmpxchg.org, tj@kernel.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V3 13/13] HV/Storvsc: Add Isolation VM support for storvsc driver Date: Mon, 9 Aug 2021 13:56:17 -0400 Message-Id: <20210809175620.720923-14-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210809175620.720923-1-ltykernel@gmail.com> References: <20210809175620.720923-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628532093828100001 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 need to handle. Use DMA API to map/umap these memory during sending/receiving packet and Hyper-V DMA ops callback will use swiotlb function to allocate bounce buffer and copy data from/to bounce buffer. Signed-off-by: Tianyu Lan --- drivers/scsi/storvsc_drv.c | 68 +++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 5 deletions(-) diff --git a/drivers/scsi/storvsc_drv.c b/drivers/scsi/storvsc_drv.c index 328bb961c281..78320719bdd8 100644 --- a/drivers/scsi/storvsc_drv.c +++ b/drivers/scsi/storvsc_drv.c @@ -21,6 +21,8 @@ #include #include #include +#include +#include #include #include #include @@ -427,6 +429,8 @@ struct storvsc_cmd_request { u32 payload_sz; =20 struct vstor_packet vstor_packet; + u32 hvpg_count; + struct hv_dma_range *dma_range; }; =20 =20 @@ -509,6 +513,14 @@ struct storvsc_scan_work { u8 tgt_id; }; =20 +#define storvsc_dma_map(dev, page, offset, size, dir) \ + dma_map_page(dev, page, offset, size, dir) + +#define storvsc_dma_unmap(dev, dma_range, dir) \ + dma_unmap_page(dev, dma_range.dma, \ + dma_range.mapping_size, \ + dir ? DMA_FROM_DEVICE : DMA_TO_DEVICE) + static void storvsc_device_scan(struct work_struct *work) { struct storvsc_scan_work *wrk; @@ -1260,6 +1272,7 @@ static void storvsc_on_channel_callback(void *context) struct hv_device *device; struct storvsc_device *stor_device; struct Scsi_Host *shost; + int i; =20 if (channel->primary_channel !=3D NULL) device =3D channel->primary_channel->device_obj; @@ -1314,6 +1327,15 @@ static void storvsc_on_channel_callback(void *contex= t) request =3D (struct storvsc_cmd_request *)scsi_cmd_priv(scmnd); } =20 + if (request->dma_range) { + for (i =3D 0; i < request->hvpg_count; i++) + storvsc_dma_unmap(&device->device, + request->dma_range[i], + request->vstor_packet.vm_srb.data_in =3D=3D READ_TYPE); + + kfree(request->dma_range); + } + storvsc_on_receive(stor_device, packet, request); continue; } @@ -1810,7 +1832,9 @@ static int storvsc_queuecommand(struct Scsi_Host *hos= t, struct scsi_cmnd *scmnd) 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); + dma_addr_t dma; u64 hvpfn; + u32 size; =20 if (hvpg_count > MAX_PAGE_BUFFER_COUNT) { =20 @@ -1824,6 +1848,13 @@ 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 + cmd_request->dma_range =3D kcalloc(hvpg_count, + sizeof(*cmd_request->dma_range), + GFP_ATOMIC); + if (!cmd_request->dma_range) { + ret =3D -ENOMEM; + goto free_payload; + } =20 for (i =3D 0; sgl !=3D NULL; sgl =3D sg_next(sgl)) { /* @@ -1847,9 +1878,29 @@ static int storvsc_queuecommand(struct Scsi_Host *ho= st, struct scsi_cmnd *scmnd) * last sgl should be reached at the same time that * the PFN array is filled. */ - while (hvpfns_to_add--) - payload->range.pfn_array[i++] =3D hvpfn++; + while (hvpfns_to_add--) { + size =3D min(HV_HYP_PAGE_SIZE - offset_in_hvpg, + (unsigned long)length); + dma =3D storvsc_dma_map(&dev->device, pfn_to_page(hvpfn++), + offset_in_hvpg, size, + scmnd->sc_data_direction); + if (dma_mapping_error(&dev->device, dma)) { + ret =3D -ENOMEM; + goto free_dma_range; + } + + if (offset_in_hvpg) { + payload->range.offset =3D dma & ~HV_HYP_PAGE_MASK; + offset_in_hvpg =3D 0; + } + + cmd_request->dma_range[i].dma =3D dma; + cmd_request->dma_range[i].mapping_size =3D size; + payload->range.pfn_array[i++] =3D dma >> HV_HYP_PAGE_SHIFT; + length -=3D size; + } } + cmd_request->hvpg_count =3D hvpg_count; } =20 cmd_request->payload =3D payload; @@ -1860,13 +1911,20 @@ static int storvsc_queuecommand(struct Scsi_Host *h= ost, struct scsi_cmnd *scmnd) put_cpu(); =20 if (ret =3D=3D -EAGAIN) { - if (payload_sz > sizeof(cmd_request->mpb)) - kfree(payload); /* no more space */ - return SCSI_MLQUEUE_DEVICE_BUSY; + ret =3D SCSI_MLQUEUE_DEVICE_BUSY; + goto free_dma_range; } =20 return 0; + +free_dma_range: + kfree(cmd_request->dma_range); + +free_payload: + if (payload_sz > sizeof(cmd_request->mpb)) + kfree(payload); + return ret; } =20 static struct scsi_host_template scsi_driver =3D { --=20 2.25.1