From nobody Fri May 3 09:54:22 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=1625672818; cv=none; d=zohomail.com; s=zohoarc; b=f3tNHsLk8fhe9fzihy9us8j9Ru8xGUhu40IBpDp3rdxgv3nsEz0pS5TOEvNRrK4unha9KaieZkq5X7GLcD0Fdi5UxyZ1X/ET2C4nqrPxG+50ANxXh+44ocj1ulsMaS5yD9SOpfcsL1z0szhM4T3wbWfdRIw4vM+UB4f308Cd1OE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1625672818; 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=R5OLGAMwYXeSrSgg628gKxmY1CTczZlgXFfhQZlHpg0=; b=R6gZyHOPKPKSbypsckIj48olEAPGFeR0YsQ6Ecdvv6oC6z9YhVb/rsuNX80rxYW2u7tMdVE5EfJq/IbAL/zt3Popy2BDS8L8SIcSoBx013LoH2nVgHG9ZRf7g6Uu51rP4LOQw/z5U+3rRXieoyhWsKJ5SnOrDC/OuKTzyaZLeLE= 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 1625672818390891.0753241252564; Wed, 7 Jul 2021 08:46:58 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.152486.281718 (Exim 4.92) (envelope-from ) id 1m19l7-000356-AL; Wed, 07 Jul 2021 15:46:45 +0000 Received: by outflank-mailman (output) from mailman id 152486.281718; Wed, 07 Jul 2021 15:46:45 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19l7-00034v-5t; Wed, 07 Jul 2021 15:46:45 +0000 Received: by outflank-mailman (input) for mailman id 152486; Wed, 07 Jul 2021 15:46:44 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19l6-0002ed-3w for xen-devel@lists.xenproject.org; Wed, 07 Jul 2021 15:46:44 +0000 Received: from mail-pf1-x42e.google.com (unknown [2607:f8b0:4864:20::42e]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id f952742f-823b-431e-b5fa-5d6e02d6d4af; Wed, 07 Jul 2021 15:46:42 +0000 (UTC) Received: by mail-pf1-x42e.google.com with SMTP id f20so2555667pfa.1 for ; Wed, 07 Jul 2021 08:46:41 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:38:6b47:cf3e:bbf2:d229]) by smtp.gmail.com with ESMTPSA id q18sm23093560pgj.8.2021.07.07.08.46.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 08:46:41 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: f952742f-823b-431e-b5fa-5d6e02d6d4af 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=R5OLGAMwYXeSrSgg628gKxmY1CTczZlgXFfhQZlHpg0=; b=tgnrux6t6SkoTnavPrn5fh2qP8KaJe6YyAcLF8Zt4MPOaOlMQnjeAPQtLYHcXHGF0l RB/sKa5anmBAiy1Yvr2Y+jbSOIsZKj02IHcz79CtMxEELmbdvgejBFoU8P/inGk7Ski+ UB5Fbm6qJvsCU7DXOgoODC5/LhmKJ2RHM0qWVcBm5px6hqJ6rLFhYCJMaghMJN7A4+Ae pn3qiH84GvkyWF7QU5yE4BQr7v0t0dCF2LiTlW/fuYW0dRdIKXqx8OiOcW0SB0gvFoEe 6fqsHE83UrjkzL8JL9iT4cPKy7cfHYkLm0LgcRdpfqv0E36dKdrKn5jfgHF+5+w1Jqdf JEIA== 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=R5OLGAMwYXeSrSgg628gKxmY1CTczZlgXFfhQZlHpg0=; b=qM84QtTHgvqVQiVO5CFo0ahEow3uDPtUtElJ9uiDEeqTyg0CjZ2bzybXuvxk9STrrM ObYFSrZVh/9UaZQeRsZVE1k9kBqlDKSKNBqLKiEqQ2LgzpyUkHLg+ZnzU5sH97AX2RcQ eymrwVVMa0gW9DzKYWGXK69wD4a46shHcjRH4tbHx8v3mhNnAjTEFF4rx0c4qlGR9qUp X5IqudVYWYL/Za02zTDpfI4zlMf7G7Aw8TcoCQoN+g14mk6x0lJn4fOFVcEyHSlwzAVD CHCPLQcl8r/hvSWBgGs+NDdNox2Y9ro2fuMHXnLGYLNKs/z13rgPFwerSu8YNw4eJXok S1ew== X-Gm-Message-State: AOAM533QMFIDo0g2b2uxTNz9vGB2Auny3BECo5Bf/2ohTE1d9bQCBrnq 1HVrMcpFJFM24YPkh7cHhIg= X-Google-Smtp-Source: ABdhPJzuj8kyKMtNb+EskaT+9TtDKmzwnLiT5Tz7koSTfL0Lib9zH4bi1+4ZTW8ZC1YP+6PSa+4BSA== X-Received: by 2002:a62:fb13:0:b029:309:8d89:46b2 with SMTP id x19-20020a62fb130000b02903098d8946b2mr25766588pfm.67.1625672801313; Wed, 07 Jul 2021 08:46:41 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, will@kernel.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, kirill.shutemov@linux.intel.com, akpm@linux-foundation.org, rppt@kernel.org, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, ardb@kernel.org, robh@kernel.org, nramas@linux.microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, david@redhat.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, keescook@chromium.org, rientjes@google.com, hannes@cmpxchg.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, brijesh.singh@amd.com, anparri@microsoft.com Subject: [Resend RFC PATCH V4 01/13] x86/HV: Initialize GHCB page in Isolation VM Date: Wed, 7 Jul 2021 11:46:15 -0400 Message-Id: <20210707154629.3977369-2-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210707154629.3977369-1-ltykernel@gmail.com> References: <20210707154629.3977369-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1625672820569100003 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 | 64 ++++++++++++++++++++++++++++++--- arch/x86/include/asm/mshyperv.h | 2 ++ include/asm-generic/mshyperv.h | 2 ++ 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c index b756b2866deb..e058f72eeee5 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 @@ -54,6 +55,26 @@ EXPORT_SYMBOL_GPL(hyperv_pcpu_output_arg); u32 hv_max_vp_index; EXPORT_SYMBOL_GPL(hv_max_vp_index); =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) { u64 msr_vp_index; @@ -106,6 +127,8 @@ static int hv_cpu_init(unsigned int cpu) wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val); } =20 + hyperv_init_ghcb(); + return 0; } =20 @@ -201,6 +224,7 @@ static int hv_cpu_die(unsigned int cpu) unsigned long flags; void **input_arg; void *pg; + void **ghcb_va =3D NULL; =20 local_irq_save(flags); input_arg =3D (void **)this_cpu_ptr(hyperv_pcpu_input_arg); @@ -214,6 +238,13 @@ static int hv_cpu_die(unsigned int cpu) *output_arg =3D NULL; } =20 + 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 free_pages((unsigned long)pg, hv_root_partition ? 1 : 0); @@ -410,9 +441,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); @@ -473,7 +517,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); @@ -502,6 +547,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); @@ -586,6 +634,14 @@ bool hv_is_isolation_supported(void) } EXPORT_SYMBOL_GPL(hv_is_isolation_supported); =20 +DEFINE_STATIC_KEY_FALSE(isolation_type_snp); + +bool hv_isolation_type_snp(void) +{ + return static_branch_unlikely(&isolation_type_snp); +} +EXPORT_SYMBOL_GPL(hv_isolation_type_snp); + /* Bit mask of the extended capability to query: see HV_EXT_CAPABILITY_xxx= */ bool hv_query_ext_cap(u64 cap_query) { diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyper= v.h index 67ff0d637e55..aeacca7c4da8 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 9a000ba2bb75..3ae56a29594f 100644 --- a/include/asm-generic/mshyperv.h +++ b/include/asm-generic/mshyperv.h @@ -35,6 +35,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 @@ -224,6 +225,7 @@ bool hv_is_hyperv_initialized(void); bool hv_is_hibernation_supported(void); enum hv_isolation_type hv_get_isolation_type(void); bool hv_is_isolation_supported(void); +bool hv_isolation_type_snp(void); void hyperv_cleanup(void); bool hv_query_ext_cap(u64 cap_query); #else /* CONFIG_HYPERV */ --=20 2.25.1 From nobody Fri May 3 09:54:22 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=1625672823; cv=none; d=zohomail.com; s=zohoarc; b=EQhMuSn0B+b8iQ+VuHMG2839bDNQ1vSkAgEW/9V5dhWUSYEz2yiOIawxKYGVrsw0ZznQx7TJCgBfljCvoJ/MUG3N59OpXsyXXX7HJCFqAv/Bz2iQwwOCvfwbZEMuFUZGQTe1hE7byOSrGvGata0bRBYHv5G450O/D4XLCIViy1w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1625672823; 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=0MmjMjO7XvUbpBRdqqPG7tGmikUa5z2pWikCp/JzIEY=; b=b7vMn3kP/Jrg5BpDe1UQlWtBKPIuV4cJMSX+9QK4t3pTQT83/D0xLZ6/I9WmY3zOwwzq287QFr1IKUgUzaE3CezJLEiRvWilrZczvWSL5EDq4CZY+4lqeICtBVjyFP6u4M1bb8IjXQ4x82cB1BgLOgi9z0vKwkFSbw+6orzp/1Y= 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 1625672823047535.4639255122374; Wed, 7 Jul 2021 08:47:03 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.152487.281729 (Exim 4.92) (envelope-from ) id 1m19lC-0003RY-IE; Wed, 07 Jul 2021 15:46:50 +0000 Received: by outflank-mailman (output) from mailman id 152487.281729; Wed, 07 Jul 2021 15:46: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 1m19lC-0003RP-EP; Wed, 07 Jul 2021 15:46:50 +0000 Received: by outflank-mailman (input) for mailman id 152487; Wed, 07 Jul 2021 15:46:49 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19lB-0002ed-4D for xen-devel@lists.xenproject.org; Wed, 07 Jul 2021 15:46:49 +0000 Received: from mail-pg1-x530.google.com (unknown [2607:f8b0:4864:20::530]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id f823c15d-0146-48bb-8c62-e74ba6bc6f20; Wed, 07 Jul 2021 15:46:44 +0000 (UTC) Received: by mail-pg1-x530.google.com with SMTP id v7so2681618pgl.2 for ; Wed, 07 Jul 2021 08:46:44 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:38:6b47:cf3e:bbf2:d229]) by smtp.gmail.com with ESMTPSA id q18sm23093560pgj.8.2021.07.07.08.46.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 08:46:43 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: f823c15d-0146-48bb-8c62-e74ba6bc6f20 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=0MmjMjO7XvUbpBRdqqPG7tGmikUa5z2pWikCp/JzIEY=; b=UW7L39XXXA0lFLWgWURoFcPlCEh4S4Z8KGVwXPdNWOZ1uC97xNL/QzrnRe932i5Zpl v+us9x+QqUCVOJR7NW0cti8Dlvz3ntW5JBnk3T8mrXTQQXScCfMHjoOC5qWs78n+WXHy l0/TBbeXhFNdgxncs8vmuBc0rOiOjaE2C0pPG29QKAwWxzv4x0ZxqdciLaEAszX0ad2L rytLK20HtdLSd1w9TyUEPQIWBcbAQAlFIoL9atuDdZLes8JZJ04ezb04sQO7vSDnJg/s lgt8L6SFaYrDfbO40RdC74i07ap4lhvqEU/pQl2wDRJfhNq+GPC5XQGz2XIiVhjZjRsT 5j1g== 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=0MmjMjO7XvUbpBRdqqPG7tGmikUa5z2pWikCp/JzIEY=; b=PrfrBcqv2sIE69wSNtdovZOmMq5X/Gb+VpbqMn5q0inT6dyKKUqghlBF5HHtfK4BrH TwK3oYJSiTSJNrvnycyYeKazLj8crEJcSPo8SzfJPxXgkUc+j//F1r2TensJ2EazWJc9 08Bg69IJqwfPsqlWC1lwwqxyZX35NzP3LJDb+CeIPsG0W8txYqF48LDIK4hLZalrj4r2 W4VKL0I2a5DtylM7XEZN3ZL6cosGxsF+2ncgZ5Kw92eyhHCSgLHMZsUe1+ncngwDlHb7 /sZBh4B2id0BNEpmqh4naaGAYle8M08XupOujr7MD6EyPIB9iStMWkeN20hi3jukzb9r ozjg== X-Gm-Message-State: AOAM5329ORqc7yzOldzgXuO7G6Z9D7SomAnYen74dwLtJc6/lX8+Y3z5 9gSOJuc2rqlndgqpb5CzuWc= X-Google-Smtp-Source: ABdhPJyAY8zlw6ShjL9q8KFDuhfqbUuaIuxnQh2fAt/BqNVogHlUsHJh8B4rAjBi+hH2uyVONxd4RA== X-Received: by 2002:a05:6a00:8c4:b029:2b4:8334:ed4d with SMTP id s4-20020a056a0008c4b02902b48334ed4dmr25950524pfu.36.1625672804218; Wed, 07 Jul 2021 08:46:44 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, will@kernel.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, kirill.shutemov@linux.intel.com, akpm@linux-foundation.org, rppt@kernel.org, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, ardb@kernel.org, robh@kernel.org, nramas@linux.microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, david@redhat.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, keescook@chromium.org, rientjes@google.com, hannes@cmpxchg.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, brijesh.singh@amd.com, anparri@microsoft.com Subject: [Resend RFC PATCH V4 02/13] x86/HV: Initialize shared memory boundary in the Isolation VM. Date: Wed, 7 Jul 2021 11:46:16 -0400 Message-Id: <20210707154629.3977369-3-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210707154629.3977369-1-ltykernel@gmail.com> References: <20210707154629.3977369-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1625672824963100001 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 10b2a8c10cb6..8aed689db621 100644 --- a/arch/x86/kernel/cpu/mshyperv.c +++ b/arch/x86/kernel/cpu/mshyperv.c @@ -334,6 +334,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 3ae56a29594f..2914e27b0429 100644 --- a/include/asm-generic/mshyperv.h +++ b/include/asm-generic/mshyperv.h @@ -34,8 +34,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 Fri May 3 09:54:22 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=1625672833; cv=none; d=zohomail.com; s=zohoarc; b=K7JKTV83p3C3ROnb1zZdw+trNuLtbAY0SwUkAp7D9RYePVENC4M3YDMkyohpzHWeo72boJ09Fo7GqfMFJgqH8Mfo4cY5rc60Le7zzPiMgfuQR7CzniQbrVOcVu8e6cWhcaqHJnDLtUzevq+teXGuMvCGgHGlh/2ILqgF2eHieX8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1625672833; 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=Wbx/GDjQo5wf+q3MZtzwwUpSNi9ocBIpEvvl5wWSUuY=; b=LZWJoxc6JZS1jj05tlllB1kYCw0zYu9oqTmWOovzhMkpAtpox5bhKnUvFCxoEtanYHv8yE3MonXQGlYfHQSmuWApUg5sY8Dujiqg6Iv4tQewm5TmKg6pEjP30jGRKatlRIlYetKSYoe81bxk8bjAxCfe+OLeHuoHTPRMCBXgpTg= 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 1625672833497407.4032686018995; Wed, 7 Jul 2021 08:47:13 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.152489.281740 (Exim 4.92) (envelope-from ) id 1m19lL-000415-SN; Wed, 07 Jul 2021 15:46:59 +0000 Received: by outflank-mailman (output) from mailman id 152489.281740; Wed, 07 Jul 2021 15:46:59 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19lL-00040u-OE; Wed, 07 Jul 2021 15:46:59 +0000 Received: by outflank-mailman (input) for mailman id 152489; Wed, 07 Jul 2021 15:46:59 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19lL-0002ed-4Y for xen-devel@lists.xenproject.org; Wed, 07 Jul 2021 15:46:59 +0000 Received: from mail-pg1-x52d.google.com (unknown [2607:f8b0:4864:20::52d]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id baa5ace2-e2d6-40fa-84cb-9859b6b6173e; Wed, 07 Jul 2021 15:46:47 +0000 (UTC) Received: by mail-pg1-x52d.google.com with SMTP id d12so2654901pgd.9 for ; Wed, 07 Jul 2021 08:46:47 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:38:6b47:cf3e:bbf2:d229]) by smtp.gmail.com with ESMTPSA id q18sm23093560pgj.8.2021.07.07.08.46.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 08:46:46 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: baa5ace2-e2d6-40fa-84cb-9859b6b6173e 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=Wbx/GDjQo5wf+q3MZtzwwUpSNi9ocBIpEvvl5wWSUuY=; b=ecT1hGqfYNVSX47SXb/APhCQFAswJ6vhiyOM6V1lIiA0hdFY9ZpWFZt1c9TCHdJNBQ XcXQc3/4fDbjUbmerNVJa3Dq5cdzVDozKwKEKmH6PkjWp3yHaaNxPDerJsPua4+aXTpz NAqzl+0braib2153E5+FYzVaRmn3KttlKhGB5UEIqj5z7Xk9jLP9fxgQc7bkq8yIGvGS XiQXLXNrRP/d4WnulrZZCEKmBC83CaeEczmrFGQar6E5Fq85/gHnHoR8qDQg/EnO0TBu l1qwOsj5nozRCuye6CdjI4MHIeUpFkmrrSo/j8xH4id+T6+Nv0R1VCL2jOSOWnIuEImF PNZw== 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=Wbx/GDjQo5wf+q3MZtzwwUpSNi9ocBIpEvvl5wWSUuY=; b=EcgvQcmHhDdIkd3pIc1d6XlPui0nMxmA0jmb7N0LF1TVT5593RatWt8AgwiqBO+WXW hZi0dLkRkkuoeNkTH3GLvbNqajJssAAWwxnl92YZE0HnjP3TmWzvO3ofo++E6Ar5MVaL 35gEdq/ucriKlTQmfTGQ8FR62DzIHjlcEfeZYFqdgiZcHdNn9Ey/U7uJKuQBDUEnhXGi 3gIBiFswWzoWQs/yCtK7xmcY2kEmBPACa3Eka6tCN/9b93ceOxi0wJbDROoyDryateb6 RiQabmVQKXR5U0QcbqdAWon7xHN7sJOLBtTxGPiXEd9xu67B2HAGcXU88wuCgb0adD3d Br7w== X-Gm-Message-State: AOAM531ealoHdvJVTMGD2hTd8eHQ707nulJbk6c0s+udrP/eVXp2u8XI vPne0wUMQ77orc+mOgFsRrc= X-Google-Smtp-Source: ABdhPJwxDqBKtHTZaIt8Uim4fO9bVuZt2kYRNRPcJ7s0g20n9ucfbPa+nMlRMlxLWT2zMMFJyY2OBg== X-Received: by 2002:a65:528d:: with SMTP id y13mr27233738pgp.276.1625672807122; Wed, 07 Jul 2021 08:46:47 -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, kirill.shutemov@linux.intel.com, akpm@linux-foundation.org, rppt@kernel.org, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, ardb@kernel.org, robh@kernel.org, nramas@linux.microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, david@redhat.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, keescook@chromium.org, rientjes@google.com, hannes@cmpxchg.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, brijesh.singh@amd.com, anparri@microsoft.com Subject: [Resend RFC PATCH V4 03/13] x86/HV: Add new hvcall guest address host visibility support Date: Wed, 7 Jul 2021 11:46:17 -0400 Message-Id: <20210707154629.3977369-4-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210707154629.3977369-1-ltykernel@gmail.com> References: <20210707154629.3977369-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1625672847065100003 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 606f5cc579b2..68826fbf92ca 100644 --- a/arch/x86/include/asm/hyperv-tlfs.h +++ b/arch/x86/include/asm/hyperv-tlfs.h @@ -262,6 +262,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. */ @@ -561,4 +566,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 aeacca7c4da8..2172493dc881 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -194,7 +194,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 156cd235659f..6cc83c57383d 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 515c3fb06ab3..8a0219255545 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 Fri May 3 09:54:22 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=1625672843; cv=none; d=zohomail.com; s=zohoarc; b=JlCkwg1SiUSem6SbrKIppc3xWD2YX/Co11IGRHnbfwiKoxCj1MWv4ALJa1feDpgBDkTaPOvoEILM0VcT16DnbUxkksB31DqlAZEBFi2lLolgk+RR0elSk/fYcg7gZpnID1Y5C0lBN41C+QD4jJW8i9DHIWzGFfQV0R/bLpoSCzE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1625672843; 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=PNH9Zl+OnwabTtVty58+IA0sVVSQzLozhmH+z5R7esmZPeN13s3taYDg/Z3W9jGCr2hzy9sosjGczpn2SbeXarAxCrs31taKYXG8QpNoN9LGq/LCVkx7LKtF97QJjwf2B3F6WHCYikuhL57QubYtb1cI26bjamjDS+CZV+c7QfU= 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 1625672843253148.42800958539885; Wed, 7 Jul 2021 08:47:23 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.152495.281751 (Exim 4.92) (envelope-from ) id 1m19lW-0004mV-6n; Wed, 07 Jul 2021 15:47:10 +0000 Received: by outflank-mailman (output) from mailman id 152495.281751; Wed, 07 Jul 2021 15:47:10 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19lW-0004mM-2t; Wed, 07 Jul 2021 15:47:10 +0000 Received: by outflank-mailman (input) for mailman id 152495; Wed, 07 Jul 2021 15:47:09 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19lV-0002ed-58 for xen-devel@lists.xenproject.org; Wed, 07 Jul 2021 15:47:09 +0000 Received: from mail-pg1-x52c.google.com (unknown [2607:f8b0:4864:20::52c]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 3179204f-2683-4a13-a32d-f4f409af832b; Wed, 07 Jul 2021 15:46:50 +0000 (UTC) Received: by mail-pg1-x52c.google.com with SMTP id u14so2645814pga.11 for ; Wed, 07 Jul 2021 08:46:50 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:38:6b47:cf3e:bbf2:d229]) by smtp.gmail.com with ESMTPSA id q18sm23093560pgj.8.2021.07.07.08.46.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 08:46:49 -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: 3179204f-2683-4a13-a32d-f4f409af832b 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=lJrhgA1XTGfrv91uZ+jA8dtUDhjHzVehhtyPYHx4wY0tTXW2S4H7JfDXfyBeh2nD2o 9mVqg3wXPbSUSV6fPlu3C/AZKJh4dS9KD9xwQPpzyrcN3Xu13jLVp8aQSrrWIFKNZQeD 4G5G+wOLbTGP+A36EMXtcYoEySmW1Yi9WUlsc0j8F0qDh74W3Rp/ZaJhaGIxt/xKePha pD4wQ6tbM0JeW/zDuyKLpBxE/Rby1/lAn8KOSufgT/PSTFCbfQTpKjOK8zSVUOuIvlix z8rSVyoVFgMq0pQ1W5PcjZjPWnGlDLdW8cgZhFWyTykNUw0TChYbLtpnEaROSGBsJoHX 5X9A== 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=Ue0YpNP7dII2EBtxFsLb1t4oYC/mgVduKtEgSpv5BZmgcNKy+M/uEHVtTegSsRAa+M AiyCjO4jRxSJYH7koqXDH0crk8miehvBCgc/xnHZ/euVZrRPwSZUaleSiQUTV4dwUdz7 Y9iLfTriy1RkykzmRdYIC76Pmv1D1od1ic/277x5ElnOcPY9IkTpVW5855ONVdUn+mlM Dci7jS2FPxXuzPmQnvpxzorZyGvqL/Dfy327CdDdBB2naNDNbiaW4OQ1JrfeseCRw+CH wkCEsG3VIH+ELgDP7toZavDqOGn7kXR5ZUWSaX39Nk4L4JGL+ysw05Y8DQDoJ9h8YRNi cR9w== X-Gm-Message-State: AOAM531MiiZxyEthpZj5MuKyNd5LLHgM/g0vrgAgIJVi89BRf1+5xF1V NDNxRDv/HAzXCS9fQAFYsKU= X-Google-Smtp-Source: ABdhPJxTu4cFz1SK0NY8gSx4hi5ea6vbpE+9icHBySCQX8uZ+4a9XgaS7eR3olRpzqi9WXosELCn4g== X-Received: by 2002:a63:807:: with SMTP id 7mr15130001pgi.122.1625672809994; Wed, 07 Jul 2021 08:46:49 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, will@kernel.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, kirill.shutemov@linux.intel.com, akpm@linux-foundation.org, rppt@kernel.org, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, ardb@kernel.org, robh@kernel.org, nramas@linux.microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, david@redhat.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, keescook@chromium.org, rientjes@google.com, hannes@cmpxchg.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, brijesh.singh@amd.com, anparri@microsoft.com Subject: [Resend RFC PATCH V4 04/13] HV: Mark vmbus ring buffer visible to host in Isolation VM Date: Wed, 7 Jul 2021 11:46:18 -0400 Message-Id: <20210707154629.3977369-5-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210707154629.3977369-1-ltykernel@gmail.com> References: <20210707154629.3977369-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1625672844875100001 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 Fri May 3 09:54:22 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=1625672849; cv=none; d=zohomail.com; s=zohoarc; b=XO1o/Jb3OJnMJWeRqYxZPtcqlip/1JEEZNlKjxAYmC5q0hA3mdyJFoAud5rmEyRyUN9Q6zVkymtxG3882bB1atEtiS68RfgmGCKQo94/Z1dkJ16+3aMHM2iStnEBaZYlISa36cskpjjtah9T1l5ovsh757yKltHC06g78gS2FRg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1625672849; 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=Yf6ZTtN7PeYt6X8aFjGtHsBACJgt9SiJhwxT0+Mfzbw=; b=Qg9cDo5mgLRP/JDtY6xhKzOq5cwm/czJp/39lggwKzcXMO/9dqv3WzvOfKbAADdcbwrKXJVh4V1c5yT6LAU7CM6dbnKplWyTTqby8sL5ClY+RCik7PzxmnvjqnYL8FPOW7mmFM2fCPSQBRHgQO4EmnY+wAb8aVXEqTmz5jTWAis= 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 1625672849669712.9256459476039; Wed, 7 Jul 2021 08:47:29 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.152497.281762 (Exim 4.92) (envelope-from ) id 1m19lb-0005ED-LO; Wed, 07 Jul 2021 15:47:15 +0000 Received: by outflank-mailman (output) from mailman id 152497.281762; Wed, 07 Jul 2021 15:47:15 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19lb-0005Dx-Hf; Wed, 07 Jul 2021 15:47:15 +0000 Received: by outflank-mailman (input) for mailman id 152497; Wed, 07 Jul 2021 15:47:14 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19la-0002ed-5Q for xen-devel@lists.xenproject.org; Wed, 07 Jul 2021 15:47:14 +0000 Received: from mail-pf1-x42a.google.com (unknown [2607:f8b0:4864:20::42a]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id ec27e03c-a224-4bcc-842b-d09f70cf2ac3; Wed, 07 Jul 2021 15:46:53 +0000 (UTC) Received: by mail-pf1-x42a.google.com with SMTP id 17so2552682pfz.4 for ; Wed, 07 Jul 2021 08:46:53 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:38:6b47:cf3e:bbf2:d229]) by smtp.gmail.com with ESMTPSA id q18sm23093560pgj.8.2021.07.07.08.46.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 08:46:52 -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: ec27e03c-a224-4bcc-842b-d09f70cf2ac3 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=Yf6ZTtN7PeYt6X8aFjGtHsBACJgt9SiJhwxT0+Mfzbw=; b=sifWWUzXmixvnGdsQykdK2Wy6G+ZMy9MY6kv6S7iets5sYOfuSTxai3Y++EFR9zQL1 6N2QQZHUhXBcXABah5k6DsIZzbwYym3TPlwmbwhwCPJk/3LVH5IeQAIeJlKVFbEFhg8R CUZlBsj1jbcK/UAIsHAPwaTk/Zdn0J/k87BLgyXQYIphH8+IBOQ7Y30r2Hdp3s9bPTS3 YEwlNsQXEo7trKuzcvB1DrSMIGkZu/rJsd2K1t0spLWK9Ysx/Nhe5lfuP3g/3TmHp4sa F+7WngQNJM5y8InhFNVWvrTb7DBrvhzuJusVsn7iKACWSfBjoD8B/fJyOdcyWr49GIS+ tX0w== 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=Yf6ZTtN7PeYt6X8aFjGtHsBACJgt9SiJhwxT0+Mfzbw=; b=IE+CWQmKPxzMZuJFzasqSSyHmH38GWqcwski5QVhIyWxWm0DOFVLXVdgFeqEzZjn1o SSld5KuCr4yhagWSImcokb7Y71q6WyK15r5YDBVwvvNJtXIdDVygNEnzynmyqWqrIEzu Yl0dw0UQ6WRhuFfKt5uO7mWWJqA9bIuaefc7q4Aipdesn29VfFxDztzUGnEXKcdEB+oD fZlW1Gzqb9DsyG5qqIhmb4DA+rDsJZ+GRDXzvUaCQHIebFAW6rL7UHOYWZyH2Y5sd0Yp v8xjhhhi7kAQi0WffKH22S4FWWyEbqqt82LugSm22oTmK7I5Lafp6PSGvgxyEyYTdWs0 WLzA== X-Gm-Message-State: AOAM533rpcxUazB0IZ3wZlWHaynOb5ub65a3tib1dCm1m+72046ktTzV kdCSTHlrSxB5uChIrsufirV5hGVMO4oRsjRu X-Google-Smtp-Source: ABdhPJxPmY8rrNBIgMOufu5t49wD5FKe56ShTc3mMOWTqCdj4DWoMBZU8fK92CgFTTjFWXKCyNp4Gg== X-Received: by 2002:a63:1841:: with SMTP id 1mr26554315pgy.377.1625672812665; Wed, 07 Jul 2021 08:46:52 -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, kirill.shutemov@linux.intel.com, akpm@linux-foundation.org, rppt@kernel.org, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, ardb@kernel.org, robh@kernel.org, nramas@linux.microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, david@redhat.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, keescook@chromium.org, rientjes@google.com, hannes@cmpxchg.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, brijesh.singh@amd.com, anparri@microsoft.com Subject: [Resend RFC PATCH V4 05/13] HV: Add Write/Read MSR registers via ghcb page Date: Wed, 7 Jul 2021 11:46:19 -0400 Message-Id: <20210707154629.3977369-6-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210707154629.3977369-1-ltykernel@gmail.com> References: <20210707154629.3977369-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1625672851910100001 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 | 25 +------ arch/x86/hyperv/ivm.c | 115 ++++++++++++++++++++++++++++++ arch/x86/include/asm/mshyperv.h | 78 +++++++++++++++++++- arch/x86/include/asm/sev-es.h | 4 ++ arch/x86/kernel/cpu/mshyperv.c | 3 + arch/x86/kernel/sev-es-shared.c | 21 ++++-- drivers/hv/hv.c | 121 ++++++++++++++++++++++---------- include/asm-generic/mshyperv.h | 12 +++- 8 files changed, 308 insertions(+), 71 deletions(-) diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c index e058f72eeee5..97d1c774cfce 100644 --- a/arch/x86/hyperv/hv_init.c +++ b/arch/x86/hyperv/hv_init.c @@ -445,7 +445,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 @@ -539,6 +539,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, @@ -620,28 +621,6 @@ bool hv_is_hibernation_supported(void) } EXPORT_SYMBOL_GPL(hv_is_hibernation_supported); =20 -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; -} -EXPORT_SYMBOL_GPL(hv_is_isolation_supported); - -DEFINE_STATIC_KEY_FALSE(isolation_type_snp); - -bool hv_isolation_type_snp(void) -{ - return static_branch_unlikely(&isolation_type_snp); -} -EXPORT_SYMBOL_GPL(hv_isolation_type_snp); - /* Bit mask of the extended capability to query: see HV_EXT_CAPABILITY_xxx= */ bool hv_query_ext_cap(u64 cap_query) { diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index 24a58795abd8..c7b54631ca0d 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,119 @@ #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; +} +EXPORT_SYMBOL_GPL(hv_is_isolation_supported); + +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 2172493dc881..f9cc3753040a 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); @@ -196,6 +253,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) {} @@ -212,9 +288,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-es.h b/arch/x86/include/asm/sev-es.h index cf1d957c7091..482a4bb92c48 100644 --- a/arch/x86/include/asm/sev-es.h +++ b/arch/x86/include/asm/sev-es.h @@ -103,6 +103,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); +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 8aed689db621..d9ac13230950 100644 --- a/arch/x86/kernel/cpu/mshyperv.c +++ b/arch/x86/kernel/cpu/mshyperv.c @@ -339,6 +339,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 (ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED) { diff --git a/arch/x86/kernel/sev-es-shared.c b/arch/x86/kernel/sev-es-share= d.c index 0aa9f13efd57..b7da08c97d2c 100644 --- a/arch/x86/kernel/sev-es-shared.c +++ b/arch/x86/kernel/sev-es-shared.c @@ -93,10 +93,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 @@ -108,7 +108,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) { @@ -119,7 +128,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 2914e27b0429..e6d6886faed1 100644 --- a/include/asm-generic/mshyperv.h +++ b/include/asm-generic/mshyperv.h @@ -22,9 +22,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; @@ -44,13 +51,14 @@ 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; =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) @@ -145,7 +153,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 Fri May 3 09:54:22 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=1625672853; cv=none; d=zohomail.com; s=zohoarc; b=kqzBl+1cn5mvw7Sn7QhWsV1zY6ZU+g9FTNcP93RKaJoEumaBtvA/N2Yxqw+t3T6sJ1TPHhHWxmrnk8bPIP1nqpylIju6HbILNwAB2Jhn7YXlaDs0efJNOmjf4h0LsRMb/5oFv30W3nmpBVMPPrstgqP17ekj4J92JUJpchhDw34= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1625672853; 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=XnnD+WKjmMaIcoxzXQeIqu3OOT9S8jIhhegPGmtXPps=; b=HwIyG+OWGgDGDhiqpYX13isZcZxUg2IpuDPGaWBT5CJAPBf4pzxhlSxdOXj6v8sV7Z1oGoa0xtSlVtfOczpsG0eNfftjsZSGSRof81yhnNpzLtAbKUoSWaz9Rpp/WiBUNn4Acq41YYiZUWxNTCCVkVhXecgGoGvNVlshFieuTJo= 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 1625672853164361.3909726320262; Wed, 7 Jul 2021 08:47:33 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.152499.281773 (Exim 4.92) (envelope-from ) id 1m19lg-0005eY-12; Wed, 07 Jul 2021 15:47:20 +0000 Received: by outflank-mailman (output) from mailman id 152499.281773; Wed, 07 Jul 2021 15:47:19 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19lf-0005eM-SD; Wed, 07 Jul 2021 15:47:19 +0000 Received: by outflank-mailman (input) for mailman id 152499; Wed, 07 Jul 2021 15:47:19 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19lf-0002ed-5U for xen-devel@lists.xenproject.org; Wed, 07 Jul 2021 15:47:19 +0000 Received: from mail-pl1-x62d.google.com (unknown [2607:f8b0:4864:20::62d]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id ea1a7335-d03b-4b96-a550-7409c2ddb71f; Wed, 07 Jul 2021 15:46:56 +0000 (UTC) Received: by mail-pl1-x62d.google.com with SMTP id p17so764633plf.12 for ; Wed, 07 Jul 2021 08:46:56 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:38:6b47:cf3e:bbf2:d229]) by smtp.gmail.com with ESMTPSA id q18sm23093560pgj.8.2021.07.07.08.46.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 08:46:55 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: ea1a7335-d03b-4b96-a550-7409c2ddb71f 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=XnnD+WKjmMaIcoxzXQeIqu3OOT9S8jIhhegPGmtXPps=; b=agixlmBytfRil/fkf4OgDDmdZCvfuSwnj5sy2vnE2pYjSr4fq49TUp4T0f/DBWL+R7 dtp0xUVHFyRDbO2ZZHZG47mGHjJiVyYQesO2NN0JFKBXh2xwgv0l85XvXqUywSG8Fvr0 1Ul+fpcelmkcjAQw+bszpcRSz+GI7SFKNDQMiNrze7nfUoRflIMbr8UPrWEDWdIiLHWT 977bhPck25q/c2/QSy5qX/caMDzXJuO73RHyL0IAREsmggF+7GypT2XtMaRNeZvwgssJ Jb9R+KHghWzumV11pGybgCdu9V73FPISKwDj6Kc2id9ZDjTW1viAcgETkpNHjkay/0uH yUMg== 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=XnnD+WKjmMaIcoxzXQeIqu3OOT9S8jIhhegPGmtXPps=; b=dDnYE2WNJHzD0PM9mvHQkxIVhtlRjYvk2WDY6QLHF0piDiqIL90cFuWICJkul+BHJG 5YbQTIJvDf7m4EYuf0qZXOfZpY1Z7nUyWY59n1Vmlq8CMCOtGzmXwiMx1JEy+8HuU7Tj 7HBuCoABlbBBL9yrHmdEI2HjXZvQcskeVuHFDx98AZJ5eXOYb8rH6ATdCpjB81YfbKgI /YbkzPNiD+DGKlHNhcTsBA/qW378kraif9Q5JWghkqv+JX6MPDjf32hOZW4YFDRndxGf k4d7e710GkpS+C6YqOYDI3IiuJTPv92aNqtIdo8xd5T4ueupbbcmtK6F1VV+UuUBuvaG rS/g== X-Gm-Message-State: AOAM532gx/L/Rei/bvm9IHENs+wiKRZcqQ3BF8iIWbkMDqGU6+WWN8zw 7eHetpRKw6dAmHTsABGXeC8= X-Google-Smtp-Source: ABdhPJwr1zQl10i7qLwixIOxPsOw0GmooMgWPeefwIHrZFrrW9J6CDnj4do29d88YBIZx41hhaEGeQ== X-Received: by 2002:a17:902:ed82:b029:ef:48c8:128e with SMTP id e2-20020a170902ed82b02900ef48c8128emr21831521plj.72.1625672815405; Wed, 07 Jul 2021 08:46:55 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, will@kernel.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, kirill.shutemov@linux.intel.com, akpm@linux-foundation.org, rppt@kernel.org, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, ardb@kernel.org, robh@kernel.org, nramas@linux.microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, david@redhat.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, keescook@chromium.org, rientjes@google.com, hannes@cmpxchg.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, brijesh.singh@amd.com, anparri@microsoft.com Subject: [Resend RFC PATCH V4 06/13] HV: Add ghcb hvcall support for SNP VM Date: Wed, 7 Jul 2021 11:46:20 -0400 Message-Id: <20210707154629.3977369-7-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210707154629.3977369-1-ltykernel@gmail.com> References: <20210707154629.3977369-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1625672854435100005 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 c7b54631ca0d..8a6f4e9e3d6c 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 f9cc3753040a..fe03e3e833ac 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -258,6 +258,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 311cd005b3be..186fd4c8acd4 100644 --- a/drivers/hv/connection.c +++ b/drivers/hv/connection.c @@ -445,6 +445,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 e6d6886faed1..8f6f283fb5b5 100644 --- a/include/asm-generic/mshyperv.h +++ b/include/asm-generic/mshyperv.h @@ -30,6 +30,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 Fri May 3 09:54:22 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=1625672859; cv=none; d=zohomail.com; s=zohoarc; b=DIBDHpdOa7ppEXcBS+wuLIqzHhJkI5Yj5Wokhelb7NGiB4+n1jjw1NhvTMzAKhJwm1LiSw6UR2njRIxElbm0sd3akthKdz17kI6RQEvMtjIdFkxlq8SxQ01Rk8MW4Rsism0mJw3rT/vAEbC5SyCSupKsTGWNkYjev+zAFQv9XaU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1625672859; 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=DW09bNLqhYERQ+eFNE9hKu+s7sJ/vWcqsCTKXxunGqM=; b=MWbnx4NPA3CaSZ5e/ajN3Z5rRtZrhkmfg4dhF2gK67APIdpAmbCz9+PVxWTSqCnOujQUbvGu90jp0EmrOPGC6u90RJo088wiWOEDnvMDaWBxaveQdJ4TGqvFXNbz0Yrboo96mPMEmk4l14Ix5a+iWtIG1wcFFrU46/Ei3NDm9+w= 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 16256728594454.569310343560687; Wed, 7 Jul 2021 08:47:39 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.152502.281784 (Exim 4.92) (envelope-from ) id 1m19ll-00069F-Fb; Wed, 07 Jul 2021 15:47:25 +0000 Received: by outflank-mailman (output) from mailman id 152502.281784; Wed, 07 Jul 2021 15:47:25 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19ll-000697-B6; Wed, 07 Jul 2021 15:47:25 +0000 Received: by outflank-mailman (input) for mailman id 152502; Wed, 07 Jul 2021 15:47:24 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19lk-0002ed-5e for xen-devel@lists.xenproject.org; Wed, 07 Jul 2021 15:47:24 +0000 Received: from mail-pf1-x436.google.com (unknown [2607:f8b0:4864:20::436]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id affef5dc-2f85-4946-b791-bae7063d5beb; Wed, 07 Jul 2021 15:46:58 +0000 (UTC) Received: by mail-pf1-x436.google.com with SMTP id j199so2543224pfd.7 for ; Wed, 07 Jul 2021 08:46:58 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:38:6b47:cf3e:bbf2:d229]) by smtp.gmail.com with ESMTPSA id q18sm23093560pgj.8.2021.07.07.08.46.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 08:46:57 -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: affef5dc-2f85-4946-b791-bae7063d5beb 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=DW09bNLqhYERQ+eFNE9hKu+s7sJ/vWcqsCTKXxunGqM=; b=pzPZ4UbUjcWFHUdSVhYVjk9YtvOICnXi9Nj7qM0jD7NcOmMUzwlYm+mq4sVKSXHo4s zwWoOJE8e1JyIkMkk3/mjZ1AiiVZ0IENGQNVf2MOGDQLBposH64f6hW3VT0Cq2PsVCbr tOMBrJ9MBTTtQMescRAbw8HnjXqdHg2GSt7Vi8nWw5jrMF0tRLBbJCu3V07Jgtd5wXA9 uyw3Qhuaro5O/6Tkkc6mOCO+9IQ1PdZdh1X3OflWYtFDTM7eEh2Db1vHkZYha9dY0Rx3 Cm1uV3hTpgrxDjAS27M3/n9PWHnqdTXISGW4NU3SE49KizVIaa4miaPta4Ng6xcTA8rq b3fg== 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=DW09bNLqhYERQ+eFNE9hKu+s7sJ/vWcqsCTKXxunGqM=; b=jE2+hbilae/a8l0vyUDm5IPjawYzIkIffG+SjLd/YBdIYCyzmr9caZNPOoLAPC3hOW qtfKq5gzDmgCP6aW9xSZzudaH8fU+kSUWQf2hXLggIatKJutZj9JY0bZ6h6+KwxedeRD dd7e/+GBIyyWcGXL+5xjROosao2oWFNAvd1BVC2oPvQbSliqG7swkMsgfakiZFmMmYRg Wm6w2Fx3KTDK5T0IvsPYxokxHS5EnuUeof24iLDlYa2EnQXHRTElgkzffeP8Lppoe9UM Xu5AtBbkWrC5xAsSSELcazpyVcsGO5EFo5yEIWZgVIv9n1qYFTOu353N8rtqDbwzih6x 08Zw== X-Gm-Message-State: AOAM533R1jKJ0N2+C3wlKc3UWy9ZOqYz1WYAJdpQ0nXrVL3JJrKNv3t4 bgxp4gYCE5NkC4N28zDw95U= X-Google-Smtp-Source: ABdhPJwKvS+dtvQGx81eyCbmtw1G8LgUyQrXwz4RZg4n8DLW529IghRF7a3TI1kMxl+iEubpWMtjRg== X-Received: by 2002:aa7:8d86:0:b029:2ec:82d2:5805 with SMTP id i6-20020aa78d860000b02902ec82d25805mr26174427pfr.11.1625672818255; Wed, 07 Jul 2021 08:46:58 -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, kirill.shutemov@linux.intel.com, akpm@linux-foundation.org, rppt@kernel.org, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, ardb@kernel.org, robh@kernel.org, nramas@linux.microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, david@redhat.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, keescook@chromium.org, rientjes@google.com, hannes@cmpxchg.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, brijesh.singh@amd.com, anparri@microsoft.com Subject: [Resend RFC PATCH V4 07/13] HV/Vmbus: Add SNP support for VMbus channel initiate message Date: Wed, 7 Jul 2021 11:46:21 -0400 Message-Id: <20210707154629.3977369-8-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210707154629.3977369-1-ltykernel@gmail.com> References: <20210707154629.3977369-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1625672861233100001 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 186fd4c8acd4..a32bde143e4c 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); @@ -282,6 +325,8 @@ int vmbus_connect(void) =20 void vmbus_disconnect(void) { + u64 pfn[2]; + /* * First send the unload request to the host. */ @@ -301,6 +346,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 Fri May 3 09:54:23 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=1625673123; cv=none; d=zohomail.com; s=zohoarc; b=mu2XUxNedGCuRNTVoQAJtdIqYYvROggT6Cr9jlBieFugEuJpvCBJ6+rxBto5IWUnvpVR0A99DSQVrJMshz96+Ls2dgvzYqPvpuxwptd8edanugdUKi8u4+U+7olI7l4pB6FwZIBD57v+KXaDcfw1R8m2Ps2rv3ZkBZpbJTULb6c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1625673123; 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=Xp1bQp5JOyCFZXg+YALrN5u8MoJIk4N+hCNczh9twE509ygwq5tC+XAuMiAUKBpKCBZnXkO4LheVaCTswX0TjBDdhu2xqtEM5RzsUzUPj6o1rWQYP7CT4Y6LOtCItbxTqsw2PzAezvdyTJIDKbbzmBMH1OuFnQVgDmmU9QW6U4o= 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 1625673123392709.0602726544695; Wed, 7 Jul 2021 08:52:03 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.152546.281839 (Exim 4.92) (envelope-from ) id 1m19q2-0001si-AT; Wed, 07 Jul 2021 15:51:50 +0000 Received: by outflank-mailman (output) from mailman id 152546.281839; Wed, 07 Jul 2021 15:51: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 1m19q2-0001sH-5k; Wed, 07 Jul 2021 15:51:50 +0000 Received: by outflank-mailman (input) for mailman id 152546; Wed, 07 Jul 2021 15:51:48 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19lp-0002ed-5v for xen-devel@lists.xenproject.org; Wed, 07 Jul 2021 15:47:29 +0000 Received: from mail-pg1-x52b.google.com (unknown [2607:f8b0:4864:20::52b]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id ca013a0e-bb47-48a7-a1bf-4e23d3020233; Wed, 07 Jul 2021 15:47:02 +0000 (UTC) Received: by mail-pg1-x52b.google.com with SMTP id y17so2638554pgf.12 for ; Wed, 07 Jul 2021 08:47:02 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:38:6b47:cf3e:bbf2:d229]) by smtp.gmail.com with ESMTPSA id q18sm23093560pgj.8.2021.07.07.08.47.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 08:47:01 -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: ca013a0e-bb47-48a7-a1bf-4e23d3020233 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=MBlsmW2EGc08awn3pn+tF2cqU94Qi2j0RVzs5T4d5AWeM6k/iatip8No/fth4gt9MR DY81PMkdApCNFlfGSXT5ZtO5yenK2w0MxW3c3j6XHgi8khfVG4V+nYRb49rx9z97vYwN EXDGk7wfoTl45kU/4faVLyVELy4NMAW2A43MLsJxZrItmuhi8rW1cp0heFAuSOf2R28x Cdrg7vVbFw5bk7Im74s4nCeNM+VaOOlX/fZbJKtrHJBTRH6fMjfeJ9wn+Xr6ll3vgXPN ruab8PwcdsmrtjxBo3HQcisTjo+FaKBnaB/kp55dKdYw8WoQazY4D+WM4LEzDXF1ogQV TxQg== 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=NxnX8UuXVrZEmdRbXWIbM21MxNvlP85tliL73sE3edQjUfLK07aknSOalSIShACd16 Qndzmtk2ant/gGINyb7n2TSX/eTaMisgUGbU9YfAhl9+sLjZzXz286xZS5C0jTLFUx5L 2txbcS6Wy9ufVtD/pjryajHajwAIIuOv0mL9226PYBH2uksImt4T5GwoHxNwL1EmV84U 9uoyOdSlpY+AICccas6qc2Kk4x4TnZlsQqEK7wNW1ZJkd+cuFxXaCrUqJy8bA5J+BAxu IISYSAipgixJ7sM4FqUdCPv/NjbSdquhEGUpGwe3UMD5eU9NJpjbFi3S7niP7ziohURX 5oeA== X-Gm-Message-State: AOAM533wtX7/ZsM34Nlph82MCZf1UbnmM+2FeK8ES2bHkUwuns5jNgFL oKVx+YUt6sHkcivWanpkMVM= X-Google-Smtp-Source: ABdhPJxw02vtsDYKuD6z1M2sOL4tGys2fRSpSI1IN+SqrITeBu4uXF/xWC90CwUdn5ZrShyZXZ6xuA== X-Received: by 2002:a05:6a00:158b:b029:319:c68a:8006 with SMTP id u11-20020a056a00158bb0290319c68a8006mr23629213pfk.7.1625672821514; Wed, 07 Jul 2021 08:47:01 -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, kirill.shutemov@linux.intel.com, akpm@linux-foundation.org, rppt@kernel.org, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, ardb@kernel.org, robh@kernel.org, nramas@linux.microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, david@redhat.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, keescook@chromium.org, rientjes@google.com, hannes@cmpxchg.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, brijesh.singh@amd.com, anparri@microsoft.com Subject: [Resend RFC PATCH V4 08/13] HV/Vmbus: Initialize VMbus ring buffer for Isolation VM Date: Wed, 7 Jul 2021 11:46:22 -0400 Message-Id: <20210707154629.3977369-9-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210707154629.3977369-1-ltykernel@gmail.com> References: <20210707154629.3977369-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1625673123829100005 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 Fri May 3 09:54:23 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=1625673120; cv=none; d=zohomail.com; s=zohoarc; b=CS0jPOgwR8Ij11YPrbQGP7sCK778U0LA+hgi+SaKZxYoICb6hXVI8FypoBEyHIvbWAL0KiF13BAGMK2YZL5gxKEo0Rq9l7F7DM5emCCRZBYfaGCvtYQa304z+HHj7Mqjraiyry17Mjh9n6yPhw0WZW8vghKx/ep34TxCMOLZNdQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1625673120; 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=vp54ZQawAXt49CkThDbFLyNwiKlz10UxxnQhsKUA4kk=; b=MNmAgoYv7fOQPXeTs6VXmIhBh5RDb806jfxpcntMOwTkB1vQ00paxyyHUq9Ul80I3ai/NeUqz6dZOhIgmWe8Z9O6zysrUjEBETNhCzBBpi7fC1y/q+EYVe7sBXJ9qUlxYYhLT7KlQZkeL7X5W8s2UKp4Kwhjmw3saJdrPCSo4ec= 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 1625673120530340.9935320429952; Wed, 7 Jul 2021 08:52:00 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.152544.281828 (Exim 4.92) (envelope-from ) id 1m19pz-0001WR-14; Wed, 07 Jul 2021 15:51:47 +0000 Received: by outflank-mailman (output) from mailman id 152544.281828; Wed, 07 Jul 2021 15:51:47 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19py-0001WG-UE; Wed, 07 Jul 2021 15:51:46 +0000 Received: by outflank-mailman (input) for mailman id 152544; Wed, 07 Jul 2021 15:51: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 1m19lz-0002ed-6B for xen-devel@lists.xenproject.org; Wed, 07 Jul 2021 15:47:39 +0000 Received: from mail-pf1-x42d.google.com (unknown [2607:f8b0:4864:20::42d]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id f0b9a787-f737-4412-9481-afb69935da3d; Wed, 07 Jul 2021 15:47:05 +0000 (UTC) Received: by mail-pf1-x42d.google.com with SMTP id d12so2561848pfj.2 for ; Wed, 07 Jul 2021 08:47:05 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:38:6b47:cf3e:bbf2:d229]) by smtp.gmail.com with ESMTPSA id q18sm23093560pgj.8.2021.07.07.08.47.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 08:47:04 -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: f0b9a787-f737-4412-9481-afb69935da3d 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=vp54ZQawAXt49CkThDbFLyNwiKlz10UxxnQhsKUA4kk=; b=PnlYogD/c2tN+V//C2WWfqx2lkMsJm1KP/m5bsJdd/PBU3lKDLf3vgf1RfecUffbi5 Cn7eWqP/lJmDf4AcC5jTnFHI8XDKLv2pE7h5Kbl/PQr4jI++do5kQr3pjjZRg6OGZ1WI 4u3fUGsl1MLWEe26rGgo5F/gcLmw1fVA6or1qg3AXu5StUu6LTt905O7YR6NgXLAdA1Z Wc67MSOKPYhXSEqyPriybFeq5TXPFivuoUb3GBT9BOxPs6zLiJpMZR21Jo6GqowJg/XZ v90GyR43xecqXty3TDAWFr19k61piW/xLMYHzk4228TCmQNNj0RgkQUAinOcvSP9o8nP hsGQ== 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=vp54ZQawAXt49CkThDbFLyNwiKlz10UxxnQhsKUA4kk=; b=pHOBsPeJUTv3Ytsd3r5jH9ZmI83HrOrPBmUDWZhix/Sqvd6aNo/okX8NiOtNHAJsBe 2hDywQNXTMei3aakZp6RehwdHAsZhQc/Ej0cc/p5+a4vXgtb4UHJ5ioKj0z3KYL5atIH wJHKkYLlwvbcgGs5FQNLGFaqeuNBKs8iyhn4yXiUU0swi7nmAUqMZw2Fl33DtDjcNK72 a9kQUkbSy6ZIOgRVeQ4CzIdTn+78c4JrXRkj2IWBX5DgygLUhlOaOaph/DlRID9PQqXL lstF16VOOuphmRnGwwVpFcLPbUNSVucng11y5JpbAguaNvEME6hooXtYRwQhQYp/8taH fcBA== X-Gm-Message-State: AOAM53349n4/we6l1sSEIOEJMAENMdYp7aUPZqkfV3GJmWJHCK3tbD3f +FfSI7DonKhrW1L6beX1iGg= X-Google-Smtp-Source: ABdhPJxiQoR7b9/H9OUOgdHQKh0PkV/Pa6tXWYSXPcKMMkE7EjBhqX/P5/cFjinfyZC3Zy0XtSMGAQ== X-Received: by 2002:a05:6a00:2491:b029:308:d524:1a26 with SMTP id c17-20020a056a002491b0290308d5241a26mr25712835pfv.45.1625672824452; Wed, 07 Jul 2021 08:47:04 -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, kirill.shutemov@linux.intel.com, akpm@linux-foundation.org, rppt@kernel.org, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, ardb@kernel.org, robh@kernel.org, nramas@linux.microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, david@redhat.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, keescook@chromium.org, rientjes@google.com, hannes@cmpxchg.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, brijesh.singh@amd.com, anparri@microsoft.com Subject: [Resend RFC PATCH V4 09/13] x86/Swiotlb/HV: Add Swiotlb bounce buffer remap function for HV IVM Date: Wed, 7 Jul 2021 11:46:23 -0400 Message-Id: <20210707154629.3977369-10-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210707154629.3977369-1-ltykernel@gmail.com> References: <20210707154629.3977369-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1625673121551100001 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. Introduce set_memory_decrypted_map() function to decrypt memory and remap memory with platform callback. Use set_memory_decrypted_ map() in the swiotlb code, store remap address returned by the new API and use the remap address to copy data from/to swiotlb bounce buffer. Signed-off-by: Tianyu Lan --- arch/x86/hyperv/ivm.c | 30 ++++++++++++++++++++++++++++++ arch/x86/include/asm/mshyperv.h | 2 ++ arch/x86/include/asm/set_memory.h | 2 ++ arch/x86/mm/pat/set_memory.c | 28 ++++++++++++++++++++++++++++ include/linux/swiotlb.h | 4 ++++ kernel/dma/swiotlb.c | 11 ++++++++--- 6 files changed, 74 insertions(+), 3 deletions(-) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index 8a6f4e9e3d6c..ea33935e0c17 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -267,3 +267,33 @@ 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. + */ +unsigned long hv_map_memory(unsigned long addr, unsigned long size) +{ + unsigned long *pfns =3D kcalloc(size / HV_HYP_PAGE_SIZE, + sizeof(unsigned long), + GFP_KERNEL); + unsigned long 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((void *)addr + i * HV_HYP_PAGE_SIZE) + + (ms_hyperv.shared_gpa_boundary >> HV_HYP_PAGE_SHIFT); + + vaddr =3D (unsigned long)vmap_pfn(pfns, size / HV_HYP_PAGE_SIZE, + PAGE_KERNEL_IO); + kfree(pfns); + + return vaddr; +} + +void hv_unmap_memory(unsigned long addr) +{ + vunmap((void *)addr); +} diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyper= v.h index fe03e3e833ac..ba3cb9e32fdb 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -253,6 +253,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); +unsigned long hv_map_memory(unsigned long addr, unsigned long size); +void hv_unmap_memory(unsigned long 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/include/asm/set_memory.h b/arch/x86/include/asm/set_m= emory.h index 43fa081a1adb..7a2117931830 100644 --- a/arch/x86/include/asm/set_memory.h +++ b/arch/x86/include/asm/set_memory.h @@ -49,6 +49,8 @@ int set_memory_decrypted(unsigned long addr, int numpages= ); int set_memory_np_noalias(unsigned long addr, int numpages); int set_memory_nonglobal(unsigned long addr, int numpages); int set_memory_global(unsigned long addr, int numpages); +unsigned long set_memory_decrypted_map(unsigned long addr, unsigned long s= ize); +int set_memory_encrypted_unmap(unsigned long addr, unsigned long size); =20 int set_pages_array_uc(struct page **pages, int addrinarray); int set_pages_array_wc(struct page **pages, int addrinarray); diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index 6cc83c57383d..5d4d3963f4a2 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2039,6 +2039,34 @@ int set_memory_decrypted(unsigned long addr, int num= pages) } EXPORT_SYMBOL_GPL(set_memory_decrypted); =20 +static unsigned long __map_memory(unsigned long addr, unsigned long size) +{ + if (hv_is_isolation_supported()) + return hv_map_memory(addr, size); + + return addr; +} + +static void __unmap_memory(unsigned long addr) +{ + if (hv_is_isolation_supported()) + hv_unmap_memory(addr); +} + +unsigned long set_memory_decrypted_map(unsigned long addr, unsigned long s= ize) +{ + if (__set_memory_enc_dec(addr, size / PAGE_SIZE, false)) + return (unsigned long)NULL; + + return __map_memory(addr, size); +} + +int set_memory_encrypted_unmap(unsigned long addr, unsigned long size) +{ + __unmap_memory(addr); + return __set_memory_enc_dec(addr, size / PAGE_SIZE, true); +} + int set_pages_uc(struct page *page, int numpages) { unsigned long addr =3D (unsigned long)page_address(page); diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index f507e3eacbea..5c6f6c7380ef 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. + * @vstart: The virtual start address of the swiotlb memory pool. The swio= tlb + * 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 *vstart; unsigned long nslabs; unsigned long used; unsigned int index; diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index d3fa4669229b..9911817250a8 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->vstart =3D (void *)set_memory_decrypted_map((unsigned long)vaddr, by= tes); + if (!mem->vstart) { + pr_err("Failed to decrypt memory.\n"); + return; + } + + memset(mem->vstart, 0, bytes); } =20 int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verb= ose) @@ -352,7 +357,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->vstart + tlb_addr - mem->start; =20 if (orig_addr =3D=3D INVALID_PHYS_ADDR) return; --=20 2.25.1 From nobody Fri May 3 09:54:23 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=1625673125; cv=none; d=zohomail.com; s=zohoarc; b=RD3kOptu+5xCX2oWLhY6K4cB6cmh51kK5XTbdwSyvQcB1BjtyjfWVZ3Pn4oNprtTUWeCal+aCHO2F8Bb8sh9kjWrKgmpM6lLHG2aoAfTFVqU7gBpYT+9+OIKY+P4wDTvL00HpHIvNzJ3bHq3LoS5cyOuhbRfmwbniOVHkgTuEPE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1625673125; 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=uSViD25AWGJ6J+QgAwUoKBubGeqJPy/dkYcV0Fzox+c=; b=Gje2WluO9RXSgtldVyJZpdo221BOpQcLHt99zJy8VHR6WH/vR6t8S/1TIa4GZQTveWGWuA41kDLAfhAUzi4X/Qr5Gpf+GS/g0CjCb6oxNOUTCLMCeIU+5TVJ05Vr32qsqbaJJNfv7Kcs3DHKbA+h2bkVPq9OnJB5r5DdUXyuqJc= 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 1625673125527511.51590147398394; Wed, 7 Jul 2021 08:52:05 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.152549.281850 (Exim 4.92) (envelope-from ) id 1m19q4-0002DA-LO; Wed, 07 Jul 2021 15:51:52 +0000 Received: by outflank-mailman (output) from mailman id 152549.281850; Wed, 07 Jul 2021 15:51: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 1m19q4-0002Cv-Gd; Wed, 07 Jul 2021 15:51:52 +0000 Received: by outflank-mailman (input) for mailman id 152549; Wed, 07 Jul 2021 15:51:52 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19mE-0002ed-6i for xen-devel@lists.xenproject.org; Wed, 07 Jul 2021 15:47:54 +0000 Received: from mail-pg1-x52b.google.com (unknown [2607:f8b0:4864:20::52b]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 27ad855d-4f2c-4584-86e4-bc6fb9fc7caf; Wed, 07 Jul 2021 15:47:08 +0000 (UTC) Received: by mail-pg1-x52b.google.com with SMTP id a2so2661703pgi.6 for ; Wed, 07 Jul 2021 08:47:08 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:38:6b47:cf3e:bbf2:d229]) by smtp.gmail.com with ESMTPSA id q18sm23093560pgj.8.2021.07.07.08.47.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 08:47: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: 27ad855d-4f2c-4584-86e4-bc6fb9fc7caf 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=uSViD25AWGJ6J+QgAwUoKBubGeqJPy/dkYcV0Fzox+c=; b=cQcYT5XSBLbRJRDUNa0e5CgSigjoy2I5REnIiUUUqfnwYfNGPXXLVSum+8IzUodfaP NTItaqDrZixn5v3ga9DqqV+CMjZrPq+hOq0qzW+JzdevrlfXgqEqK2Ehmyky0HMcgFP3 fBUAJnZALGSD2lwUXPo1oB7+tjhYoonkMW4pDlMoiGMuEP1yUjVan+9lRbSkG9DNhWQV dWxY/vIo+6ak/OXzqcOGKv7Ch8+RaNsC9N5387m4YR1JdwzUEvcBUBdz3axjyvNLlHdK q4JRQ6k96Mk9DN+/RNIoDW1vtP7bBHzLBDQcvP33UQJ6r+BeKTe/eU2ca43MQb/lQL6x TM5g== 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=uSViD25AWGJ6J+QgAwUoKBubGeqJPy/dkYcV0Fzox+c=; b=aCfMtyczx32Kmq8JNTTYc6lKhGX5RajkwHe4H+V+1UNUUrmzqM0W5cAv3yOr1sHNoo 12EUjL5gjDK+ZteZv4SrxiLfoUs2HLkmGnsDsnLFNLvkLBB1aHQ/5CVmh0cL2UaeYlBa cIEgwibFTpXfSQAPoIbXSnK//9SYeue9xnwWJgaIeIwavSaSO5I1Crktwx7atihM59Gb PB8/2RiTCzhrPTtn6fIdXqxrWmUzcCio73ImPbJHccZwYXqo29ehhf+MG1CfBHFvJiZu RLhLCQz5mbW2VTLUysVJiISzzpv22I/MXmdGo7ib1xWMgoiQWfz8LDFlBrkOifqofd4o RukQ== X-Gm-Message-State: AOAM533mpPvrB+Mz6HYDziR+GfA+o0DHmTQI5f3k4b9M1wqZU6AZ2g3b Bu+QOcyvhFEnPZ+zCy7odqY= X-Google-Smtp-Source: ABdhPJy7eYx9Hj+tY38P1ebQQq0y3i61YfxbRUD8cqpY0wJNgpDT6hIlSZDAU9aZE7BllJmWIxymNA== X-Received: by 2002:a63:ed12:: with SMTP id d18mr26142030pgi.12.1625672827391; Wed, 07 Jul 2021 08:47:07 -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, kirill.shutemov@linux.intel.com, akpm@linux-foundation.org, rppt@kernel.org, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, ardb@kernel.org, robh@kernel.org, nramas@linux.microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, david@redhat.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, keescook@chromium.org, rientjes@google.com, hannes@cmpxchg.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, brijesh.singh@amd.com, anparri@microsoft.com Subject: [Resend RFC PATCH V4 10/13] HV/IOMMU: Enable swiotlb bounce buffer for Isolation VM Date: Wed, 7 Jul 2021 11:46:24 -0400 Message-Id: <20210707154629.3977369-11-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210707154629.3977369-1-ltykernel@gmail.com> References: <20210707154629.3977369-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1625673125988100001 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 set_memory_decrypted_map() to mark bounce buffer visible to host and map it in extra address space. 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/xen/pci-swiotlb-xen.c | 3 +- drivers/hv/vmbus_drv.c | 3 ++ drivers/iommu/hyperv-iommu.c | 62 ++++++++++++++++++++++++++++++++++ include/linux/hyperv.h | 1 + 4 files changed, 68 insertions(+), 1 deletion(-) 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 92cb3f7d21d9..5e3bb76d4dee 100644 --- a/drivers/hv/vmbus_drv.c +++ b/drivers/hv/vmbus_drv.c @@ -23,6 +23,7 @@ #include #include =20 +#include #include #include #include @@ -2080,6 +2081,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 */ @@ -2120,6 +2122,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..d7ea8e05b991 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,56 @@ 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; + void *vstart; + + /* + * Allocate Hyper-V swiotlb bounce buffer at early place + * to reserve large contiguous memory. + */ + hyperv_io_tlb_size =3D 200 * 1024 * 1024; + hyperv_io_tlb_start =3D memblock_alloc_low(PAGE_ALIGN(hyperv_io_tlb_size), + HV_HYP_PAGE_SIZE); + + if (!hyperv_io_tlb_start) { + pr_warn("Fail to allocate Hyper-V swiotlb buffer.\n"); + return; + } +} + +int __init hyperv_swiotlb_detect(void) +{ + if (hypervisor_is_type(X86_HYPER_MS_HYPERV) + && hv_is_isolation_supported()) { + /* + * Enable swiotlb force mode in Isolation VM to + * use swiotlb bounce buffer for dma transaction. + */ + swiotlb_force =3D SWIOTLB_FORCE; + return 1; + } + + return 0; +} + +void __init hyperv_iommu_swiotlb_later_init(void) +{ + void *hyperv_io_tlb_remap; + 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 Fri May 3 09:54:23 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=1625673137; cv=none; d=zohomail.com; s=zohoarc; b=dUkG7XQbkQ6CQInNED0L0zOGgx9UCj/WvPn4ucyiGKlTsG9L2wG0zOpinNTDtl8wOqNY7VA9gIB1tJRC/SEXisNQkLMj3yACrptE139ou0K3NeLO/37CL8Fk9k/WrcePqHbTywmnkUL3Wqbd9ttjYiACFPmJllZNTUl00Jlr5XA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1625673137; 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=d9G7rX1Wf2e4jgYzMUXcCiOPT4rIMtwlWsRa/j4ZTx8=; b=aZWpHow9A4Y2jP3Mi0NsItzbt25QEyDWNUCwm7Itp7M0CZY6FEeDVsqyXPI5ywccIs0KRwUCvIPUzxRYoJl5G0LyP/22k3WZvQzZBWJ8ST3kge8GS4AOgVdhaKXz7ndlYgfbdT2V/QMsOgdrXX2E62TnUAfjMKnWAAExlns3H3g= 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 1625673137068506.34964612750855; Wed, 7 Jul 2021 08:52:17 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.152555.281861 (Exim 4.92) (envelope-from ) id 1m19qF-0003AL-5x; Wed, 07 Jul 2021 15:52:03 +0000 Received: by outflank-mailman (output) from mailman id 152555.281861; Wed, 07 Jul 2021 15:52:03 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19qF-00039z-1F; Wed, 07 Jul 2021 15:52:03 +0000 Received: by outflank-mailman (input) for mailman id 152555; Wed, 07 Jul 2021 15:52:01 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19mJ-0002ed-6s for xen-devel@lists.xenproject.org; Wed, 07 Jul 2021 15:47:59 +0000 Received: from mail-pf1-x429.google.com (unknown [2607:f8b0:4864:20::429]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 58f9da59-0e9c-4f53-8c76-da520f0ea693; Wed, 07 Jul 2021 15:47:11 +0000 (UTC) Received: by mail-pf1-x429.google.com with SMTP id b12so2541184pfv.6 for ; Wed, 07 Jul 2021 08:47:11 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:38:6b47:cf3e:bbf2:d229]) by smtp.gmail.com with ESMTPSA id q18sm23093560pgj.8.2021.07.07.08.47.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 08:47:10 -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: 58f9da59-0e9c-4f53-8c76-da520f0ea693 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=d9G7rX1Wf2e4jgYzMUXcCiOPT4rIMtwlWsRa/j4ZTx8=; b=MBccprpsRZbRBzn6Q74+KmpWH/C6n7RGUH8aA689Jf6+OWOMykcnt/ZY37atWT4gic DOjRNhV5KQvMsBL9VxsrKxt3IkxH7xLFDJNZiPni9y6shi6ugb60m7cU9VeXX64Y81LP xgPcv35SphN43aCrwRWGJr9XUKndx9QxVUhL/4TdO18lYNSAEJluabGylM3ACDVDtJ/h sBUs2A7aqcwl2SUBmvmD4p7RBT0gOmbRB7ShQ8nx7G0M5dGHQbYtntiAQ27Cy6+XC6wa f+tRGI3zFgJhUOaZEHGkNaMdOsoeZm+p5CYOC1oMvnExhumjI6l4IEtWCcA/ChkzHZO4 5tCQ== 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=d9G7rX1Wf2e4jgYzMUXcCiOPT4rIMtwlWsRa/j4ZTx8=; b=A8TPfNDrWOTGn2HkaBQq2x6wT3q8KzAMdleh3wfDJajbzUMMGWBI+RqtAhm3s1Ml9z jYGvj0oZqmCDbV7P5Qe7SdZXWl7W6u9XLTreSrQRzmhVg54ZqnMhZGnSmqJnwcxX9ipt Vxp2mkuSLPzYjvV7cYZNEulg8YE9o0s1KCqDFKqQ8yM46U855hFmDq2MC8lfhbBWiKs5 vAOGt7Xqf2p3Z3VE05ZdefyKeARV2Q1Wns+WQCl5mtJsb5JTHAzQJmZrwHH2Uk4mJkj+ KWOQ+LUPbWxHyiucG6IS5pLBZmLSOGg037i07XpWLb3whnapft2WeXpCZRfPs6nNcCi1 Z5iQ== X-Gm-Message-State: AOAM530KhN2obSP52bZUqPPWO+gn3HOmwutnWKeaqm04OJaDAlCWUo8P fS0zY2SErwtuv6bFsAhjYjE= X-Google-Smtp-Source: ABdhPJzvlBIKPtUWe4+tZyz+A3EwlEGnKYo9dj5XU3MYJvkGWWUZOwWT2GALfxenXK+wdhiIBd383w== X-Received: by 2002:a63:d915:: with SMTP id r21mr26953592pgg.283.1625672830421; Wed, 07 Jul 2021 08:47:10 -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, kirill.shutemov@linux.intel.com, akpm@linux-foundation.org, rppt@kernel.org, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, ardb@kernel.org, robh@kernel.org, nramas@linux.microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, david@redhat.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, keescook@chromium.org, rientjes@google.com, hannes@cmpxchg.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, brijesh.singh@amd.com, anparri@microsoft.com Subject: [Resend RFC PATCH V4 11/13] HV/Netvsc: Add Isolation VM support for netvsc driver Date: Wed, 7 Jul 2021 11:46:25 -0400 Message-Id: <20210707154629.3977369-12-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210707154629.3977369-1-ltykernel@gmail.com> References: <20210707154629.3977369-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1625673139656100001 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 b11aa68b44ec..c2fbb9d4df2c 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; @@ -1729,4 +1733,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 983bf362466a..9425fee85aa0 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 Fri May 3 09:54:23 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=1625673120; cv=none; d=zohomail.com; s=zohoarc; b=JfSPtxLtVgSloG+DrOoamWarfHloiOOZF9meLv+Rp3VkRrqYS9Iqa0KZLqt2LXHpH+4PKBUnzAVhEvZwgGSCpjvuGWXA7hFw9wYLOV5KtzGMDAAJ7cbdL2F48H0zlLcADbMxl98ndHPihJWy6nbOM5n0Wd1GHVwBCLWTjYGwy2Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1625673120; 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=HlyScOtQVoYnoHAZopBay4tlsKScaz3uD66IFgkGB/I=; b=kBuVbr/ElkvVnSeB+pvmX+iCKAtA2RXB8k7BjsgIJFRX99WZtMWcK6/bd1Ns8bVbER9ej2AOdWCMvDkhAuy+l4vJKAIirj+mbQwQZW3gZYArp+ASUgnUtuhDNBXVzMiRBgFO5O+bw7Tn364wDPyUJmH8Z0II5X+KRVxuI0zBgHg= 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 1625673120945182.65736944073876; Wed, 7 Jul 2021 08:52:00 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.152539.281809 (Exim 4.92) (envelope-from ) id 1m19ps-0000xw-Gy; Wed, 07 Jul 2021 15:51:40 +0000 Received: by outflank-mailman (output) from mailman id 152539.281809; Wed, 07 Jul 2021 15:51:40 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19ps-0000x5-DE; Wed, 07 Jul 2021 15:51:40 +0000 Received: by outflank-mailman (input) for mailman id 152539; Wed, 07 Jul 2021 15:51:38 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19mT-0002ed-6y for xen-devel@lists.xenproject.org; Wed, 07 Jul 2021 15:48:09 +0000 Received: from mail-pj1-x1031.google.com (unknown [2607:f8b0:4864:20::1031]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id ca43c959-8a80-44c2-a26d-38b2ca2f8494; Wed, 07 Jul 2021 15:47:13 +0000 (UTC) Received: by mail-pj1-x1031.google.com with SMTP id b8-20020a17090a4888b02901725eedd346so1909825pjh.4 for ; Wed, 07 Jul 2021 08:47:13 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:38:6b47:cf3e:bbf2:d229]) by smtp.gmail.com with ESMTPSA id q18sm23093560pgj.8.2021.07.07.08.47.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 08:47:12 -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: ca43c959-8a80-44c2-a26d-38b2ca2f8494 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=HlyScOtQVoYnoHAZopBay4tlsKScaz3uD66IFgkGB/I=; b=gnz1C+yn2F6jk2O7gk2x9gxUHURZsfF5XLUJ9QUqZxW9QQ8vYC3vvegOxXO9R4DFYN 9aEKSS0P/WP8FXlRkhBlICHCsZgnUNkAuXj8nx6L5YTV+kaDmrwF8i4/jBsHqeF8WqH7 uFGPQwROQWFNjwC6rT/V+bBbNRJ2h7/YJRFCtUaL3fJS2vdOnzLy6vNSOHFb5ZFLk3r/ hrKiKbbfpfSkPOvJuQ53To6i9/z5+AfF1kbKl+tWygqCe8kJqljt+E/DlC7f9zYoimIN 99hqd8fWGEIOble3BEp3B9fbFL/yoNtxRUiHgHZHOu1QD9tOlArcAoHYdogTxetcVgku eGIA== 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=HlyScOtQVoYnoHAZopBay4tlsKScaz3uD66IFgkGB/I=; b=Wcx/2ypl9wXKp5JR+qZXaWF+QXt4UePVULaonGcit7OZMCsNnmwcty/L7x1m8qQGYG aEtj78pOQpPbSkVTgZgYWPpNrvZ5IIOzc2LJsch5YxPzEOhfo62EZVgTYknEyBDhOcGh yBpr/R/U+HWAIyV9nHCtSGynrZcykZ6MNazgG2YfdSYMcCUrJa4JAnhINDptW+dXGkLw H1UtcJtEk+LyicP3He/YEk+c/PgnhA0wnAHdn3pXKNuZWgKZVhdhDH42yw/aoH5hrzMI rPGUFnjMQibU6upv0N1o4BdOqnoLkILZASv2zqjTI3EwQCPkf1I+aHkoIGk4DnJCFxKG z7Pw== X-Gm-Message-State: AOAM5324GiAYRCVsCqqsBhTx5LmFbLwsngbPEdY7K9eFCIZcXvkYCpXY qzMzvaljDQMEjKVYgsH6yG4= X-Google-Smtp-Source: ABdhPJyKzXN9u1DVm1W6X3qXZ/nh6euE9GS3yVqvO9By160QCA1G6fa3xLr2paYIAzHzvblMoOF23g== X-Received: by 2002:a17:90a:3c8d:: with SMTP id g13mr198396pjc.229.1625672833174; Wed, 07 Jul 2021 08:47:13 -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, kirill.shutemov@linux.intel.com, akpm@linux-foundation.org, rppt@kernel.org, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, ardb@kernel.org, robh@kernel.org, nramas@linux.microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, david@redhat.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, keescook@chromium.org, rientjes@google.com, hannes@cmpxchg.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, brijesh.singh@amd.com, anparri@microsoft.com Subject: [Resend RFC PATCH V4 12/13] HV/Storvsc: Add Isolation VM support for storvsc driver Date: Wed, 7 Jul 2021 11:46:26 -0400 Message-Id: <20210707154629.3977369-13-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210707154629.3977369-1-ltykernel@gmail.com> References: <20210707154629.3977369-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1625673121573100002 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 403753929320..cc9cb32f6621 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; @@ -1267,6 +1279,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; @@ -1321,6 +1334,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; } @@ -1817,7 +1839,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 @@ -1831,6 +1855,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)) { /* @@ -1854,9 +1885,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; @@ -1867,13 +1918,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 From nobody Fri May 3 09:54:23 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=1625673116; cv=none; d=zohomail.com; s=zohoarc; b=Z5tr3cnOj8fcmeidCen9KY1HhgVRjQe4NJo/56QzpIiLicPo6+uye7Di/ipADfTd/JA0yq4eoj9u+ZU9toroyGJ2Xsw87rHrRrOsjM1MFNs/nRnSm7uJ5Qz5YjfWx/kEvciHBhxUDhWMUzUE4ALVguc+R1xn7LEEBlUyOnXK3+o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1625673116; 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=84IEbLNgJNomBqMgxBtrETy72H72EvXAGdQ00eQF2Mc=; b=SWN0S+CoHgD23AiRya+ij5E5LoRp11clo/Nm94utA0h7oozYz/RTa9OdXz175pgZnRKlo+Rd8BlJHBUMEPIHF6TkADWzfAkPWaA5Mtwf1ftbIppSIbzFmRHMZxPyFrJxSFLgWG+KdrVtAh+XJ9oxxJQ4XKPSSK3Srk1OKp0UxXc= 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 1625673116875180.31829346935854; Wed, 7 Jul 2021 08:51:56 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.152537.281806 (Exim 4.92) (envelope-from ) id 1m19ps-0000uW-82; Wed, 07 Jul 2021 15:51:40 +0000 Received: by outflank-mailman (output) from mailman id 152537.281806; Wed, 07 Jul 2021 15:51:40 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19ps-0000uP-4d; Wed, 07 Jul 2021 15:51:40 +0000 Received: by outflank-mailman (input) for mailman id 152537; Wed, 07 Jul 2021 15:51:38 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19md-0002ed-7K for xen-devel@lists.xenproject.org; Wed, 07 Jul 2021 15:48:19 +0000 Received: from mail-pg1-x530.google.com (unknown [2607:f8b0:4864:20::530]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id ebec7393-d1fd-4e30-85c3-7850c9255489; Wed, 07 Jul 2021 15:47:16 +0000 (UTC) Received: by mail-pg1-x530.google.com with SMTP id v7so2683332pgl.2 for ; Wed, 07 Jul 2021 08:47:16 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:38:6b47:cf3e:bbf2:d229]) by smtp.gmail.com with ESMTPSA id q18sm23093560pgj.8.2021.07.07.08.47.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 08:47:15 -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: ebec7393-d1fd-4e30-85c3-7850c9255489 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=84IEbLNgJNomBqMgxBtrETy72H72EvXAGdQ00eQF2Mc=; b=YNnohj4Chb9T3B9ZYxiBv9YVqoi0X5lB74SQ7L1EnVYnuQ7j+Sylm+oTEpclLAr4Rp QMdcSRfsEpsNu8h57gAfWgW+S7FmRK+lRz16mOIVQB9v19q7vKIS/3imFU7L8I+a+CoW 4CZ1ji2QtUkrwbieeV7oza4eciyEFAW2B4m/iegyGFZxo9emMHECDfkYKuchj9BUm6ka +lt0qfR+AlSO/jWbQhKgOe0W6mJ6vX8d+oS1VyY2+/m2yXPG+YkyAhEMSZLvHoK3AYA2 aynhmc1FnAYYAolwJfs0NpD2mwngTXdQQx4mA0KgjhyurE1FSWmq2vOFbfaVKW7EnbvH tEwg== 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=84IEbLNgJNomBqMgxBtrETy72H72EvXAGdQ00eQF2Mc=; b=JmB4NLzV12Grm7dtD1LoKqIrvzNhKP5h7d55VZeKQv9o2+M8fTNwAaEn+m6j9tVJEk yDzcGTjNcPMnooHXekaHl9F33auIzsC37ydnP2GkgEmci4LLQg4e6DFu1HiygfmXTdY2 CznTKX6s+cE4a6Wq37fgUx10/27XqwrLY2nElMl5C+CJb+XWir667vbwcNewZoYI+d0U lO31rA36NusXaqSv2GuIX4V1tTnrtURA2mPnO0RcSopTcwyPo6OLXDtd5zxkheDa7yh6 2GsGbckneV5D1HBwEd/zNNtzT93N/4bKvkLFld1WEj3Xys1DYCGPg5UCj8r2KDMlaLtD QAWg== X-Gm-Message-State: AOAM532wMordziOSdpj/qrFm2KKJ7jsHUIZRECQIWnnWqgjua1HhFqgj wQNyaut/bd7v3HdcnC9aF2Y= X-Google-Smtp-Source: ABdhPJwc0xo238tlV5y9FHemJmnwVdI1QKXmzxerVDS/ZE3a5qm/V6a2aCdYF88RGalmD6tecULCLg== X-Received: by 2002:a63:1601:: with SMTP id w1mr26556927pgl.116.1625672836292; Wed, 07 Jul 2021 08:47: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, kirill.shutemov@linux.intel.com, akpm@linux-foundation.org, rppt@kernel.org, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, ardb@kernel.org, robh@kernel.org, nramas@linux.microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, david@redhat.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, keescook@chromium.org, rientjes@google.com, hannes@cmpxchg.org, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, brijesh.singh@amd.com, anparri@microsoft.com Subject: [Resend RFC PATCH V4 13/13] x86/HV: Not set memory decrypted/encrypted during kexec alloc/free page in IVM Date: Wed, 7 Jul 2021 11:46:27 -0400 Message-Id: <20210707154629.3977369-14-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210707154629.3977369-1-ltykernel@gmail.com> References: <20210707154629.3977369-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1625673118794100001 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan Hyper-V Isolation VM reuses set_memory_decrypted/encrypted function and not needs to decrypted/encrypted in arch_kexec_post_alloc(pre_free) _pages just likes AMD SEV VM. So skip them. Signed-off-by: Tianyu Lan --- arch/x86/kernel/machine_kexec_64.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_k= exec_64.c index c078b0d3ab0e..0cadc64b6873 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -26,6 +26,7 @@ #include #include #include +#include =20 #ifdef CONFIG_ACPI /* @@ -598,7 +599,7 @@ void arch_kexec_unprotect_crashkres(void) */ int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp) { - if (sev_active()) + if (sev_active() || hv_is_isolation_supported()) return 0; =20 /* @@ -611,7 +612,7 @@ int arch_kexec_post_alloc_pages(void *vaddr, unsigned i= nt pages, gfp_t gfp) =20 void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) { - if (sev_active()) + if (sev_active() || hv_is_isolation_supported()) return; =20 /* --=20 2.25.1