From nobody Thu May 2 22:36:05 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=1627484016; cv=none; d=zohomail.com; s=zohoarc; b=YcxLx8zBh7KGkJUK8siNuWAM1/utxSLYSgHPH5SJoW5lUDhvS24E6AeQ5BbfgOAhp+DngA8/gYIFgwEQj+SAPfKTxN2892ffpsiZ/VgV+Ftaf/9usfIfPzhYV/VCRpGscJTJFJQK0ectmnply9gHu9XtFgaaPI4DttOoGymakDo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1627484016; 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=Ra70YmOoKyUigRxssc8bdLw9SZJrXLPE0YQ4dcYNGVo=; b=dlF99s5rBgxCbbRrjkrtPLj0vjbxQ3Ggr0PbGvY77yQs8FsjrSqUZ93YLQ0JssO/jxGNoASbQcyQRNyDbgYSbQrc/vdk/0sQ7XArF9Zy3K5SAv0QjgR6X30juEoGz7fjbdSrLCVesHEQuHL4Gf+OFBxDj7zsoNUXMCbP3hRhThA= 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 1627484016020847.5200719760568; Wed, 28 Jul 2021 07:53:36 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.161583.296586 (Exim 4.92) (envelope-from ) id 1m8kvn-0002me-Es; Wed, 28 Jul 2021 14:53:11 +0000 Received: by outflank-mailman (output) from mailman id 161583.296586; Wed, 28 Jul 2021 14:53:11 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kvn-0002mT-AQ; Wed, 28 Jul 2021 14:53:11 +0000 Received: by outflank-mailman (input) for mailman id 161583; Wed, 28 Jul 2021 14:53:10 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kvm-0002W0-Os for xen-devel@lists.xenproject.org; Wed, 28 Jul 2021 14:53:10 +0000 Received: from mail-pl1-x632.google.com (unknown [2607:f8b0:4864:20::632]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 8ef6e888-024b-47e5-b703-ca2fbe2129c6; Wed, 28 Jul 2021 14:53:08 +0000 (UTC) Received: by mail-pl1-x632.google.com with SMTP id q2so2966178plr.11 for ; Wed, 28 Jul 2021 07:53:08 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:0:3823:141e:6d51:f0ad]) by smtp.gmail.com with ESMTPSA id n134sm277558pfd.89.2021.07.28.07.53.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 07:53:07 -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: 8ef6e888-024b-47e5-b703-ca2fbe2129c6 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=Ra70YmOoKyUigRxssc8bdLw9SZJrXLPE0YQ4dcYNGVo=; b=LmKHehSzb54ioqXz1ez6t1x7O5R86K81v+pIequaacef/tO9BDCzSjcgHS6hGrhQv2 qzARLAfbJyE6sHuqTHz9vyy6kXmEQNBaebh5fDgBK43+147na/gMGQQWfTHnt9bMZGW3 PI/xSNfASsbrpBOIlGaXrQ5YMH8qB1cvo0ozOtA0lboOZTSaOrbYcfC4Zr/MGizHIj4K UQn2qOIx7n+NsKeHbyzbGo6c8gE+9TdBheq0rWZ1PelrbCELqStjq++WLQ17f+9YCPDK xG/Aw0FLnS5tr9GZTkXJZ/H6BWgFhS32PFT431EJh1q4PAKHp3O/cbydxcbUBibmcfx9 Kdig== 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=Ra70YmOoKyUigRxssc8bdLw9SZJrXLPE0YQ4dcYNGVo=; b=gdrbvtuIDzx5Z+LkwIAeM3CsOT68Jfh+0xdn7aw+qRxfoeaLxkg3TLWl+1igpRranW jMHzxqVUthkXpM3bs64pwD7Jwryk08nRGI8pYFBJc6afWMKqNJ2WiCTcPtcwN3ZV2zlH NaVyGf1wzfOGJt30mKTLtBuPrkMkKQKUsqJKskbp41PCNiQ2nIKj3rqzXcuAq63pUiGt zpgwuSEoqj+Sm8GmOZ5+gK+GQDIrBT1Oernx3lV1Pw4iZsRiDkfHsOm5DtISiskFyPHF MVIpSInujZOb1Ay0RQcRzOZtbTnvseE7mucGUbPqkM8A6CeQNEiXJsCrW/lcjWgIjZgD k2cA== X-Gm-Message-State: AOAM53116mdHdNkKoxCfwcNdge/B29RbmcjzH5ubySaHGUTtZpab6a5z vVCwRvZ/vBk1wO+UHRK+I+A= X-Google-Smtp-Source: ABdhPJzsXb8YMbXnD2VQ8NxmDIMRNHMhIgP2MCg4JalE3hBkOx0ET9j7WaB1NV2mPcJTuLhWN1sM8A== X-Received: by 2002:a17:90a:ce02:: with SMTP id f2mr9955691pju.232.1627483988220; Wed, 28 Jul 2021 07:53:08 -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, rientjes@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, rppt@kernel.org, kirill.shutemov@linux.intel.com, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, pgonda@google.com, david@redhat.com, keescook@chromium.org, hannes@cmpxchg.org, sfr@canb.auug.org.au, 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, anparri@microsoft.com Subject: [PATCH 01/13] x86/HV: Initialize GHCB page in Isolation VM Date: Wed, 28 Jul 2021 10:52:16 -0400 Message-Id: <20210728145232.285861-2-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210728145232.285861-1-ltykernel@gmail.com> References: <20210728145232.285861-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1627484018210100005 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 | 73 +++++++++++++++++++++++++++++++-- arch/x86/include/asm/mshyperv.h | 2 + include/asm-generic/mshyperv.h | 2 + 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c index 4a643a85d570..ee449c076ef4 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,26 @@ 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; + + 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) { struct hv_vp_assist_page **hvp =3D &hv_vp_assist_page[smp_processor_id()]; @@ -75,6 +96,8 @@ static int hv_cpu_init(unsigned int cpu) wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val); } =20 + hyperv_init_ghcb(); + return 0; } =20 @@ -167,6 +190,31 @@ static int hv_cpu_die(unsigned int cpu) { struct hv_reenlightenment_control re_ctrl; unsigned int new_cpu; + unsigned long flags; + void **input_arg; + void *pg; + void **ghcb_va =3D NULL; + + local_irq_save(flags); + input_arg =3D (void **)this_cpu_ptr(hyperv_pcpu_input_arg); + pg =3D *input_arg; + *input_arg =3D NULL; + + if (hv_root_partition) { + void **output_arg; + + output_arg =3D (void **)this_cpu_ptr(hyperv_pcpu_output_arg); + *output_arg =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; + } + + local_irq_restore(flags); =20 hv_common_cpu_die(cpu); =20 @@ -340,9 +388,22 @@ 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; + } + + /* 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); @@ -403,7 +464,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); @@ -431,6 +493,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); 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 Thu May 2 22:36:05 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=1627484014; cv=none; d=zohomail.com; s=zohoarc; b=FxjapOeHQO7bjui8teNY/349/S2K68TWQtF+ZEALNAKKMZa+wfSmgz4S05qLjKyzjjUqGMXUmK030H8FtfxfvaUp8j2IGtHzv8wr1V4AnwENuuk2tP2Ujb40SFx3f7RwWFcr1ytqtUciEyABbkgyYkrKBbhJaLMxalVpIg0HsMw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1627484014; 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=4UOe5b2fx3Fh6u6Lqilgce/bFIZGpTYCRKnCgcNYEYs=; b=m9A5Ll8aIEm5RDmglD7rO7GjAp3Q1SAJPUePpxbyYmWvRNwOxx161KYMmaDRRSgSoyE/vvnzGdZ7/HUn6D3iEfMhZAuYQnEet8RgkAKrvK3KJeRHwOmq/VedIb7ZuC97+8wLzj9eRlzhAYcOWOsTKyxm9pOjJohpVs8TGHCO+Vc= 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 1627484014760957.114271167909; Wed, 28 Jul 2021 07:53:34 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.161584.296598 (Exim 4.92) (envelope-from ) id 1m8kvs-000370-Mu; Wed, 28 Jul 2021 14:53:16 +0000 Received: by outflank-mailman (output) from mailman id 161584.296598; Wed, 28 Jul 2021 14:53: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 1m8kvs-00036r-JY; Wed, 28 Jul 2021 14:53:16 +0000 Received: by outflank-mailman (input) for mailman id 161584; Wed, 28 Jul 2021 14:53:15 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kvr-0002W0-Ov for xen-devel@lists.xenproject.org; Wed, 28 Jul 2021 14:53:15 +0000 Received: from mail-pj1-x1035.google.com (unknown [2607:f8b0:4864:20::1035]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 7f5c277f-66a6-4317-8763-05643c208ad8; Wed, 28 Jul 2021 14:53:12 +0000 (UTC) Received: by mail-pj1-x1035.google.com with SMTP id ds11-20020a17090b08cbb0290172f971883bso10454369pjb.1 for ; Wed, 28 Jul 2021 07:53:12 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:0:3823:141e:6d51:f0ad]) by smtp.gmail.com with ESMTPSA id n134sm277558pfd.89.2021.07.28.07.53.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 07:53:11 -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: 7f5c277f-66a6-4317-8763-05643c208ad8 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=4UOe5b2fx3Fh6u6Lqilgce/bFIZGpTYCRKnCgcNYEYs=; b=WDKkQI+4pFS1qUeyUk57PY1MjkBYohzBWIb2Shmz+3ikDL79tSqwvQaiBZIwR19VSV dtuCv7UwmkIiIpSVydgmRXsq4SXT58K1jkHIDA+Ja2eNYeQ+JfYhBSLGJgIqMWT+bc9u jn9DFsvsJFVEm/mT3Qd5QMUaScTC2c6I0EcwfuMOiA09m8OnLuDcKGustBEmOhByCNCb UdKibxAfAhQ0GVglWURs6ArttH3DtZw4RGMYEuyymQ5OdshzupJ3JETzCcjFbONxafd0 wu37AbNCS4KyX/8swZ0UP0x5U0PV56Djw2JcQAFar7BoDTk/eaysFTu3H9fIm7n1xXs5 FvuA== 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=4UOe5b2fx3Fh6u6Lqilgce/bFIZGpTYCRKnCgcNYEYs=; b=mSBp0J3XGj/vLfDjnXNAVCOfGzGwl547PgIm0nAyRAIH4+6+/PfIoJO3Yt2hl3xQ2i 4RfUjfb5345SFHkKKbsMzLo72m/Zfuccgp6o0yrw9oboCKsd0+GuwUjKlwZVJ8M6jrDl NK8/8Ik9Z21LXW+bndSH5fRqcXoYBjQGmIN0TgCp3BrsbEd/CsKLV7djFG4A/EFkhJnm Cei24hDcuya4t8/kK1M5uKEsIH+Ty9QO1QmB//Te96LXMaQrgpVbUi+E524nTVSlBkHl wGAqR37BN9FVY/52/5lX97+h92CQm3q46l6LvbfYUknMqFy8JA5xoq6Ou9IDXmCi8+1I spmg== X-Gm-Message-State: AOAM5338Tjj4o0kG6M60EC2bF48XcfzWklVOF3jm+MfSUGcVLPXcFFuh /fvty/kHySRpCE2/j6OIRSM= X-Google-Smtp-Source: ABdhPJyc8/U4Ariizb4QvY5nGICIcZLhQ4VEvgrpEwjxD9fWyR7o9OBbxbMiUCjC06cPGD3+3H7lLQ== X-Received: by 2002:a65:610c:: with SMTP id z12mr151324pgu.453.1627483991403; Wed, 28 Jul 2021 07:53:11 -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, rientjes@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, rppt@kernel.org, kirill.shutemov@linux.intel.com, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, pgonda@google.com, david@redhat.com, keescook@chromium.org, hannes@cmpxchg.org, sfr@canb.auug.org.au, 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, anparri@microsoft.com Subject: [PATCH 02/13] x86/HV: Initialize shared memory boundary in the Isolation VM. Date: Wed, 28 Jul 2021 10:52:17 -0400 Message-Id: <20210728145232.285861-3-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210728145232.285861-1-ltykernel@gmail.com> References: <20210728145232.285861-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1627484016247100001 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 dcfbd2770d7f..773e84e134b3 100644 --- a/arch/x86/kernel/cpu/mshyperv.c +++ b/arch/x86/kernel/cpu/mshyperv.c @@ -327,6 +327,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 Thu May 2 22:36:05 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=1627484014; cv=none; d=zohomail.com; s=zohoarc; b=QteHXa8FvooYFkolWyBshSVULFz/O3x4YpatgXEg5fzywRRS0Ez5thFr6cGHBjKJ3mBQGqTgtUrFCN0zAcz+ba4wUoaYQUuWxuhPWJcGug0YxuxUuDEXlB4HdMWndywGGZ+yFIBTf9vRBbPEt1GJ++QQnELRihp6XsjY7dqPBdE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1627484014; 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=/79DzCyIHtWmaT+NzmaxtEIMVXl5WnXl2WxqKtTVhJc=; b=HtlSGGDA+Dqw8VFtsifLUYg4hgZk/RvUigZuVBZs43FJY+ezT9PockgKWiCd5KDuVHaI4rZp6m3pR255m7XwppeV6YF9AskyiIeIUA1XQRhxfzLp024b/ebWF+R0RKWXapbdzTRD01nCyvJKfzetic+ner/+SKxMLkEH5Dn2KW8= 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 1627484014731914.1839203103411; Wed, 28 Jul 2021 07:53:34 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.161586.296610 (Exim 4.92) (envelope-from ) id 1m8kvy-0003V3-5G; Wed, 28 Jul 2021 14:53:22 +0000 Received: by outflank-mailman (output) from mailman id 161586.296610; Wed, 28 Jul 2021 14:53:22 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kvy-0003Ul-0D; Wed, 28 Jul 2021 14:53:22 +0000 Received: by outflank-mailman (input) for mailman id 161586; Wed, 28 Jul 2021 14:53:20 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kvw-0002W0-P1 for xen-devel@lists.xenproject.org; Wed, 28 Jul 2021 14:53:20 +0000 Received: from mail-pl1-x62a.google.com (unknown [2607:f8b0:4864:20::62a]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 084ba6f8-18b9-469b-a169-0c8027a1b045; Wed, 28 Jul 2021 14:53:15 +0000 (UTC) Received: by mail-pl1-x62a.google.com with SMTP id d17so2972900plh.10 for ; Wed, 28 Jul 2021 07:53:15 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:0:3823:141e:6d51:f0ad]) by smtp.gmail.com with ESMTPSA id n134sm277558pfd.89.2021.07.28.07.53.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 07:53:13 -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: 084ba6f8-18b9-469b-a169-0c8027a1b045 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=/79DzCyIHtWmaT+NzmaxtEIMVXl5WnXl2WxqKtTVhJc=; b=hGNxhplSujZA86RagN4zqWeSm8MjGR5wsvAT6oDexvY5ZHXdLk4NaBmTsOL4UfVIs0 Fgg8HC7TYWh4fbmWrK9RdUrXoutrRqx258TtIIXYra8pcljEwhXOabhl0XaomgA4aGWn wND/7qeQcDE2tZpWMcyVxz4jpTpz38Y8zzEpeZwN4GDCT2fyAwTnD4bFjIw2GcgaF7ij VoZn1AQgJwULyrZlqzRfue6mnJ6CNT2iwHGMjkFHIugZbut1hzYeU0IIXZ1zixsHIE48 X0HkVdO4JHgMLvVpPhNGAV2T7z3SQ/h5Ax416TT0V3MSYhMM8SFYqNP622Niak1n+uJ7 jVHA== 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=/79DzCyIHtWmaT+NzmaxtEIMVXl5WnXl2WxqKtTVhJc=; b=seB1cnAmHdQgSfdlS0qcoKgClTEMMTLieKb1J7AfBLwFPPKJXP1V4gM0A3MKrzY2ZN DmvttQPStZ/bqHkQlxfRZIr7SRwF1BZJWZVwpXjjDkRE66Z15MjKVVCXCsSItR+gAP36 cec2fmTOQpzeJjxKebxSOTo/p/qBjM3UwXL59P8jvGPFVlR2adacFDIM/BQFYUQUcGgm /ukTlZs5r2BoiWDwx7/ws3Z/ZF3D62Jdxahy4cppvy5991j0rjuhXVBdAdR6cQTeNnBV QpM4V6az63pBQ9eIdwI2LagSh+hGWYBWwHx0UMT5T5QKGTuCAW584Ehcll0R1LEXshbM JJ/A== X-Gm-Message-State: AOAM533o0H0wxmlMoU6GikZiWbZEOQx7fyjaxPft2cJaEDofJ10KvMaz q6G2V6SLtJbFhpopWmdfJYk= X-Google-Smtp-Source: ABdhPJxgNPNARBGy7DDQhfhzd75Fkf5dcYm6KNp/rI8FF8zLjPwrqJZeCTVRgpl6JWXIpl/EXBulxQ== X-Received: by 2002:a17:90b:1612:: with SMTP id la18mr233349pjb.95.1627483994252; Wed, 28 Jul 2021 07:53:14 -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, rientjes@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, rppt@kernel.org, kirill.shutemov@linux.intel.com, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, pgonda@google.com, david@redhat.com, keescook@chromium.org, hannes@cmpxchg.org, sfr@canb.auug.org.au, 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, anparri@microsoft.com Subject: [PATCH 03/13] x86/HV: Add new hvcall guest address host visibility support Date: Wed, 28 Jul 2021 10:52:18 -0400 Message-Id: <20210728145232.285861-4-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210728145232.285861-1-ltykernel@gmail.com> References: <20210728145232.285861-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1627484016273100003 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(). Signed-off-by: Tianyu Lan --- arch/x86/hyperv/Makefile | 2 +- arch/x86/hyperv/ivm.c | 112 +++++++++++++++++++++++++++++ arch/x86/include/asm/hyperv-tlfs.h | 18 +++++ arch/x86/include/asm/mshyperv.h | 3 +- arch/x86/mm/pat/set_memory.c | 6 +- include/asm-generic/hyperv-tlfs.h | 1 + 6 files changed, 139 insertions(+), 3 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/ivm.c b/arch/x86/hyperv/ivm.c new file mode 100644 index 000000000000..24a58795abd8 --- /dev/null +++ b/arch/x86/hyperv/ivm.c @@ -0,0 +1,112 @@ +// 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[], u32 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); + +/* + * 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. + */ +static int hv_set_mem_host_visibility(void *kbuffer, size_t size, u32 visi= bility) +{ + int pagecount =3D size >> HV_HYP_PAGE_SHIFT; + 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; +} + +int hv_set_mem_enc(unsigned long addr, int numpages, bool enc) +{ + return hv_set_mem_host_visibility((void *)addr, + numpages * HV_HYP_PAGE_SIZE, + enc ? VMBUS_PAGE_NOT_VISIBLE + : VMBUS_PAGE_VISIBLE_READ_WRITE); +} diff --git a/arch/x86/include/asm/hyperv-tlfs.h b/arch/x86/include/asm/hype= rv-tlfs.h index f1366ce609e3..f027b5bf6076 100644 --- a/arch/x86/include/asm/hyperv-tlfs.h +++ b/arch/x86/include/asm/hyperv-tlfs.h @@ -276,6 +276,11 @@ 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 GPA map flags */ +#define VMBUS_PAGE_NOT_VISIBLE 0 +#define VMBUS_PAGE_VISIBLE_READ_ONLY 1 +#define VMBUS_PAGE_VISIBLE_READ_WRITE 3 + /* * Declare the MSR used to setup pages used to communicate with the hyperv= isor. */ @@ -578,4 +583,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..68dd207c2603 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -190,7 +190,8 @@ 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[], u32 visibility); +int hv_set_mem_enc(unsigned long addr, int numpages, bool enc); #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..ba2a22886976 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 @@ -1986,7 +1988,9 @@ static int __set_memory_enc_dec(unsigned long addr, i= nt numpages, bool enc) int ret; =20 /* Nothing to do if memory encryption is not active */ - if (!mem_encrypt_active()) + if (hv_is_isolation_supported()) + return hv_set_mem_enc(addr, numpages, enc); + else if (!mem_encrypt_active()) return 0; =20 /* Should not be working on unaligned addresses */ 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 --=20 2.25.1 From nobody Thu May 2 22:36:05 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=1627484021; cv=none; d=zohomail.com; s=zohoarc; b=iGtf/u8m6lhkW0DGZ7svp/peg6VQVcifIp3HZQZ1LHxbnfVeIx8VJWmW9yPhNkoNA3F9CCbU6gyqhRE9MXENdkQL2Rz/6kuHcE9xvv1vRvNIC9cViqjUynnBovu3IVWUIcEIZBkOjVHYIFKoRjXAkLk9SrZTtDXiOU3xJ/bDibE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1627484021; 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=+XJfA3AqCHkXuREWq5JYX1SJ1GS8dmwfVs7cs/uDyzo=; b=PJe2TuTwS7lBU4hsjWAw4zT6YAumJcSv9lA8M9JygEf3PSw/yk3G9iyMFwJk6JKDXdJ7H9cCLNKzd9JocXBN14NEmC9ZOgN8BzPdHMMznKHjqvhMvrEsSMubgjdqhYR0corKahQ+qpzah1FBxAveR6+zuo97yt7ODFoX56LA7Hc= 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 1627484021158912.3411718544223; Wed, 28 Jul 2021 07:53:41 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.161588.296622 (Exim 4.92) (envelope-from ) id 1m8kw2-0003t1-EV; Wed, 28 Jul 2021 14:53:26 +0000 Received: by outflank-mailman (output) from mailman id 161588.296622; Wed, 28 Jul 2021 14:53:26 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kw2-0003sq-Ap; Wed, 28 Jul 2021 14:53:26 +0000 Received: by outflank-mailman (input) for mailman id 161588; Wed, 28 Jul 2021 14:53:25 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kw1-0002W0-P8 for xen-devel@lists.xenproject.org; Wed, 28 Jul 2021 14:53:25 +0000 Received: from mail-pl1-x62a.google.com (unknown [2607:f8b0:4864:20::62a]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 1221c58a-84a1-44a0-913d-d0033ac9bb2a; Wed, 28 Jul 2021 14:53:17 +0000 (UTC) Received: by mail-pl1-x62a.google.com with SMTP id q2so2966678plr.11 for ; Wed, 28 Jul 2021 07:53:17 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:0:3823:141e:6d51:f0ad]) by smtp.gmail.com with ESMTPSA id n134sm277558pfd.89.2021.07.28.07.53.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 07:53:16 -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: 1221c58a-84a1-44a0-913d-d0033ac9bb2a 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=+XJfA3AqCHkXuREWq5JYX1SJ1GS8dmwfVs7cs/uDyzo=; b=JTfxH2MI6lr3vhghm+bYn1lDtWC6S40YgGpd2n5ZBE0WXJ57nPRp/vMeCnq6SmO90h UPiRb39uM4iMAgW0slMgb6ATsLC4Z0lW9pf+AZc7pl5WJGMDawbbzgXKBt1Z+GuOXtly 9TYhzeKXWg55tjzDSrNqnVlbyUVgZ1VhgspLUJ3MDp7VmLIaFpFkVzlACYJoXgx4h9W9 y5VivpznJ4FNs2S4rqNSmRUG+RlcTHOQjC6dLg1qrzPm0TcGvPf/VqvCVComHCJhCA2P 7AXzP0K57z4DE458hjOtv6x4NMiUz3iK84iyApsE6NxjF0AupgOoBJTCCu7r53H4pSie WfpA== 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=+XJfA3AqCHkXuREWq5JYX1SJ1GS8dmwfVs7cs/uDyzo=; b=GQiN3wbkiDDRL4G7/YmIIdyY4veaczrDS8I4DTDbCWdgsvJDeh3pr7um+KNUL8mm4G zju7WnWuLMDSDzGgtWzp1tFoUoVOAPe8+HoCXyOeqt/97C7xStJ+Wx7GQSnYZ1bjeyaB iiyY1jicYH8bizBlXLYukKYRRGVkGvCGTw/5e5TQBIw1Wd6hyAm5YSniZCORxMoi22Ql +a2PuIqlK4mlAD6TkuTxUfr+Mxj5SNgo6bsPIxFaSgHD1iRwAgbQJoRDMvxsUSIoK9N8 To6h3C9xClwOVfueRgQmxCiXV8Gpd/znJkWfvIlZwPsnuVoFqTi+XyckV8w1AwJ+/3GJ N2TA== X-Gm-Message-State: AOAM532PgkrEoK81roj8QYnhE5fjHAiXhOtQJSpQs+HSxdeqL0vXFmzg ySYtMS8kidbv2qmfwGrEfc8= X-Google-Smtp-Source: ABdhPJz4j/vsLQnZny1GE3WB7JJRgcpBrn921B99u5//TBJ+oOZxL5FLeGsOUc3yuQEVykF8bbeFJQ== X-Received: by 2002:a65:654c:: with SMTP id a12mr224730pgw.118.1627483996980; Wed, 28 Jul 2021 07:53:16 -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, rientjes@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, rppt@kernel.org, kirill.shutemov@linux.intel.com, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, pgonda@google.com, david@redhat.com, keescook@chromium.org, hannes@cmpxchg.org, sfr@canb.auug.org.au, 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, anparri@microsoft.com Subject: [PATCH 04/13] HV: Mark vmbus ring buffer visible to host in Isolation VM Date: Wed, 28 Jul 2021 10:52:19 -0400 Message-Id: <20210728145232.285861-5-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210728145232.285861-1-ltykernel@gmail.com> References: <20210728145232.285861-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1627484022880100001 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 | 38 ++++++++++++++++++++++++++++++++++++-- include/linux/hyperv.h | 10 ++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/drivers/hv/channel.c b/drivers/hv/channel.c index f3761c73b074..01048bb07082 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,7 @@ static int __vmbus_establish_gpadl(struct vmbus_channel= *channel, struct list_head *curr; u32 next_gpadl_handle; unsigned long flags; - int ret =3D 0; + int ret =3D 0, index; =20 next_gpadl_handle =3D (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1); @@ -474,6 +475,13 @@ static int __vmbus_establish_gpadl(struct vmbus_channe= l *channel, if (ret) return ret; =20 + ret =3D set_memory_decrypted((unsigned long)kbuffer, + HVPFN_UP(size)); + if (ret) { + pr_warn("Failed to set host visibility.\n"); + return ret; + } + init_completion(&msginfo->waitevent); msginfo->waiting_channel =3D channel; =20 @@ -539,6 +547,15 @@ 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 + if (type =3D=3D HV_GPADL_BUFFER) + index =3D 0; + else + index =3D channel->gpadl_range[1].gpadlhandle ? 2 : 1; + + channel->gpadl_range[index].size =3D size; + channel->gpadl_range[index].buffer =3D kbuffer; + channel->gpadl_range[index].gpadlhandle =3D *gpadl_handle; + cleanup: spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); list_del(&msginfo->msglistentry); @@ -549,6 +566,11 @@ static int __vmbus_establish_gpadl(struct vmbus_channe= l *channel, } =20 kfree(msginfo); + + if (ret) + set_memory_encrypted((unsigned long)kbuffer, + HVPFN_UP(size)); + return ret; } =20 @@ -811,7 +833,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 +881,18 @@ 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_range[i].gpadlhandle =3D=3D gpadl_handle) + break; + + if (set_memory_encrypted((unsigned long)channel->gpadl_range[i].buffer, + HVPFN_UP(channel->gpadl_range[i].size))) + pr_warn("Fail to set mem host visibility.\n"); + + channel->gpadl_range[i].gpadlhandle =3D 0; + return ret; } EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl); diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h index 2e859d2f9609..06eccaba10c5 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -809,6 +809,14 @@ struct vmbus_device { =20 #define VMBUS_DEFAULT_MAX_PKT_SIZE 4096 =20 +struct vmbus_gpadl_range { + u32 gpadlhandle; + u32 size; + void *buffer; +}; + +#define VMBUS_GPADL_RANGE_COUNT 3 + struct vmbus_channel { struct list_head listentry; =20 @@ -829,6 +837,8 @@ struct vmbus_channel { struct completion rescind_event; =20 u32 ringbuffer_gpadlhandle; + /* GPADL_RING and Send/Receive GPADL_BUFFER. */ + struct vmbus_gpadl_range gpadl_range[VMBUS_GPADL_RANGE_COUNT]; =20 /* Allocated memory for ring buffer */ struct page *ringbuffer_page; --=20 2.25.1 From nobody Thu May 2 22:36:05 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=1627484026; cv=none; d=zohomail.com; s=zohoarc; b=BWIp9gbtEKf/bwvX9fwi4FYWlFOGXwByy66hGPYuzJHUXcoU999ct3YfLmsYVFRAhl2Tvj8kFRDnLnTspzVqS3GqAAH4rg5wPDaIUou3w3gBYXYG/cgJyyF/VrAdX0GUWq6nhdZulQLYUwe4pPGYMHNL7oTQSVBH73QQBS1IqHQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1627484026; 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=HB97a+2OhA3eQm24fC97PNoK0NDP1pJHHUMQuiKyYaM=; b=Ozd3kCNEvLGo9LB+CI8Jy/ZMOV7zl/43y8x0dVL0Dkx8ORoV5gdB0OeCNsFlujFbW+I8PDl27irVHPZHN7D5874Zu26yx/YznbHq5yP5AE9Y9QegfScr1rPOJhmxxSmHLwoQfIi4n3WGFbJWu7G9WNniKFuNs73kxr+uXE1SyjE= 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 1627484026047653.0713232052931; Wed, 28 Jul 2021 07:53:46 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.161591.296634 (Exim 4.92) (envelope-from ) id 1m8kw7-0004XC-OA; Wed, 28 Jul 2021 14:53:31 +0000 Received: by outflank-mailman (output) from mailman id 161591.296634; Wed, 28 Jul 2021 14:53:31 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kw7-0004Wz-Kf; Wed, 28 Jul 2021 14:53:31 +0000 Received: by outflank-mailman (input) for mailman id 161591; Wed, 28 Jul 2021 14:53:30 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kw6-0002W0-PB for xen-devel@lists.xenproject.org; Wed, 28 Jul 2021 14:53:30 +0000 Received: from mail-pj1-x1036.google.com (unknown [2607:f8b0:4864:20::1036]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id fa2e4d68-e223-437c-8f25-ded714f106b5; Wed, 28 Jul 2021 14:53:20 +0000 (UTC) Received: by mail-pj1-x1036.google.com with SMTP id mz5-20020a17090b3785b0290176ecf64922so10410370pjb.3 for ; Wed, 28 Jul 2021 07:53:20 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:0:3823:141e:6d51:f0ad]) by smtp.gmail.com with ESMTPSA id n134sm277558pfd.89.2021.07.28.07.53.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 07:53:19 -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: fa2e4d68-e223-437c-8f25-ded714f106b5 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=HB97a+2OhA3eQm24fC97PNoK0NDP1pJHHUMQuiKyYaM=; b=VtdJuUxlciBl6pJKd+T5kPRnYqd4ADa8ubLACu1CVtNEwjYJlxr6eDq4+Hgfya+Te6 PvnGqgUHTKpdkX0DI5yBZrcTH08G9xfi8ByRylJS+rQNdBVLN52e1QC99fLLVE4Gewuk wsLXwPZKb9LDn1HRTpPaZXhsqfC/c04IGxbNz35Gi3i7k1h7G/XDTXWKSyXE1UaFWPY5 onQqhH0PUc2MzGtKX+YPuIlyMYvuZmSeK3fAQ5U4fCdjCpsRy+OqjnMlxUwK/ToqunsD Cfb0x4z6CqpNpUexwa90vvWfcWZHgWT0i4DgoK6is5msDBDI/SrDjm3ravlTkHksJsLc 9uYA== 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=HB97a+2OhA3eQm24fC97PNoK0NDP1pJHHUMQuiKyYaM=; b=BAHE4dcwp0htfADRy6z3ubMm8zLhiACZ4TbYL5I55Xn3Bzx+1euksMuxaQDBlTVZ7Z Z4lefyGU5DuXN/SuSHR7Y4awhwKcvojncNf29E7otHRL4sEeMBs7JM3DluX5sjyzn00B bs4xR4FqYVkDDzd8UVYwEmLSMy7WR4jLvUOT+02nHneXUF6Tn+5vxJbPvbIk8GmBjy36 Cvcf1p8nbxls+bxnLm0mpUPCYCjGCJAKMvsuJOg6lP3JYiZn7eryaIHwG3KY47stJyU8 22DPTMU3esFTQ82SGWC71gyLwd6BJ6r7QZQYie8OCheX3rvWsB1eb1JuXdNE04RYMA4Y KyRA== X-Gm-Message-State: AOAM530VDQokk+8z1BOBoutGneJrcGZnIPjabuDq4kNmlVqBeFbia+hL +L0FfuETf8hfMwVXCUPUkPk= X-Google-Smtp-Source: ABdhPJwJR0YDD/874EWanOoXxDHqUKtLEIWjbHNtwnsflKAdj5/12656WWege/+q5qLjYQ2qOK3pTw== X-Received: by 2002:a63:b953:: with SMTP id v19mr228948pgo.40.1627483999761; Wed, 28 Jul 2021 07:53:19 -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, rientjes@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, rppt@kernel.org, kirill.shutemov@linux.intel.com, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, pgonda@google.com, david@redhat.com, keescook@chromium.org, hannes@cmpxchg.org, sfr@canb.auug.org.au, 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, anparri@microsoft.com Subject: [PATCH 05/13] HV: Add Write/Read MSR registers via ghcb page Date: Wed, 28 Jul 2021 10:52:20 -0400 Message-Id: <20210728145232.285861-6-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210728145232.285861-1-ltykernel@gmail.com> References: <20210728145232.285861-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1627484027733100001 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 --- arch/x86/hyperv/hv_init.c | 16 +---- arch/x86/hyperv/ivm.c | 114 ++++++++++++++++++++++++++++++ arch/x86/include/asm/mshyperv.h | 78 +++++++++++++++++++- arch/x86/include/asm/sev.h | 4 ++ arch/x86/kernel/cpu/mshyperv.c | 3 + arch/x86/kernel/sev-shared.c | 21 ++++-- drivers/hv/hv.c | 121 ++++++++++++++++++++++---------- include/asm-generic/mshyperv.h | 12 +++- 8 files changed, 307 insertions(+), 62 deletions(-) diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c index ee449c076ef4..b99f6b3930b7 100644 --- a/arch/x86/hyperv/hv_init.c +++ b/arch/x86/hyperv/hv_init.c @@ -392,7 +392,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 @@ -485,6 +485,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, @@ -558,16 +559,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) -{ - return hv_get_isolation_type() !=3D HV_ISOLATION_TYPE_NONE; -} diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index 24a58795abd8..9c30d5bb7b64 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,118 @@ #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; + } + + memset(hv_ghcb, 0x00, HV_HYP_PAGE_SIZE); + + 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(&hv_ghcb->ghcb, NULL, 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; + } + + memset(hv_ghcb, 0x00, HV_HYP_PAGE_SIZE); + + ghcb_set_rcx(&hv_ghcb->ghcb, msr); + if (sev_es_ghcb_hv_call(&hv_ghcb->ghcb, NULL, 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 68dd207c2603..3c0cafdf7309 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); @@ -192,6 +249,25 @@ int hv_map_ioapic_interrupt(int ioapic_id, bool level,= int vcpu, int vector, int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *en= try); int hv_mark_gpa_visibility(u16 count, const u64 pfn[], u32 visibility); int hv_set_mem_enc(unsigned long addr, int numpages, bool enc); +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) {} @@ -208,9 +284,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..4249fde0a30e 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -81,6 +81,10 @@ 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(struct ghcb *ghcb, + struct es_em_ctxt *ctxt, + 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 773e84e134b3..46a09cdfa77a 100644 --- a/arch/x86/kernel/cpu/mshyperv.c +++ b/arch/x86/kernel/cpu/mshyperv.c @@ -332,6 +332,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..e039e55b9c72 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -94,10 +94,10 @@ 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(struct ghcb *ghcb, + struct es_em_ctxt *ctxt, + u64 exit_code, u64 exit_info_1, + u64 exit_info_2) { enum es_result ret; =20 @@ -109,7 +109,16 @@ 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)); + /* + * Hyper-V runs paravisor with SEV. Ghcb page is allocated by + * paravisor and not needs to be updated in the Linux guest. + * Otherwise, the ghcb page's PA reported by paravisor is above + * VTOM. Hyper-V use this function with NULL for ctxt point and + * skip setting ghcb page in such case. + */ + if (ctxt) + sev_es_wr_ghcb_msr(__pa(ghcb)); + VMGEXIT(); =20 if ((ghcb->save.sw_exit_info_1 & 0xffffffff) =3D=3D 1) { @@ -120,7 +129,7 @@ static enum es_result sev_es_ghcb_hv_call(struct ghcb *= ghcb, v =3D info & SVM_EVTINJ_VEC_MASK; =20 /* Check if exception information from hypervisor is sane. */ - if ((info & SVM_EVTINJ_VALID) && + if (ctxt && (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; 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 aa26d24a5ca9..b0cfc25dffaa 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 Thu May 2 22:36:05 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=1627484032; cv=none; d=zohomail.com; s=zohoarc; b=khU0MrvwFMAgHJVQO4FPtadjH/SLdExhXmavkxIiDFi9rOfPh8g4G+vBN1vGDKhKpvTbFolupAyM6rGfXJUthCi2KoHxqV6UDVenc34TjDOiPbROylU79gwl9V1p0N+9aXLkG19WUnI59UYwnuC8McFEMbU1xVXBfe5GtmuuMis= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1627484032; 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=v51HbVj9oUgMShtZEj3Pp1xicbgTTaQMmcrpxVSeHWs=; b=fb2lWWoA89bCA+gwyoDNMkQwiWPaHsfptUNy4fG6bk8tEZins/EKiIeCcl1lUHpqL90RGZUTkhQeJvr+FF/C/MZYuvefOMsXPPVuzcvVlD1Vp7a31oQAtYyRBZtJLzLINr4bhDPzC/Cn2ovVv2hjnHL+Yqg7QAkGamiMgOFOGTo= 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 1627484032753657.2183398826319; Wed, 28 Jul 2021 07:53:52 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.161598.296646 (Exim 4.92) (envelope-from ) id 1m8kwD-0005C4-7o; Wed, 28 Jul 2021 14:53:37 +0000 Received: by outflank-mailman (output) from mailman id 161598.296646; Wed, 28 Jul 2021 14:53:37 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kwD-0005Bp-48; Wed, 28 Jul 2021 14:53:37 +0000 Received: by outflank-mailman (input) for mailman id 161598; Wed, 28 Jul 2021 14:53:35 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kwB-0002W0-PU for xen-devel@lists.xenproject.org; Wed, 28 Jul 2021 14:53:35 +0000 Received: from mail-pl1-x635.google.com (unknown [2607:f8b0:4864:20::635]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id f99b2e51-5a8c-462d-a7dd-48dfdefccb59; Wed, 28 Jul 2021 14:53:22 +0000 (UTC) Received: by mail-pl1-x635.google.com with SMTP id c16so2994308plh.7 for ; Wed, 28 Jul 2021 07:53:22 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:0:3823:141e:6d51:f0ad]) by smtp.gmail.com with ESMTPSA id n134sm277558pfd.89.2021.07.28.07.53.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 07:53:22 -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: f99b2e51-5a8c-462d-a7dd-48dfdefccb59 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=v51HbVj9oUgMShtZEj3Pp1xicbgTTaQMmcrpxVSeHWs=; b=J1bP3zz2e9Ky6Qac+aF/nab3UrGFJCXZcJO+f4RIMpnMf5/FPnldo0qUWUtq//NT5y gC/R4rItPntl/y/ciDSd9VWz04QFZe3XcST16OzWZtFjSEz1aVUAeDJ3QtRE2cSWojXO cZ75GJsU4hDfJfqFP8o6hN6kwUJtfSeEk21602ZW7rRvu7mNJyBe8lvmT9orbv2qW+P1 XUJ2oymKMxvbkhA8y5/wvIzfEbXg0UZ8APbjtFUVxdcyM0fkc0tM4I993BegwvwzHhPo XwT+MQZ2cphoNI70/JrvceBhTCfttWejvlYS6aYsvD62kE1r33z8ezadatHFd9fbrpvF GEBg== 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=v51HbVj9oUgMShtZEj3Pp1xicbgTTaQMmcrpxVSeHWs=; b=Wo4DTXAYU/PXTog+hvvooYiB61LoRiLqkv9Zz1Z1rJWNF5SSzQB4isBXZ4CCVbuZyH kfddNe19kzPIYTWXVv2cj2mfV7am7gLvC7WkteT+42uPL4YGr0WBEZsJMGXPZ89Qyq44 NgXBiWcGpFgbzy3E2KeugpNStt72W7GG5iPalfjimvYj3LV27pj6sgkiLcdVNCXpUNOk mlp29EO6qvlDg2L2ccCmtxmz5TFF1EXEbqphieBCvFmDTO0mKK6C3X/hHYW2e1WLMkA2 6KIODUfbgnZHU/FzPhxMNO7hBHJKin5SEXYMMuLlSfMq1Cn2NJrHtfmNURdq1WKlV4hR CVVg== X-Gm-Message-State: AOAM533Xv2OARwuQBrNRoAGxynH4+7kY2YR6s652eVaHxoPrROhGJXa7 VUc4fIjE9JJEIGiaO4usJvM= X-Google-Smtp-Source: ABdhPJx53zStKiyjEMPaVE5CG/AOPZ91uL54k+pFpCVnyB2dlyAFOANHVd5oiIcOg5eiMuEJpQ9u6g== X-Received: by 2002:a63:ef02:: with SMTP id u2mr211497pgh.298.1627484002239; Wed, 28 Jul 2021 07:53:22 -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, rientjes@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, rppt@kernel.org, kirill.shutemov@linux.intel.com, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, pgonda@google.com, david@redhat.com, keescook@chromium.org, hannes@cmpxchg.org, sfr@canb.auug.org.au, 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, anparri@microsoft.com Subject: [PATCH 06/13] HV: Add ghcb hvcall support for SNP VM Date: Wed, 28 Jul 2021 10:52:21 -0400 Message-Id: <20210728145232.285861-7-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210728145232.285861-1-ltykernel@gmail.com> References: <20210728145232.285861-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1627484034777100001 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 | 42 +++++++++++++++++++++++++++++++++ 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, 84 insertions(+), 2 deletions(-) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index 9c30d5bb7b64..13bab7f07085 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -15,6 +15,48 @@ #include #include =20 +u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_si= ze) +{ + union hv_ghcb *hv_ghcb; + void **ghcb_base; + unsigned long flags; + + if (!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; + } + + memset(hv_ghcb, 0x00, HV_HYP_PAGE_SIZE); + hv_ghcb->ghcb.protocol_version =3D 1; + hv_ghcb->ghcb.ghcb_usage =3D 1; + + 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 3c0cafdf7309..8bf26e6e7055 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -254,6 +254,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 b0cfc25dffaa..317d2a8d9700 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 Thu May 2 22:36:05 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=1627484036; cv=none; d=zohomail.com; s=zohoarc; b=Jy1iTSUIasllAjsgK6U8OgiIV9Pqan3irtzMnOp8nnU0OZHuIBPUKK5KHR0FaZIQp5oq+kaCD8rftkkxlT5XVh9BSjlygbl9TgU+dKz7h2+HL7YUKYCp/DNjMP2N8x0ZylexAaKbHOxWBpYlviGpdpYCKZzIQ0WDBQW3afH8Vrw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1627484036; 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=VC37tGvMnXRZFIidUDGGN3vUKeS5FmCKfybWMaE6JMY=; b=hG6qQ6llml+hlQkCHd4rxY0HCxL4c4onaExb838TXg34MFpVVxUde6uEgJeabne/Fx3sEi4iHfwSQhmW43xodI9RLKatFuGbdCLkf3tiguUx8JTMgEfwjACIDrxrOLPY5r5Y+uh83eGn44tP/ZakUy1qj2kt6+2TTMfax4G9cAk= 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 1627484036405351.1137387108645; Wed, 28 Jul 2021 07:53:56 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.161602.296658 (Exim 4.92) (envelope-from ) id 1m8kwI-0005kE-Hr; Wed, 28 Jul 2021 14:53:42 +0000 Received: by outflank-mailman (output) from mailman id 161602.296658; Wed, 28 Jul 2021 14:53: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 1m8kwI-0005k5-EL; Wed, 28 Jul 2021 14:53:42 +0000 Received: by outflank-mailman (input) for mailman id 161602; Wed, 28 Jul 2021 14:53: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 1m8kwG-0002W0-Pm for xen-devel@lists.xenproject.org; Wed, 28 Jul 2021 14:53:40 +0000 Received: from mail-pl1-x633.google.com (unknown [2607:f8b0:4864:20::633]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id ecf234f3-4b96-4653-a1f0-b06fca0c9308; Wed, 28 Jul 2021 14:53:25 +0000 (UTC) Received: by mail-pl1-x633.google.com with SMTP id t21so2955111plr.13 for ; Wed, 28 Jul 2021 07:53:25 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:0:3823:141e:6d51:f0ad]) by smtp.gmail.com with ESMTPSA id n134sm277558pfd.89.2021.07.28.07.53.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 07:53:24 -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: ecf234f3-4b96-4653-a1f0-b06fca0c9308 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=VC37tGvMnXRZFIidUDGGN3vUKeS5FmCKfybWMaE6JMY=; b=lh23Rm3aSU+fQaWGX59d9ft0lbpKg8YIgoW0ciAzDdoAicxxQA0REzzkXmGjNCrEGP 4pj1gCuNR9MTox0HLcJ503kNlHTUux4o7GZcKj/lKGsRUPw3RikRbxR7SpBPKWxYqYVu xAkYNbZiR4WUrdjn5boJtiK7CDtDNXreVlifiqilqBV4N4cuNWP0fTtRbA3rbN6pN/1s RYiI6hEL9EIBT5ckmBGD6ZOx1hpJvBVU8Xhh1J6gBmIKH8LauQKmeMyjVtVR+wWihnkz OcxTQVpkPzEcil6Nqk2MWCZvGf7dWM8uECT0LE5ukPyeTp3SNM2AGww5Jr8E4esn0lwY 7JMA== 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=VC37tGvMnXRZFIidUDGGN3vUKeS5FmCKfybWMaE6JMY=; b=R+avIWfUonXhpT9MooaI2IqWOwd1vh/oB6Y9mqyjxfdu9lvh5W3wigo6gNA8DrOjH2 PZW3U+nWeTt9xdKbff1RCtS+8kFQbHO7BSvVrUu8un729d5Ftg0Qnf3dFZ0AQG3mxeix Q9swqJMhx7WgY4wXK9z+6ciesx9FV8AZufSMARw+rm2sk9ZBzrTYMl0PKgP4UTmgFAEC kNnKgKzPvWX3wdaAvQa0wJiFh9hFIl37ApAXQTucvrNPigwcapAXOctja0U4G89IG4xQ Ht6ZM1eq2r32k/yqRbZwmMsfUVDXS2cYgBFwZTbIHIEJ8Jw4MXFYRUcHdG+l5limVLZ7 G+0Q== X-Gm-Message-State: AOAM531WnFQg67YHj3hR3nL9L5hHy9U3qgG/kEF/hZd2m9uRSgDB3wVM +KTnf63zORw10N782IofSxs= X-Google-Smtp-Source: ABdhPJwd/hsA9oYd6C/i3E05WDHsS/g/X1F+CQSyKJUNUTpiXXONzBW9uqRKafI8icCemeeqwaRYFg== X-Received: by 2002:a05:6a00:1889:b029:332:13d6:a6eb with SMTP id x9-20020a056a001889b029033213d6a6ebmr269004pfh.25.1627484004862; Wed, 28 Jul 2021 07:53:24 -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, rientjes@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, rppt@kernel.org, kirill.shutemov@linux.intel.com, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, pgonda@google.com, david@redhat.com, keescook@chromium.org, hannes@cmpxchg.org, sfr@canb.auug.org.au, 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, anparri@microsoft.com Subject: [PATCH 07/13] HV/Vmbus: Add SNP support for VMbus channel initiate message Date: Wed, 28 Jul 2021 10:52:22 -0400 Message-Id: <20210728145232.285861-8-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210728145232.285861-1-ltykernel@gmail.com> References: <20210728145232.285861-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1627484037204100001 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). Signed-off-by: Tianyu Lan --- 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..e6a7bae036a8 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_is_isolation_supported()) { + 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_is_isolation_supported()) { + 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 Thu May 2 22:36:05 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=1627484040; cv=none; d=zohomail.com; s=zohoarc; b=BKoAU0JoJTDVx42onH5mu2MAUTvjB7hq5mZBkecufly6AA6WaLprX0aEZEd4SybfasQQKaRCtIjAB258HHw3dHao/vbDpQkyo8sfnok4IZPpu73uvjwvRw3UyOAYOi8uE+xEc2IYG0qyFDHzBL5yFnpjx/O9cxmSE8GNtWzAIus= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1627484040; 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=XbGhpS5yStXGWn3Ym/sECphkTnEdMSaFIpokEER49XU=; b=d3e0ER4dW2Im6xAVgiRNSz3A/HVUJ8JVNRJz85vW/yUfXlYJD1tfGODjQKtuXLfy7Ozgoy8UipF9yxFQbgChrkl0beEnR+IkayGCnzC7HXZBd8vp4iCvyFpuJXfUvU2u5JQqsnv9xGmdfSKtJK+RX97oW3/cs4pz85qr4QJrKg8= 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 1627484040794940.9791511177655; Wed, 28 Jul 2021 07:54:00 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.161606.296670 (Exim 4.92) (envelope-from ) id 1m8kwM-0006Jl-T0; Wed, 28 Jul 2021 14:53:46 +0000 Received: by outflank-mailman (output) from mailman id 161606.296670; Wed, 28 Jul 2021 14:53:46 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kwM-0006JV-Om; Wed, 28 Jul 2021 14:53:46 +0000 Received: by outflank-mailman (input) for mailman id 161606; Wed, 28 Jul 2021 14:53:45 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kwL-0002W0-Pv for xen-devel@lists.xenproject.org; Wed, 28 Jul 2021 14:53:45 +0000 Received: from mail-pj1-x102f.google.com (unknown [2607:f8b0:4864:20::102f]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id c69210b7-75e4-44c9-9cbd-4dfe9f6c821a; Wed, 28 Jul 2021 14:53:28 +0000 (UTC) Received: by mail-pj1-x102f.google.com with SMTP id a4-20020a17090aa504b0290176a0d2b67aso10445796pjq.2 for ; Wed, 28 Jul 2021 07:53:28 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:0:3823:141e:6d51:f0ad]) by smtp.gmail.com with ESMTPSA id n134sm277558pfd.89.2021.07.28.07.53.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 07:53:27 -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: c69210b7-75e4-44c9-9cbd-4dfe9f6c821a 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=XbGhpS5yStXGWn3Ym/sECphkTnEdMSaFIpokEER49XU=; b=UNhLY/LJxIAl3hhMGfLDx8/K71OJUBapw6jrZUfpa//sX59Io8zSSVcUJ8sd8cjZW/ IpifuPgZgSoTpiyyg++LD6rZ4R/rYvPUNT+eGWaoRsuD/lg8L+ciBDrd8OOOBdCYEkhz js2tUkjW2OWBEEto7n/Fd/t6iuG17xUrfbznjAqsbPCx95UcbXwq0PyDZtwyA9zj+6mh VV8Co6/r2xhbZA7i0mVUlNmwG05pZjgpKubMnwnvDw1ZsaqDOX7+1LW6TgZdA+6ZzNZx yAkrAFOkM0dUOcCMbJKdi0x1+AvwbKrf68kT8YMwii/A0/t6vwHy17NTJ0/z7QmcNHam M/fg== 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=XbGhpS5yStXGWn3Ym/sECphkTnEdMSaFIpokEER49XU=; b=tShz20BFu9Jw+GarzQt7iF+UMPHh5lk8IK0fNqrVvQTaVGndTg5s9FDLrJLnMB5pOn RzZ+PDj4T1qKaA/il5CALS89Na/rswXTACpojCy8sTfEO0zpto7ifv/UJ+26UyvG8HmX h88CfT7U0D1MsJ3paMYEe1w0/ReNFjg/Pv03j7BaZ1NsIO8XdtgxSBhHp+x1Jrp8Op6O /9pwRx1XFscl2Sgz9yYOcPh/h2Vh8QcXIBisPANpBkWvczF285CRhHLqzKtyLmMkWhPe 3yZw9eTo96/TKPPE89o7pvUSeOD56pOe/c2nPcPt+ncrNwaaBn5E4dGWSz+jWWUM0brH Sr2Q== X-Gm-Message-State: AOAM532kTCW5GqvlUL6D4YJy+P82w6lWDtNtGbJv0KcMEuTxrDBiSpkc MIuktKJ7LoW2LdLJ798IsOA= X-Google-Smtp-Source: ABdhPJzX5cWZmmYgsBr38KpDUq2ODpqZDr9XzO/VyKUYsGiabpMG2QyirU5nOCTo0Z0p6gSae2NO2w== X-Received: by 2002:a17:90a:d181:: with SMTP id fu1mr10217202pjb.157.1627484007625; Wed, 28 Jul 2021 07:53:27 -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, rientjes@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, rppt@kernel.org, kirill.shutemov@linux.intel.com, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, pgonda@google.com, david@redhat.com, keescook@chromium.org, hannes@cmpxchg.org, sfr@canb.auug.org.au, 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, anparri@microsoft.com Subject: [PATCH 08/13] HV/Vmbus: Initialize VMbus ring buffer for Isolation VM Date: Wed, 28 Jul 2021 10:52:23 -0400 Message-Id: <20210728145232.285861-9-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210728145232.285861-1-ltykernel@gmail.com> References: <20210728145232.285861-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1627484041664100006 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 66c794d92391..a8386998be40 100644 --- a/drivers/hv/Kconfig +++ b/drivers/hv/Kconfig @@ -7,6 +7,7 @@ config HYPERV depends on X86 && ACPI && X86_LOCAL_APIC && HYPERVISOR_GUEST select PARAVIRT select X86_HV_CALLBACK_VECTOR + 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 01048bb07082..7350da9dbe97 100644 --- a/drivers/hv/channel.c +++ b/drivers/hv/channel.c @@ -707,6 +707,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 Thu May 2 22:36:05 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=1627484044; cv=none; d=zohomail.com; s=zohoarc; b=kUbTbtUXxX5PGPmhLefalmSlXSeWJB3HKG4xem8azbrCPBgmEq1n6Vu4dxrGjuRe8LkDxieHNvY/RbJzpqiKjC9dcTY/7c7CU4BpWgXLawETydLSKrVx9ubtGf3ga1gtgX81xSK56K//5q4b/ISINlahlDQER1QF6EsY+iJjUNU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1627484044; 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=fRYEBTeqaJZ2nuhpRxylpNXV0HYL+RYR+rak5A96Ekw=; b=ir5zJ9mTUL33Mu35Cid0H5RzujVVdI5jc6Zom8oduVe6SV0Twn1VL3izj8lZH/09axkC3BcTnq4filUfsRF2spE4F8ZiWaQ4LGnykDgsAI/hlFUFEBRwh7H0KvhBCOA8Ze4usMQBZaAQELGBp0F0wS4E8s/Kej8kLQ91xHRaGVs= 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 1627484044407230.72907634754245; Wed, 28 Jul 2021 07:54:04 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.161610.296683 (Exim 4.92) (envelope-from ) id 1m8kwS-0006uE-8p; Wed, 28 Jul 2021 14:53:52 +0000 Received: by outflank-mailman (output) from mailman id 161610.296683; Wed, 28 Jul 2021 14:53:52 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kwS-0006tr-1N; Wed, 28 Jul 2021 14:53:52 +0000 Received: by outflank-mailman (input) for mailman id 161610; Wed, 28 Jul 2021 14:53:50 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kwQ-0002W0-Q5 for xen-devel@lists.xenproject.org; Wed, 28 Jul 2021 14:53:50 +0000 Received: from mail-pl1-x62c.google.com (unknown [2607:f8b0:4864:20::62c]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id caea6084-847c-49f8-a00a-2b631b3bfcbd; Wed, 28 Jul 2021 14:53:30 +0000 (UTC) Received: by mail-pl1-x62c.google.com with SMTP id n10so3010650plf.4 for ; Wed, 28 Jul 2021 07:53:30 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:0:3823:141e:6d51:f0ad]) by smtp.gmail.com with ESMTPSA id n134sm277558pfd.89.2021.07.28.07.53.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 07:53:29 -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: caea6084-847c-49f8-a00a-2b631b3bfcbd 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=fRYEBTeqaJZ2nuhpRxylpNXV0HYL+RYR+rak5A96Ekw=; b=sig9nFz8eF+R0264OvGZWIOLFWHbYjYEwR2pu5Gx18nTH1XR1cSX7qthexUthkJGnF pJ3gnLUuVZV82sVUwcehqWiFsrDdsp1tZqg/jE/39e470wf89bUSF5Q65P+BWFm5oA6l tdiu0+NkRbrdrY5sf+SlZYMVQQRnay/soDyBm9yR+3Sk7hPDtBJcXtSKp6bbcAlin5vP TxrwmdY7JJpG3nMN0Kpik8j7JBNr1h1zSYFRfmYey7tr/PqG3AfyBn5BZzutMLEWs9dk Q8BFOoHDdjXEgX/RmQsfZ/nngsOe/X2TpZTA23wyhqE+8CwnuHHe28EpNrXDO0BWSkNE 89qw== 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=fRYEBTeqaJZ2nuhpRxylpNXV0HYL+RYR+rak5A96Ekw=; b=iSff11AjMloSoGUAY+Q6TcmGDBSoYIyqYUEOQbcLu3/i7jIr+c2fMsFPUVBrKlFJC/ yTGh23QuqfvLgxPxiLxPvxw3cv/VMSYTa/mTkehugXoNOXY7euepjvcA9kkI9n/e8mcS B2UOUvs1XBqjHJtklbM2wXAzdtxP/rhyrQtfSvDJfTcpPpzDDwnPE3h3Gb59OTWuED0y phIlOkf3fdc9V2wIsaQ01wbhqPZH2K+ZWr9QyjAji9uHIZklpIGU56EHwClnajr6y/Hx kg8vCNj9hzUJJZLJfq1pyzXCLlJHxM7bENOuX4Zt4apzuN9QV255f0LNz9o8qYEBWiP2 jc9A== X-Gm-Message-State: AOAM532MCIvE5qk1UQnibecBpvez9c9W+iL/DZgTV2RUO7Wsh/i9gU5C u2KiPMsNiPZBYkFd1Mido8c= X-Google-Smtp-Source: ABdhPJw5OcxnrbSJBNvT8e2xeKUOHmvmo/7aRU5B5UYJ8E3STm6ku1q9/sANkCbS99R2obrk00cckA== X-Received: by 2002:a17:90b:385:: with SMTP id ga5mr6922305pjb.183.1627484010063; Wed, 28 Jul 2021 07:53:30 -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, rientjes@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, rppt@kernel.org, kirill.shutemov@linux.intel.com, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, pgonda@google.com, david@redhat.com, keescook@chromium.org, hannes@cmpxchg.org, sfr@canb.auug.org.au, 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, anparri@microsoft.com Subject: [PATCH 09/13] DMA: Add dma_map_decrypted/dma_unmap_encrypted() function Date: Wed, 28 Jul 2021 10:52:24 -0400 Message-Id: <20210728145232.285861-10-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210728145232.285861-1-ltykernel@gmail.com> References: <20210728145232.285861-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1627484046054100001 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. Signed-off-by: Tianyu Lan --- include/linux/dma-map-ops.h | 9 +++++++++ kernel/dma/mapping.c | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index 0d53a96a3d64..01d60a024e45 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -71,6 +71,11 @@ struct dma_map_ops { unsigned long (*get_merge_boundary)(struct device *dev); }; =20 +struct dma_memory_decrypted_ops { + void *(*map)(void *addr, unsigned long size); + void (*unmap)(void *addr); +}; + #ifdef CONFIG_DMA_OPS #include =20 @@ -374,6 +379,10 @@ static inline void debug_dma_dump_mappings(struct devi= ce *dev) } #endif /* CONFIG_DMA_API_DEBUG */ =20 +void *dma_map_decrypted(void *addr, unsigned long size); +int dma_unmap_decrypted(void *addr, unsigned long size); + extern const struct dma_map_ops dma_dummy_ops; +extern struct dma_memory_decrypted_ops dma_memory_generic_decrypted_ops; =20 #endif /* _LINUX_DMA_MAP_OPS_H */ diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c index 2b06a809d0b9..6fb150dc1750 100644 --- a/kernel/dma/mapping.c +++ b/kernel/dma/mapping.c @@ -13,11 +13,13 @@ #include #include #include +#include #include "debug.h" #include "direct.h" =20 bool dma_default_coherent; =20 +struct dma_memory_decrypted_ops dma_memory_generic_decrypted_ops; /* * Managed DMA API */ @@ -736,3 +738,23 @@ unsigned long dma_get_merge_boundary(struct device *de= v) return ops->get_merge_boundary(dev); } EXPORT_SYMBOL_GPL(dma_get_merge_boundary); + +void *dma_map_decrypted(void *addr, unsigned long size) +{ + if (set_memory_decrypted((unsigned long)addr, + size / PAGE_SIZE)) + return NULL; + + if (dma_memory_generic_decrypted_ops.map) + return dma_memory_generic_decrypted_ops.map(addr, size); + else + return addr; +} + +int dma_unmap_encrypted(void *addr, unsigned long size) +{ + if (dma_memory_generic_decrypted_ops.unmap) + dma_memory_generic_decrypted_ops.unmap(addr); + + return set_memory_encrypted((unsigned long)addr, size / PAGE_SIZE); +} --=20 2.25.1 From nobody Thu May 2 22:36:05 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=1627484170; cv=none; d=zohomail.com; s=zohoarc; b=kGSY+9RORd6Rtpi2yOoTdgLuOkQLXrvnYWXomP4kJM6pXOKMAfU5xR/0dBbjtcciP00DfHm9yEuEpbZSkdZbaj5Zimgj4mZ+IZattyyl1MRO+akYuSNkwbtqchS4V73by3XhdzZ8kAUsyimwyaFlVoowSehEET5mrGEXgG0i7S0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1627484170; 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=ot9NJwgdtSwny3Hz0sW1NmpmwSF9uMUFojPkk0oFEg8=; b=li1wv3pc83gRM6pSEcAMjL7OOmYVyy9EkFuKWDJiUC4e6QpwyZ5Oa8pDKrJ4ORktu4TCphyGDdqit65rm/xvaDj+KTbMJ+MtbtA46YTl/zUZ34W+sNJSVU68t481d14OnxbDU1/UxLrvkBoK6ExJwmBIeSWXHUpnpQilv2qmZOE= 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 1627484170390491.59185551372616; Wed, 28 Jul 2021 07:56:10 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.161625.296694 (Exim 4.92) (envelope-from ) id 1m8kyM-0000HK-SV; Wed, 28 Jul 2021 14:55:50 +0000 Received: by outflank-mailman (output) from mailman id 161625.296694; Wed, 28 Jul 2021 14:55:50 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kyM-0000HD-Oo; Wed, 28 Jul 2021 14:55:50 +0000 Received: by outflank-mailman (input) for mailman id 161625; Wed, 28 Jul 2021 14:55:50 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kwa-0002W0-QR for xen-devel@lists.xenproject.org; Wed, 28 Jul 2021 14:54:00 +0000 Received: from mail-pj1-x102e.google.com (unknown [2607:f8b0:4864:20::102e]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 73f76943-e8a9-476b-b47f-60c6712fe3d1; Wed, 28 Jul 2021 14:53:32 +0000 (UTC) Received: by mail-pj1-x102e.google.com with SMTP id mz5-20020a17090b3785b0290176ecf64922so10411258pjb.3 for ; Wed, 28 Jul 2021 07:53:32 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:0:3823:141e:6d51:f0ad]) by smtp.gmail.com with ESMTPSA id n134sm277558pfd.89.2021.07.28.07.53.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 07:53:32 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 73f76943-e8a9-476b-b47f-60c6712fe3d1 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=ot9NJwgdtSwny3Hz0sW1NmpmwSF9uMUFojPkk0oFEg8=; b=S9fynuTeK9vSXvNUDeY4KnRGKPztyWj8Fnz9B9pB5BahJ6cJS+RVcWxVYle9dJnAIB DXvmJ5t+++ZA0+T/R07cFZjYEvWfOBk5aAnD8gwRqttsKg4OchtPTc7bNT+bkFxS0q+T TS/EMh8f7+QSBU3kGVcPuPs8WY7W8CHJzLzI6CjPe3REMSyY+WNgH2eXqZDMnV2ZtFhz G26GNjCWgJtPCYIYclclGs7MF/0fcsoDjy+ziedykVSkxHQCeVHQSN/W73Qf/d3uXzjp f3+qF0i9dOE+cA2voQSGgplfosvRU8qjFeC6hKdER1BDhuJIhBrZI5zNDfVR9HgknOBd oryw== 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=ot9NJwgdtSwny3Hz0sW1NmpmwSF9uMUFojPkk0oFEg8=; b=NDA+O99HxTSE/jyn+GSVuN+sEwYr2p+PDRbEplKl+43kjhraqfQScJqEV3tz7A41kL 7GWxAJFzDUfxIjHDd6iDo9mbn7bg0wGfinjORXF8dgulIdfzRL0/EFt1v42rDT2zhwq+ jGpzO77LsRWLjcVsweQt6KkMHKaWN4hmOVW3OkdDlxdYvm42N1mjQB1AoeQ0ZzT1UTd/ aRSWNGQ7B2luHrw8XZsg+t5Bv0GTrwtMeS2ABCoOU88aT+wFOCbXqW+cD2Af79WyE0Jv HAX63S8W46Q8bXOcAvYTmsCLtEvZEMni6patknzy2kI+hE48oVQLvfWxI81BEkFmTgXk nsDQ== X-Gm-Message-State: AOAM530/JP4vJQ2cLNzk+WMR7158k8o4b/4Iv9pNqfuyGymSkZ4SMC3K SjwhmPzZYO0gFeJSYx4WoyE= X-Google-Smtp-Source: ABdhPJwWvL5ltGyV4xAnV2PomvPFPoswsA8DVwzJ1kj0bPYDH6cusQ3v3SJ1R0nnIFdc7ySbf7Z6SQ== X-Received: by 2002:a17:90a:19db:: with SMTP id 27mr10213457pjj.216.1627484012282; Wed, 28 Jul 2021 07:53:32 -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, rientjes@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, rppt@kernel.org, kirill.shutemov@linux.intel.com, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, pgonda@google.com, david@redhat.com, keescook@chromium.org, hannes@cmpxchg.org, sfr@canb.auug.org.au, 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, anparri@microsoft.com Subject: [PATCH 10/13] x86/Swiotlb: Add Swiotlb bounce buffer remap function for HV IVM Date: Wed, 28 Jul 2021 10:52:25 -0400 Message-Id: <20210728145232.285861-11-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210728145232.285861-1-ltykernel@gmail.com> References: <20210728145232.285861-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1627484171878100003 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 --- include/linux/swiotlb.h | 4 ++++ kernel/dma/swiotlb.c | 11 ++++++++--- 2 files changed, 12 insertions(+), 3 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..6866e5784b53 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -194,8 +194,13 @@ 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; + } + + memset(mem->vaddr, 0, bytes); } =20 int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verb= ose) @@ -360,7 +365,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 Thu May 2 22:36:05 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=1627484184; cv=none; d=zohomail.com; s=zohoarc; b=Fd9VdCBXHRhPQObRJfiQEVfL85P9ZDFeevsrCWcgxkhbu22oyKLTVBiFc1nwixvmuuHuUzFDmejhpCoi+z79wcwkq1ZYEqtpQgPnnCkuwIP4VnpsxuJ319G5jDWi63hLD+FW9RXRyyCFeTADhIBmJ49cYet81rl0shWtQfUlWlA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1627484184; 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=qzKwcvVxhca+ciJXqg2U8rozm2nSDx+vDxmGytYMFV8=; b=fAqnIT6/ulj0vBAfU1V+lm0Pdfp1cYl15WRkPcDBsLRy9leXeXN7+jh0ZcY3zRlOzVHWg2oCh9AyHMukiAt+eiuRfP+jNfw/yNdJYvob7JFYRPEkqYSaNaa/4yRzefyBg1Ol5uv9aF7S0dfUhC3D2quQ35p0Bs35v1qEOuLCO9o= 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 1627484184819277.3708008277696; Wed, 28 Jul 2021 07:56:24 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.161641.296705 (Exim 4.92) (envelope-from ) id 1m8kyi-0000uT-3r; Wed, 28 Jul 2021 14:56:12 +0000 Received: by outflank-mailman (output) from mailman id 161641.296705; Wed, 28 Jul 2021 14:56:12 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kyi-0000uK-0u; Wed, 28 Jul 2021 14:56:12 +0000 Received: by outflank-mailman (input) for mailman id 161641; Wed, 28 Jul 2021 14:56:11 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kwf-0002W0-QT for xen-devel@lists.xenproject.org; Wed, 28 Jul 2021 14:54:05 +0000 Received: from mail-pj1-x1030.google.com (unknown [2607:f8b0:4864:20::1030]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 9b789a6f-3217-48bf-9c36-bcf2610749da; Wed, 28 Jul 2021 14:53:35 +0000 (UTC) Received: by mail-pj1-x1030.google.com with SMTP id ds11-20020a17090b08cbb0290172f971883bso10456227pjb.1 for ; Wed, 28 Jul 2021 07:53:35 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:0:3823:141e:6d51:f0ad]) by smtp.gmail.com with ESMTPSA id n134sm277558pfd.89.2021.07.28.07.53.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 07:53: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: 9b789a6f-3217-48bf-9c36-bcf2610749da 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=qzKwcvVxhca+ciJXqg2U8rozm2nSDx+vDxmGytYMFV8=; b=GFuVrx3l5pZLm7DeD09JNxJc3JkwdPBsDYP6JO873zFO4XMaIgVpNG/+WnOETKVtlY m//je+3k3RFHUddjBm0pO6wsDR0v8tDSjlTNdabGQhBaYE/QmhtzACKofhAuxSjK5RZQ a9RVmPr8+9jibtDYFMsOe0DZ0UvDQ/qIBlFczmN179r7qv1WKZ68P6JibJNeXK2MJfMX Sclrix9Bced5k2g6C76vFA2OCMBX1PEDh5DZT3RicuRM6O9ReK9XFn+n9XnpC6vFDCM9 GWJYFJqK9kpg2ISP1Z5IOjZTYuASDNkUkIq5IL2nXb7/q2S0EKYvEh1kjrR6EX6Z06I7 /V4w== 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=qzKwcvVxhca+ciJXqg2U8rozm2nSDx+vDxmGytYMFV8=; b=OMSdsdjYGm65LN3tP/vPGU6JwCgbk0/xxKXWdRmYd3QgPrkjmDtY6cfoQrUZLK+Lkq KkQZfjliFKy7nGYyuCAgA1HohRuvnixj0HDHh1vr0GA1iKSM1IAFctffCNNn4vifnRsM nTHKl9uTbMy1BXl5nhh5sBBN7/cX9qDK6JRjFzIIqu0m5R9yJaO9Rq1UIp0zRNniU5tZ RMYDQ40SU+ys6FpHPb00y4dAhLuAnscZxUDFfyaAZrqxPzuFeg1NDuNOxcn/sS5BdPwY rrwpN4C3p2aFI+4dGWDqtJr4TDdDi/tdoAXLlAyKR8THUc2GINEShQHcRNcr4rstu6rT Z55Q== X-Gm-Message-State: AOAM530OlZ7xAMYeMcvKgLIoSk06u/461IBk+JQuHBZIdJTz4YabrmwD ckfT9xIeca3Coo+fJiqKwag= X-Google-Smtp-Source: ABdhPJxp/Z+0x7njF0HYKpDRe1xQUzsvfMQucpsS0v4OrFXzODEQULJTIW6YK80P2I/aGYtZMapg7Q== X-Received: by 2002:a17:902:b40b:b029:12b:997f:5677 with SMTP id x11-20020a170902b40bb029012b997f5677mr143577plr.11.1627484014488; Wed, 28 Jul 2021 07:53: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, rientjes@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, rppt@kernel.org, kirill.shutemov@linux.intel.com, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, pgonda@google.com, david@redhat.com, keescook@chromium.org, hannes@cmpxchg.org, sfr@canb.auug.org.au, 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, anparri@microsoft.com Subject: [PATCH 11/13] HV/IOMMU: Enable swiotlb bounce buffer for Isolation VM Date: Wed, 28 Jul 2021 10:52:26 -0400 Message-Id: <20210728145232.285861-12-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210728145232.285861-1-ltykernel@gmail.com> References: <20210728145232.285861-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1627484187319100001 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 13bab7f07085..9fbb5cbf3321 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -266,3 +266,31 @@ int hv_set_mem_enc(unsigned long addr, int numpages, b= ool enc) enc ? VMBUS_PAGE_NOT_VISIBLE : VMBUS_PAGE_VISIBLE_READ_WRITE); } + +/* + * 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 (unsigned long)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 8bf26e6e7055..b815ec0bc36d 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -249,6 +249,8 @@ int hv_map_ioapic_interrupt(int ioapic_id, bool level, = int vcpu, int vector, int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *en= try); int hv_mark_gpa_visibility(u16 count, const u64 pfn[], u32 visibility); int hv_set_mem_enc(unsigned long addr, int numpages, bool enc); +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..089617085a69 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,8 @@ static cpumask_t ioapic_max_cpumask =3D { CPU_BITS_NONE }; static struct irq_domain *ioapic_ir_domain; =20 +static unsigned long hyperv_io_tlb_start, hyperv_io_tlb_size; + static int hyperv_ir_set_affinity(struct irq_data *data, const struct cpumask *mask, bool force) { @@ -337,4 +347,59 @@ 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 + (unsigned long)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) +{ + int ret; + + /* + * 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. + */ + swiotlb_late_init_with_tbl(hyperv_io_tlb_start, + hyperv_io_tlb_size >> IO_TLB_SHIFT); +} + +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 06eccaba10c5..babbe19f57e2 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -1759,6 +1759,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 Thu May 2 22:36:05 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=1627484203; cv=none; d=zohomail.com; s=zohoarc; b=WNiOP0ewmvIHE0Q/g5hXjZd8R2c94b6UZfu+0H+5T9V8lR/11Mgi67O9sKqx3OGm/JwNEnxCRgjZH5x1kGQ7mugieQP6lLCil9ZtYvJEf98o99Kmu9cNEHdP6wjZtmmJZeMXTrjGdFy27NaFB/HYLqcc5ucHPJhrG7J9TsmResA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1627484203; 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=kwyPmDPn6j9wa2BHkeUlG6OG5xFsU4BPPYe25h3X/Ks=; b=MscIl+9+rpZlSq0PgR5zPDEyQimxiT8qczv5i0kyQXLxp7U5xQV/gtFGwrLCSukfJKr48tgRngUvA9TxjEPd+Jlzc5LHDBtSPD8QJniTgq+q5wYtjbtOIwA8AF77u6XNt7RpjgXZrqz0uXDP40nbUZF+OIO3WhsLon4AQVBJloY= 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 1627484203500777.2053533756945; Wed, 28 Jul 2021 07:56:43 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.161654.296718 (Exim 4.92) (envelope-from ) id 1m8kyw-0001P3-DJ; Wed, 28 Jul 2021 14:56:26 +0000 Received: by outflank-mailman (output) from mailman id 161654.296718; Wed, 28 Jul 2021 14:56:26 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kyw-0001Ow-94; Wed, 28 Jul 2021 14:56:26 +0000 Received: by outflank-mailman (input) for mailman id 161654; Wed, 28 Jul 2021 14:56:25 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kwp-0002W0-Qk for xen-devel@lists.xenproject.org; Wed, 28 Jul 2021 14:54:15 +0000 Received: from mail-pj1-x1032.google.com (unknown [2607:f8b0:4864:20::1032]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id caa2cec0-6d1c-4257-8b95-77a53ae283a5; Wed, 28 Jul 2021 14:53:37 +0000 (UTC) Received: by mail-pj1-x1032.google.com with SMTP id o44-20020a17090a0a2fb0290176ca3e5a2fso4504949pjo.1 for ; Wed, 28 Jul 2021 07:53:37 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:0:3823:141e:6d51:f0ad]) by smtp.gmail.com with ESMTPSA id n134sm277558pfd.89.2021.07.28.07.53.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 07:53: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: caa2cec0-6d1c-4257-8b95-77a53ae283a5 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=kwyPmDPn6j9wa2BHkeUlG6OG5xFsU4BPPYe25h3X/Ks=; b=YJzYki4Pa1YAyR1deIjQQXfJaGx0bnUdSSavjU3nGmSX4p4x2VXnxa90B9LPS5IphL tMJFUOFgFwatYdqGlodDe7oZbe67+luRY7Y4oY0eXNoTB2l2ShiwMpHw9p2cURFPkW2/ Bhxsi22uhDQEg+1qAYrfetl0V7tIvM/ckZMogUK0VByDoPgFf4n374TDtk4+sF3AQjrz M3swUrQaSvaSoMMWLYV/VSD48zTrMHBMpKLmxYVRu/A+FhukXU66ZAw6LqKr1hd2NzKo FNClXeJskXZAwNIfd6embv/pzZfxyP1MZE2aexGG3CAyra7JIJ1/OVLTdzNvjVX/SNMl eqCQ== 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=kwyPmDPn6j9wa2BHkeUlG6OG5xFsU4BPPYe25h3X/Ks=; b=TcPYzVEsGDyyrXPqOUKH0BAHeFeOGU/QOKGrVBE/v0kjy1FuPAt2LcbAxfAdd2o+sA m0oEIo7PCed2qHxhOc2piRt4SENp+rXjT8J/msjAoZEt0vObxtX+vwOhe4OJWVxhuAqh lsdceVBq6hJBirBA2GBKfNafIjqZZJhxuRy0hG8DICYTb0fL2et2D0pXfrCRO+XtcaXM ghdn1rhNpNGjNdYUmeyU6q+3Vv8njlJjOfVTR7gkLho5H/lwGzFCLmZLqn2AcEsbAIjY fwi0vskUlEXe8m96F9IPerUgNuD9lh99Mm4+3Oc8sJ6ORuowrtvQGuQNQAoKUUFquBMm ioyA== X-Gm-Message-State: AOAM530jOgWEBRdB/umyAM1ugaQGK6DZ7R7fm01UIkMsPr6QYcMpCE/7 kjCNI7rZcvY3p6QJWB9UWtg= X-Google-Smtp-Source: ABdhPJzxYrc/QuDCZ3aV+qz7w6yngSUaDDxoVfiRp30YagCAQELtokNGMt+R0oa149icZFp6i/asUw== X-Received: by 2002:a62:1bc7:0:b029:328:cbf5:b6b0 with SMTP id b190-20020a621bc70000b0290328cbf5b6b0mr55647pfb.81.1627484016811; Wed, 28 Jul 2021 07:53: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, rientjes@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, rppt@kernel.org, kirill.shutemov@linux.intel.com, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, pgonda@google.com, david@redhat.com, keescook@chromium.org, hannes@cmpxchg.org, sfr@canb.auug.org.au, 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, anparri@microsoft.com Subject: [PATCH 12/13] HV/Netvsc: Add Isolation VM support for netvsc driver Date: Wed, 28 Jul 2021 10:52:27 -0400 Message-Id: <20210728145232.285861-13-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210728145232.285861-1-ltykernel@gmail.com> References: <20210728145232.285861-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1627484205718100001 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 babbe19f57e2..90abff664495 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -1616,6 +1616,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 Thu May 2 22:36:05 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=1627484211; cv=none; d=zohomail.com; s=zohoarc; b=IbHoVlPhQ24H1Jfmw/U1RiPMiV2YbBgEiNZLOLs3eMeUFCGvJdj81sVUk365VtVjxUKSpzM6hy1nVv7iLBl0aUgUswIteBXAfrW24m4EaAhAzPrfyesWKZuLO8TNAbMr9x2DbdB5lj743I36BQYmJ9zxFCRkROeifHeZrIA6Xjw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1627484211; 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=jouItrXhd3X24F69z5i1HO3yS4g1n/EdMpbIQMGbOStBUWiP3BNT3xV4Tuf9BSZGNHtPx9qV2V9Kaqzg1bQbo0EJpCYp9A5XuDU+XCdrcLvaNx2WeV46eSa627Vkn5r/Szf0eDy1SKHD2J1puzX+a8Z+Dk/Om9iw0PhOFe1ZkoM= 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 1627484211221506.32024149343454; Wed, 28 Jul 2021 07:56:51 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.161667.296730 (Exim 4.92) (envelope-from ) id 1m8kz7-0001xX-Of; Wed, 28 Jul 2021 14:56:37 +0000 Received: by outflank-mailman (output) from mailman id 161667.296730; Wed, 28 Jul 2021 14:56:37 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kz7-0001xO-Lf; Wed, 28 Jul 2021 14:56:37 +0000 Received: by outflank-mailman (input) for mailman id 161667; Wed, 28 Jul 2021 14:56:36 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kwz-0002W0-R0 for xen-devel@lists.xenproject.org; Wed, 28 Jul 2021 14:54:25 +0000 Received: from mail-pj1-x102c.google.com (unknown [2607:f8b0:4864:20::102c]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 7b305db8-40a1-413e-a5bd-8329e09dba1c; Wed, 28 Jul 2021 14:53:39 +0000 (UTC) Received: by mail-pj1-x102c.google.com with SMTP id m1so5543257pjv.2 for ; Wed, 28 Jul 2021 07:53:39 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:0:3823:141e:6d51:f0ad]) by smtp.gmail.com with ESMTPSA id n134sm277558pfd.89.2021.07.28.07.53.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 07:53:38 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 7b305db8-40a1-413e-a5bd-8329e09dba1c 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=SU5+MP1dlZCdXiG26CqRqBNmqpOnXlHHd6+fcRsxbvw2EKuyqOU5Mopp7bVlS/zejO csfVLQRSDXU/7dyozcqNTbp4P2dmnl8BvfUDZ1Gqg7dgoQW5/yF5uiTbfYAy+VmENB1e ZbRjIv/17kq9qz1Iwt0YvmWCsP015hBjqyx7Z42jWLt2YBzb/erhrTjXS+igHusqWvTV bsLn/A16PIaeWfmCBYPCwhZk+c/L4TU0ebGyf7PLH2QjuceGtPagZYnSsDqBPkotX/hN i7oMHXglQkgWHkJisLKxQxmwRHNVomuhibIwUMR5k1D0bq2YE4VhqLodvPvuOU9g6oxL pZ+Q== 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=E8tAW6GAh0vAelU4zSQZXesti9BLMuRC8egT1E2n3UWTchRS0NW7vU6/HcaNL7YQii Bi/quhqpy97544qYsVXxQlwZU/PvLJ1+D3BmOsXzzBDp3nhRsBuvwMUh1z4YdWqqQ2mf oqxRu+N2ce3PF2XAMHEikIBlZQnGlN1RVJvQ0gCBWxfrOxKMJzUK4+RmnRfIp8LF+hiA IqPM7xf+Ompl8SyVHRtalyIlcYyInIi6Esko2HBjs0/085o3DW789595dPm1rBmPp8aP jAs/44Ltl9f/naU7ld76ffjZ25fqJwJLiaAirGeEggjo9BDiMlC1NXONPZEv06/XjEks 6xig== X-Gm-Message-State: AOAM531Vou7tqa9QT9K+ofNMLLPzCbdt0cvzIym5FGSdalgFGr3hDUMl 7Fd7nj9yxSIXyh0619GefeY= X-Google-Smtp-Source: ABdhPJzheaAoRTrkFU3jE3YHYpkFIR7nUHQbhRWDG+7XrGIy9dCpNUWuhIUXvdG5v42plPOPnYbJQA== X-Received: by 2002:a63:84:: with SMTP id 126mr169114pga.221.1627484019244; Wed, 28 Jul 2021 07:53: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, rientjes@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, rppt@kernel.org, kirill.shutemov@linux.intel.com, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, pgonda@google.com, david@redhat.com, keescook@chromium.org, hannes@cmpxchg.org, sfr@canb.auug.org.au, 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, anparri@microsoft.com Subject: [PATCH 13/13] HV/Storvsc: Add Isolation VM support for storvsc driver Date: Wed, 28 Jul 2021 10:52:28 -0400 Message-Id: <20210728145232.285861-14-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210728145232.285861-1-ltykernel@gmail.com> References: <20210728145232.285861-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1627484212814100001 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