From patchwork Tue Jul 18 03:22:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tianyu Lan X-Patchwork-Id: 121741 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp1481375vqt; Mon, 17 Jul 2023 20:25:56 -0700 (PDT) X-Google-Smtp-Source: APBJJlFQG9f8Y41PODHO61sqyqz8m2RDPp01c9AJNLAc8/QlLdtrCrHdxVstqG0vlpCRUCeew/H4 X-Received: by 2002:a17:902:d2c5:b0:1b6:92f0:b6f5 with SMTP id n5-20020a170902d2c500b001b692f0b6f5mr13720208plc.14.1689650756588; Mon, 17 Jul 2023 20:25:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689650756; cv=none; d=google.com; s=arc-20160816; b=D+CWVyAFA+n4FA3Ras9qrxEq5TFrvyVzmQQNjs0n9dVjEo+h7Dr0IUBAISAU6Jqhz3 1KnNTe0t9JHC18D+MMTngH8NLOTiNYmWJWfcWktkKCn7KKHWrCJbe0Eh+R6VT7Nj7U90 Pig/PSoQlHUyLU/HzqRUWZTLlRe5rQFpkuI9aUPqzP4DwCT+1EGAXSKRG4C5u2qo2xgg RFwFzdg9PpWw5tYQRiOk9KVrteeHnrSu+16FwdN0bzyWqpfxPeKIkzc8bfxsBr7YpfYn pHs1ziRVcM3dNS0mK6ZMRWDR2S5YAYOoAxN3UJbYiLSxl86b2Rar3jLQ0o22uPkYBcGf hozQ== 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=ngkAN38qL0iVs+iJhqNGyyICCfDmYg6nB1eodbZjSK9aTxZMoR/pmgtE7UxGgW52no eHZX/c5KwQmIxOhBdn3BsBMHwp1TVngcuKmSqQ/XM5YQWQA9n5LsXkNuEXTaZIYFe4ct sHjYGF3oS7YPbLCRViM7DcPbY0t3M6ujrkjygYosdNyejckrHDGJLnDSB88c9c21t18+ AMWiIACXWciIuIshXGdCCuW4cSrMfA9QGdUFVSZiAIpbEP4i1TnSu4fv+2jsDFs1gyXs 1H4z73zgHZpDOXy+m5qR7lCqgzmDfIARZ3VvL2niG/xQhabydEolpheC1co3XIVhOK5E zf9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=nvzVeuEu; 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 i9-20020a655b89000000b0053fee209655si809326pgr.664.2023.07.17.20.25.43; Mon, 17 Jul 2023 20:25:56 -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=nvzVeuEu; 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 S229669AbjGRDXn (ORCPT + 99 others); Mon, 17 Jul 2023 23:23:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230447AbjGRDXR (ORCPT ); Mon, 17 Jul 2023 23:23:17 -0400 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B55410E2; Mon, 17 Jul 2023 20:23:16 -0700 (PDT) Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-55ba5bb0bf3so3199574a12.1; Mon, 17 Jul 2023 20:23:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689650596; x=1692242596; 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=nvzVeuEupIKD66g2NAcLB3TMdCqMRB2Q+kPHig08uUKxEqN5w9Q+0sNRjhLdcnFr4V gVkQs39UCHfUKOxXm6o3q/pR0b2X8qYmrKV8PQosnaRLcH5f0DUZDsT9Fj5tOHxn2w6S yY1jHnsaYzQc4WbuTcwRrQHn9l4ys/Tvil7CwacRWEf4zX91dqXhmeq/inJADEnzxlWm UIV1BZ0fyDcHCnrfieHm85HcDRv5YOUTW4we3xBIIBbE63E7dVngtIAZL5W2QTGNpP3M bw4bH/WsqZPDaeM9tXOt0LbNfHGAsnlvdyn1aqrdflLOjm5SGLwKATfRGyGCOS0C7MTe bAVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689650596; x=1692242596; 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=jNwFkwLYHiIfMSC7gZLQj3Zd2bscBHd5BmLbJcYrncetoEqELKL/Cbiv44KD6LqoPw WagWmU8YHBvNRDqFhNGyFeEA0GD+qLJFira7fD1UugIgRmbtDbpDI/jb05LCPDJhqGs2 HPivDH2Jl67k0v1K+wKDr6LzzPr1TJMIoTt8kcuzWEw7btfzzg5TuCA6KJxnBDQsPUnM M6VoLgVwi7NAYd2NTRPmPERGTBjkB71EfK8VNa4a64w01v8uIVxkvcFseUfICSMXKjGh 0g9AcAEL8NB88cN5+tsoIgzRCqnWNEt7BNJSI7vxa3WxGWe4HejVK9WD2a/23wFWcDiV zluA== X-Gm-Message-State: ABy/qLY7yfkRfZnyUCCoTCMvKz9p7McqgnKw+x6bgawGtG0u47tnDBDw VYXmjZFjtjx9N3TfAxUKV1pnAxum6Rz8cw== X-Received: by 2002:a17:90b:1095:b0:263:3567:f99 with SMTP id gj21-20020a17090b109500b0026335670f99mr14755117pjb.15.1689650595674; Mon, 17 Jul 2023 20:23:15 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:37:c5e9:2003:6c97:8057]) by smtp.gmail.com with ESMTPSA id s92-20020a17090a2f6500b00263f41a655esm504040pjd.43.2023.07.17.20.23.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jul 2023 20:23:15 -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 V3 4/9] drivers: hv: Mark percpu hvcall input arg page unencrypted in SEV-SNP enlightened guest Date: Mon, 17 Jul 2023 23:22:58 -0400 Message-Id: <20230718032304.136888-5-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230718032304.136888-1-ltykernel@gmail.com> References: <20230718032304.136888-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,T_SCC_BODY_TEXT_LINE 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: 1771727231533559376 X-GMAIL-MSGID: 1771727231533559376 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 --- 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);