From patchwork Thu Aug 10 16:04:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tianyu Lan X-Patchwork-Id: 134099 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp542785vqi; Thu, 10 Aug 2023 09:37:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEkD4EjQnQR5nv+llwlHybMPm99D0gGr2hERJCCfYouIZVbv9AcGMyW5EH3kiiG5YdO8yDs X-Received: by 2002:a17:902:ec8d:b0:1bc:4f04:17f9 with SMTP id x13-20020a170902ec8d00b001bc4f0417f9mr2515626plg.9.1691685447695; Thu, 10 Aug 2023 09:37:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691685447; cv=none; d=google.com; s=arc-20160816; b=Jy6us1qoFtVWzuD9EFVk9lMF2yS0O9QZ7w1X0LfcsulBenAi584vc5atjBhUkgZ76T ZIaOmI9JzANMD/fBBkLEK20jlDJxiFaWNLC3l7W8o92M8mrGEpWpLT96l7ZkG2yhOHPW tSHdbzhxyi21FXDxvyoIiFzngoX92a9Zc4GsPfF8rCXo0v6drI9xz08Nm+LAThAln62C FDW/rWGXvobA2fuvUyv4T0Wqkw1RPCv9PmhbwCPixaQqDhSz8omF6Na5HipgUgjAmF51 mpIFot8MraG7c+4ZcfMsBJkko3Kmfam+hvkWZboDsUuDyAW3Yu6pn3ULH3TQuTfcXuLr d0cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0mqb14LBzHQf18FY7xszQiIf830qOZYbv6/JECgr54c=; fh=TxLVtSsj0Yk+bstPG6ly04YaPbvjOFEmGNSQ8sZkNQc=; b=RgS4PsWSQDySfQNBvoC06oDr18ChZk+UiGCHeuo5wEqaHwSBYW2tIUIhzdFSJ9j4KV RTBX4ayQJ0LdCEafltfOmKsK768aViYaIA/v29Hy8Vi4RoSDTOzUAr/I7MQg7kbITlJN qCuEYhY25w/N0DL301ZNbBTi6bMA/CANFHlJBNrf/u77KYS/X2rMqZXciYXQ/PP+dNT/ 5gowXzoT05VdtHwz4Dj5AZeSWMt9Ou2hiEyVDHx+O2MMCiigofEykAYS8Nl7MYAX6hmP r+2pd2O4LcohzxW3JYBLZL4xvwQ/G28Qz9f++klon7ZLKXJn+OpMtpHzz+k6PYld11LS z1AA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b="ZNHK/PMc"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ij30-20020a170902ab5e00b001b9e21bc14dsi1641318plb.652.2023.08.10.09.37.06; Thu, 10 Aug 2023 09:37:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b="ZNHK/PMc"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236663AbjHJQGP (ORCPT + 99 others); Thu, 10 Aug 2023 12:06:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236614AbjHJQFV (ORCPT ); Thu, 10 Aug 2023 12:05:21 -0400 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44CA52D4F; Thu, 10 Aug 2023 09:04:28 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1bcad794ad4so8394075ad.3; Thu, 10 Aug 2023 09:04:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691683467; x=1692288267; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0mqb14LBzHQf18FY7xszQiIf830qOZYbv6/JECgr54c=; b=ZNHK/PMcRaN1J6Uwf4XvlEjTLZCToZf9+yZtIIKSo2ubf0ljKFrx4TdG7NOe2sYyeC jz0rw/20j1wNn6bldu7tUiSDG9FUZEsB+X5CzBM/h9nbaMORoxB7m7mjTW6HZBlQ1067 BgfRCPq7aWZyRej58WS+zzrakA7vsarfhWebuMQXw0CiLZpNSawGbE+jUlCitD9jkRby kNTjkOIEIkz0Yr2MI0L+vXOdQ2/et/UnqT8/OhDy414bX9KC7+Ndf4V/KSttvI4plofV VDUFc0Vl9CMlU374RSqMIAzgW5XBHHcoeCYApzABGsexevW/8CYgxEyE/OnwF35xU0zq cMSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691683467; x=1692288267; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0mqb14LBzHQf18FY7xszQiIf830qOZYbv6/JECgr54c=; b=b2KO+1iIdagmWjBizj2MkYJV7z/ei53Zjk/5RShhGIVdv00W9IxCP2xtKlEPrkauLz ZxkGxIvoU5W0VsjOcA6S2bRvyGeLcw1MaCCluR0HA3gjzUqz0lyUEBMr8mHqjWFYHm4D AxQOeGezf1XXWMMSCxxKOsstGkIvL6lodfoMKOAyiJ0DLF9mhQ0NVwfy/1ohyoNXoVp0 YwLKt5a1KZkRy4lEm7ybJI3vGVsBS6ovCGSQvOY25gUncDCpMOmdGWghTKNoLDbh+KAu YNwwkZq7mqYNhag4LMuc7haExwUoHcAKSzKgFRA0Pg5Uu2sj7xoibclnqprXAjHEgU5V Fojg== X-Gm-Message-State: AOJu0YymlRpIOpnmgbnR1cd1pMj1mm6bI0KDwO9iWMcTg5OxzC1AHEWC t4M2+rUfR1gnOiW+LuG2Nh4= X-Received: by 2002:a17:902:9009:b0:1bb:d280:5e0b with SMTP id a9-20020a170902900900b001bbd2805e0bmr2062451plp.18.1691683467253; Thu, 10 Aug 2023 09:04:27 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:0:c620:2003:6c97:8057]) by smtp.gmail.com with ESMTPSA id r4-20020a1709028bc400b001b895a17429sm1948821plo.280.2023.08.10.09.04.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Aug 2023 09:04:26 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, daniel.lezcano@linaro.org, arnd@arndb.de, michael.h.kelley@microsoft.com Cc: Tianyu Lan , linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, vkuznets@redhat.com, Michael Kelley Subject: [PATCH V5 4/8] drivers: hv: Mark percpu hvcall input arg page unencrypted in SEV-SNP enlightened guest Date: Thu, 10 Aug 2023 12:04:07 -0400 Message-Id: <20230810160412.820246-5-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230810160412.820246-1-ltykernel@gmail.com> References: <20230810160412.820246-1-ltykernel@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773860760498208245 X-GMAIL-MSGID: 1773860760498208245 From: Tianyu Lan Hypervisor needs to access input arg, VMBus synic event and message pages. Mark these pages unencrypted in the SEV-SNP guest and free them only if they have been marked encrypted successfully. Reviewed-by: Michael Kelley Signed-off-by: Tianyu Lan Reviewed-by: Dexuan Cui --- drivers/hv/hv.c | 57 +++++++++++++++++++++++++++++++++++++++--- drivers/hv/hv_common.c | 13 ++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/drivers/hv/hv.c b/drivers/hv/hv.c index de6708dbe0df..ec6e35a0d9bf 100644 --- a/drivers/hv/hv.c +++ b/drivers/hv/hv.c @@ -20,6 +20,7 @@ #include #include #include +#include #include "hyperv_vmbus.h" /* The one and only */ @@ -78,7 +79,7 @@ int hv_post_message(union hv_connection_id connection_id, int hv_synic_alloc(void) { - int cpu; + int cpu, ret = -ENOMEM; struct hv_per_cpu_context *hv_cpu; /* @@ -123,26 +124,76 @@ int hv_synic_alloc(void) goto err; } } + + if (hv_isolation_type_en_snp()) { + ret = set_memory_decrypted((unsigned long) + hv_cpu->synic_message_page, 1); + if (ret) { + pr_err("Failed to decrypt SYNIC msg page: %d\n", ret); + hv_cpu->synic_message_page = NULL; + + /* + * Free the event page here so that hv_synic_free() + * won't later try to re-encrypt it. + */ + free_page((unsigned long)hv_cpu->synic_event_page); + hv_cpu->synic_event_page = NULL; + goto err; + } + + ret = set_memory_decrypted((unsigned long) + hv_cpu->synic_event_page, 1); + if (ret) { + pr_err("Failed to decrypt SYNIC event page: %d\n", ret); + hv_cpu->synic_event_page = NULL; + goto err; + } + + memset(hv_cpu->synic_message_page, 0, PAGE_SIZE); + memset(hv_cpu->synic_event_page, 0, PAGE_SIZE); + } } return 0; + err: /* * Any memory allocations that succeeded will be freed when * the caller cleans up by calling hv_synic_free() */ - return -ENOMEM; + return ret; } void hv_synic_free(void) { - int cpu; + int cpu, ret; for_each_present_cpu(cpu) { struct hv_per_cpu_context *hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu); + /* It's better to leak the page if the encryption fails. */ + if (hv_isolation_type_en_snp()) { + if (hv_cpu->synic_message_page) { + ret = set_memory_encrypted((unsigned long) + hv_cpu->synic_message_page, 1); + if (ret) { + pr_err("Failed to encrypt SYNIC msg page: %d\n", ret); + hv_cpu->synic_message_page = NULL; + } + } + + if (hv_cpu->synic_event_page) { + ret = set_memory_encrypted((unsigned long) + hv_cpu->synic_event_page, 1); + if (ret) { + pr_err("Failed to encrypt SYNIC event page: %d\n", ret); + hv_cpu->synic_event_page = NULL; + } + } + } + free_page((unsigned long)hv_cpu->synic_event_page); free_page((unsigned long)hv_cpu->synic_message_page); } diff --git a/drivers/hv/hv_common.c b/drivers/hv/hv_common.c index 4b4aa53c34c2..2d43ba2bc925 100644 --- a/drivers/hv/hv_common.c +++ b/drivers/hv/hv_common.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -359,6 +360,7 @@ int hv_common_cpu_init(unsigned int cpu) u64 msr_vp_index; gfp_t flags; int pgcount = hv_root_partition ? 2 : 1; + int ret; /* hv_cpu_init() can be called with IRQs disabled from hv_resume() */ flags = irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL; @@ -378,6 +380,17 @@ int hv_common_cpu_init(unsigned int cpu) outputarg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg); *outputarg = (char *)(*inputarg) + HV_HYP_PAGE_SIZE; } + + if (hv_isolation_type_en_snp()) { + ret = set_memory_decrypted((unsigned long)*inputarg, pgcount); + if (ret) { + kfree(*inputarg); + *inputarg = NULL; + return ret; + } + + memset(*inputarg, 0x00, pgcount * PAGE_SIZE); + } } msr_vp_index = hv_get_register(HV_REGISTER_VP_INDEX);