From patchwork Thu Nov 7 23:24:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dionna Amalie Glaze X-Patchwork-Id: 13867280 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 11DED21A706 for ; Thu, 7 Nov 2024 23:28:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731022132; cv=none; b=CaTjttzKFs/016GYJed5lWqU1h4+M6hbQvkVIeBK+vRPWQmu/461j3g8lXviDvktG1cPDDQHyX4D9GkTqoKC7tERtHdHQg8p6JRdGDcM/5zA4EiSAW18j8yi5OADriq07nY0H6qMItEP1KYxcxL3AaiUVv3Tr18Eu0AtEbXShQA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731022132; c=relaxed/simple; bh=lRBqf5twuEZRlunb4SU7u4RsPEMFdFwbD7rStcI1GVM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VptRA9vBrJsjruACE/Ixe7BFHGR3KY8Womh5W+v44eqiVT/146xHLg3VOfk9ZqrEvEEmrwF3TPlfp9JdOBWRJWpFXvALq5oLH31Uxgc0mmSeQ5STtHY3B+bUzz53VrRNWc0T7Rp3NJklrcKnL0isyQaSbveIQd5ZnYiliPo6Tq4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dionnaglaze.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=W4n13qEW; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--dionnaglaze.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="W4n13qEW" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2e3d74e5962so1602215a91.0 for ; Thu, 07 Nov 2024 15:28:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731022130; x=1731626930; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=R5GIXxcbWN8I/ZRWs7VNYRYcdtYl9gfySVnkJreof1o=; b=W4n13qEWTYMhqiAtXZTIu4lR1YVqxww1mxu7pRQDLV5immugt3hFPFjHFfjnZe82TC UnOjXKsa/0Onv4LCmcQ/l809Py5VastJp24n5VoHc7LXOfFyqBZTc/Ou9wUXsLGtU8c7 WbRt+AX/ZATpyij5HWZsWqFYw8gImUCsH8O+HFMEJKqzfAHac8XheJXNHhTHyjYPaORw UPJ0a8V2/N5eBuycUE3jOklKfLshTRmU5g6W/Fh/dxFY6z3WLSA95TPHp/DCb/K0h0Tx Frbo7vJB5r33Eka8aiVkP1ND4ZAWhjbje5amaOiQ61UtslOc2IgdONeaQDryx730LoUr clEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731022130; x=1731626930; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=R5GIXxcbWN8I/ZRWs7VNYRYcdtYl9gfySVnkJreof1o=; b=GE0wy0NMJ/dRtPPEOjcEUFPU+fMNfaL8KAGTFLD/jbWRl0/tidHs3eKnFL5dFsYetT vSF/Yme4osalRykeR1PRd38Y/lz2+AkvVrdkQWSYQPJruBZy/GWU8i/62WJ9a30PTWbS l0JiwAWpC9AiHu5lT5DOsVBhvz16uerDyj2ri0vm11dYAplJHnFeHOHt6m9TsN64M4Nn 6G2eMqTpohXluDM+9JKaGnYxtsqegUV/xAIRYPaZb63d65AyEBPYPkvJVn6ZKyf2hb61 td5/qbiz3HL0xLG+EU0QF5gRzBH+u4tLj0+tj93Tmhvi47u7pemdgTPYDqhEqac/Qj98 lvSA== X-Forwarded-Encrypted: i=1; AJvYcCWPKeeLRP14mX9bQc0Ns0NqW6W4OBY9O/stOrEWxQXOQA256KJfFWd2916H+J1e5fjSESW5ulNdj4RhaOk=@vger.kernel.org X-Gm-Message-State: AOJu0YwR7cwbF3EMQObyJfELPYXciIBMPKekU3B40GP1EVO32oLv+OCa 9T4oWwl5dZ/cjbxBDEToTK/bVbVFBF8sSlU+KLePlxv4NlUUg4VmqKcWxeUdTHakp9cKAD0uG5T dJ6vBiFUFxGA5qfJh3+m1UQ== X-Google-Smtp-Source: AGHT+IEwOfZDI4F3jRslzGIEdwn06Xqt2NhOKADKScJCrSFXrWa8oRKCWk++dR3cp8p6tEK54xsId3Hhy6ePNZyFVA== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:36:e7b8:ac13:c9e8]) (user=dionnaglaze job=sendgmr) by 2002:a17:90a:39c3:b0:2e0:a07d:1e61 with SMTP id 98e67ed59e1d1-2e9b16e7a8emr6234a91.2.1731022130343; Thu, 07 Nov 2024 15:28:50 -0800 (PST) Date: Thu, 7 Nov 2024 23:24:44 +0000 In-Reply-To: <20241107232457.4059785-1-dionnaglaze@google.com> Precedence: bulk X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241107232457.4059785-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog Message-ID: <20241107232457.4059785-5-dionnaglaze@google.com> Subject: [PATCH v5 04/10] crypto: ccp: Fix uapi definitions of PSP errors From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org, Ashish Kalra , Tom Lendacky , "Borislav Petkov (AMD)" , Michael Roth , Brijesh Singh Cc: linux-coco@lists.linux.dev, Dionna Glaze , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Dave Hansen , John Allen , Herbert Xu , "David S. Miller" , Luis Chamberlain , Russ Weight , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Tianfei zhang , Alexey Kardashevskiy , stable@vger.kernel.org, linux-crypto@vger.kernel.org Additions to the error enum after the explicit 0x27 setting for SEV_RET_INVALID_KEY leads to incorrect value assignments. Use explicit values to match the manufacturer specifications more clearly. Fixes: 3a45dc2b419e ("crypto: ccp: Define the SEV-SNP commands") CC: Sean Christopherson CC: Paolo Bonzini CC: Thomas Gleixner CC: Ingo Molnar CC: Borislav Petkov CC: Dave Hansen CC: Ashish Kalra CC: Tom Lendacky CC: John Allen CC: Herbert Xu CC: "David S. Miller" CC: Michael Roth CC: Luis Chamberlain CC: Russ Weight CC: Danilo Krummrich CC: Greg Kroah-Hartman CC: "Rafael J. Wysocki" CC: Tianfei zhang CC: Alexey Kardashevskiy CC: stable@vger.kernel.org From: Alexey Kardashevskiy Signed-off-by: Alexey Kardashevskiy Signed-off-by: Dionna Glaze --- include/uapi/linux/psp-sev.h | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index 832c15d9155bd..eeb20dfb1fdaa 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -73,13 +73,20 @@ typedef enum { SEV_RET_INVALID_PARAM, SEV_RET_RESOURCE_LIMIT, SEV_RET_SECURE_DATA_INVALID, - SEV_RET_INVALID_KEY = 0x27, - SEV_RET_INVALID_PAGE_SIZE, - SEV_RET_INVALID_PAGE_STATE, - SEV_RET_INVALID_MDATA_ENTRY, - SEV_RET_INVALID_PAGE_OWNER, - SEV_RET_INVALID_PAGE_AEAD_OFLOW, - SEV_RET_RMP_INIT_REQUIRED, + SEV_RET_INVALID_PAGE_SIZE = 0x0019, + SEV_RET_INVALID_PAGE_STATE = 0x001A, + SEV_RET_INVALID_MDATA_ENTRY = 0x001B, + SEV_RET_INVALID_PAGE_OWNER = 0x001C, + SEV_RET_AEAD_OFLOW = 0x001D, + SEV_RET_EXIT_RING_BUFFER = 0x001F, + SEV_RET_RMP_INIT_REQUIRED = 0x0020, + SEV_RET_BAD_SVN = 0x0021, + SEV_RET_BAD_VERSION = 0x0022, + SEV_RET_SHUTDOWN_REQUIRED = 0x0023, + SEV_RET_UPDATE_FAILED = 0x0024, + SEV_RET_RESTORE_REQUIRED = 0x0025, + SEV_RET_RMP_INITIALIZATION_FAILED = 0x0026, + SEV_RET_INVALID_KEY = 0x0027, SEV_RET_MAX, } sev_ret_code; From patchwork Thu Nov 7 23:24:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dionna Amalie Glaze X-Patchwork-Id: 13867281 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A24A721B441 for ; Thu, 7 Nov 2024 23:28:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731022135; cv=none; b=cFAgme7iUuVsxBDaHiWnKMXxftNrzHwsEpj503//EfWhwOL28vMYNWZHaQEvizusGzvwbfrWa19co0Z5swIYsT4QpAthEGUZ/p0JDfhR1+QCc2bF+CRBW/JPQRUz7xVKa4ZReUgxCeI/z1ZcjRHgDy4DFrXkzfxEGYOxrRzNNio= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731022135; c=relaxed/simple; bh=K+mz91F1yF96twer+mf0GLIIgXZi2rB7gU/fVt6MfUs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FOoPMFZOpqZm+qifIjoK92xhR7sQIMG6RdTFPrHfGeQZ44F2do1KA2c7t7D9uiAUOxN7ND0k3SYsbpixfI45fvWhVH5ZLozmbdaDKPq9/BL9z9NgbsVa7JBo6BLvp4mF/IH20PczHfM0eREhGuUy6IHBK0+PVRYs6810gxHCjSQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dionnaglaze.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=u/47isQX; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--dionnaglaze.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="u/47isQX" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6ea8a5e862eso19841647b3.0 for ; Thu, 07 Nov 2024 15:28:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731022133; x=1731626933; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=LBA3fBcXFp9Bsca4pv6+EzGK6gmROtQOBEHvya8zy9o=; b=u/47isQXqOUsK9ybbtanXkP07ZVslOAEDcN7SVS17KwdDwOSQRyDpLHlKCopIehPNm 1CBQnDIovfaklREEhVIGtgROeJ4HfHhQGI1cNfAtx/U+2ao9igMEiUqi+eV0wAyqQlkf v1tCa1MPnzGzfuG5+/yfmlkUxeenBW26q9CtbvT5FvDkvLXz0oU24TtpwZUIrK3w8Hz3 P465GbvDPc04FLMFaLv7/yCIeBXClSPr81ZtbVBrDNLyGbL/cC7VYj6eWxPqn718xeBs NDLOizTXYdEDCoBG4u0QXxOlBB3bixSbJp5woOGj7M1W1W4k6Zi0FGc9Qi7KR6VAdX8u 8JSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731022133; x=1731626933; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=LBA3fBcXFp9Bsca4pv6+EzGK6gmROtQOBEHvya8zy9o=; b=rU7tLydJDoBbcZo2u83B+UQocw7RxtP25qzwAMUEWyJzk3W1MZ36mNyH0wQjZZXcce /P+yW5mrwZT/SOZxQvlm7ym7WDlPNqD9CkZSA4Wn/dMpGG8j9cqcdTcfrO8EQGsZtL5O BMeSGHhmrln7ASifItnx1E48FlJ+k6MibWUBsrt5tXG1fGZMSCLp4hKYJpa3zXDv1nki isQhITbAYcle+kAhIe0dHjMrJbQpIWEsWAw06otm8cSsZS4jAxyvWJ1QgNKuobwV5Yan YUH5awMf8iNp5aDWBcmjCXyQqeGkTWS/UYVN0zkoUIxP0uTsKdldbsyxR/cMsGtpiLhm iAtg== X-Forwarded-Encrypted: i=1; AJvYcCX9GFchRimpQOFobvxhfwwE5F4VDy0gm0nISbN6QCNn+LdRDX6V4J1uuqwwZcrry5o7h4YIXaU5RmIBQYc=@vger.kernel.org X-Gm-Message-State: AOJu0YxSF7yw4BvFOvbOvLJj+5NF/WoYUHgWdSsI8ws9yicfGmKIPQbj 8pWa/cju7nkhcUdZDUjSiwmU8wNcQv0ylLtNzeAX4Z4l3+5MUywGGx5L3grcKs0KHyM3z2HDotv W6H/srB6AQm4vQ4Fd5siszA== X-Google-Smtp-Source: AGHT+IFNyqKSoQsSCoYlJkBSKnmw23KoFCIBF0JCUO1nRJPI9ElZHcKVMfkjhcgPc9HIK87jTLhabc6SLTCBBfd2aA== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:36:e7b8:ac13:c9e8]) (user=dionnaglaze job=sendgmr) by 2002:a05:690c:316:b0:620:32ea:e1d4 with SMTP id 00721157ae682-6eade0af8bbmr127717b3.0.1731022132776; Thu, 07 Nov 2024 15:28:52 -0800 (PST) Date: Thu, 7 Nov 2024 23:24:45 +0000 In-Reply-To: <20241107232457.4059785-1-dionnaglaze@google.com> Precedence: bulk X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241107232457.4059785-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog Message-ID: <20241107232457.4059785-6-dionnaglaze@google.com> Subject: [PATCH v5 05/10] crypto: ccp: Add GCTX API to track ASID assignment From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org, Ashish Kalra , Tom Lendacky , John Allen , Herbert Xu , "David S. Miller" Cc: linux-coco@lists.linux.dev, Dionna Glaze , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Michael Roth , Luis Chamberlain , Russ Weight , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Tianfei zhang , Alexey Kardashevskiy , linux-crypto@vger.kernel.org In preparation for SEV firmware hotloading support, introduce a new way to create, activate, and decommission GCTX pages such that ccp is has all GCTX pages available to update as needed. Compliance with SEV-SNP API section 3.3 Firmware Updates and 4.1.1 Live Update: before a firmware is committed, all active GCTX pages should be updated with SNP_GUEST_STATUS to ensure their data structure remains consistent for the new firmware version. There can only be CPUID 0x8000001f_EDX-1 many SEV-SNP asids in use at one time, so this map associates asid to gctx in order to track which addresses are active gctx pages that need updating. When an asid and gctx page are decommissioned, the page is removed from tracking for update-purposes. CC: Sean Christopherson CC: Paolo Bonzini CC: Thomas Gleixner CC: Ingo Molnar CC: Borislav Petkov CC: Dave Hansen CC: Ashish Kalra CC: Tom Lendacky CC: John Allen CC: Herbert Xu CC: "David S. Miller" CC: Michael Roth CC: Luis Chamberlain CC: Russ Weight CC: Danilo Krummrich CC: Greg Kroah-Hartman CC: "Rafael J. Wysocki" CC: Tianfei zhang CC: Alexey Kardashevskiy Signed-off-by: Dionna Glaze --- drivers/crypto/ccp/sev-dev.c | 107 +++++++++++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 8 +++ include/linux/psp-sev.h | 52 +++++++++++++++++ 3 files changed, 167 insertions(+) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index af018afd9cd7f..036e8d5054fcc 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -109,6 +109,10 @@ static void *sev_init_ex_buffer; */ static struct sev_data_range_list *snp_range_list; +/* SEV ASID data tracks resources associated with an ASID to safely manage operations. */ +struct sev_asid_data *sev_asid_data; +u32 nr_asids, sev_min_asid, sev_max_asid, sev_es_max_asid; + static inline bool sev_version_greater_or_equal(u8 maj, u8 min) { struct sev_device *sev = psp_master->sev_data; @@ -1093,6 +1097,81 @@ static int snp_filter_reserved_mem_regions(struct resource *rs, void *arg) return 0; } +void *sev_snp_create_context(int asid, int *psp_ret) +{ + struct sev_data_snp_addr data = {}; + void *context; + int rc; + + if (!sev_asid_data) + return ERR_PTR(-ENODEV); + + /* Can't create a context for a used ASID. */ + if (sev_asid_data[asid].snp_context) + return ERR_PTR(-EBUSY); + + /* Allocate memory for context page */ + context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT); + if (!context) + return ERR_PTR(-ENOMEM); + + data.address = __psp_pa(context); + rc = sev_do_cmd(SEV_CMD_SNP_GCTX_CREATE, &data, psp_ret); + if (rc) { + pr_warn("Failed to create SEV-SNP context, rc %d fw_error %d", + rc, *psp_ret); + snp_free_firmware_page(context); + return ERR_PTR(-EIO); + } + + sev_asid_data[asid].snp_context = context; + + return context; +} + +int sev_snp_activate_asid(int asid, int *psp_ret) +{ + struct sev_data_snp_activate data = {0}; + void *context; + + if (!sev_asid_data) + return -ENODEV; + + context = sev_asid_data[asid].snp_context; + if (!context) + return -EINVAL; + + data.gctx_paddr = __psp_pa(context); + data.asid = asid; + return sev_do_cmd(SEV_CMD_SNP_ACTIVATE, &data, psp_ret); +} + +int sev_snp_guest_decommission(int asid, int *psp_ret) +{ + struct sev_data_snp_addr addr = {}; + struct sev_asid_data *data = &sev_asid_data[asid]; + int ret; + + if (!sev_asid_data) + return -ENODEV; + + /* If context is not created then do nothing */ + if (!data->snp_context) + return 0; + + /* Do the decommision, which will unbind the ASID from the SNP context */ + addr.address = __sme_pa(data->snp_context); + ret = sev_do_cmd(SEV_CMD_SNP_DECOMMISSION, &addr, NULL); + + if (WARN_ONCE(ret, "Failed to release guest context, ret %d", ret)) + return ret; + + snp_free_firmware_page(data->snp_context); + data->snp_context = NULL; + + return 0; +} + static int __sev_snp_init_locked(int *error) { struct psp_device *psp = psp_master; @@ -1306,6 +1385,27 @@ static int __sev_platform_init_locked(int *error) return 0; } +static int __sev_asid_data_init(void) +{ + u32 eax, ebx; + + if (sev_asid_data) + return 0; + + cpuid(0x8000001f, &eax, &ebx, &sev_max_asid, &sev_min_asid); + if (!sev_max_asid) + return -ENODEV; + + nr_asids = sev_max_asid + 1; + sev_es_max_asid = sev_min_asid - 1; + + sev_asid_data = kcalloc(nr_asids, sizeof(*sev_asid_data), GFP_KERNEL); + if (!sev_asid_data) + return -ENOMEM; + + return 0; +} + static int _sev_platform_init_locked(struct sev_platform_init_args *args) { struct sev_device *sev; @@ -1319,6 +1419,10 @@ static int _sev_platform_init_locked(struct sev_platform_init_args *args) if (sev->state == SEV_STATE_INIT) return 0; + rc = __sev_asid_data_init(); + if (rc) + return rc; + /* * Legacy guests cannot be running while SNP_INIT(_EX) is executing, * so perform SEV-SNP initialization at probe time. @@ -2329,6 +2433,9 @@ static void __sev_firmware_shutdown(struct sev_device *sev, bool panic) snp_range_list = NULL; } + kfree(sev_asid_data); + sev_asid_data = NULL; + __sev_snp_shutdown_locked(&error, panic); } diff --git a/drivers/crypto/ccp/sev-dev.h b/drivers/crypto/ccp/sev-dev.h index 3e4e5574e88a3..7d0fdfdda30b6 100644 --- a/drivers/crypto/ccp/sev-dev.h +++ b/drivers/crypto/ccp/sev-dev.h @@ -65,4 +65,12 @@ void sev_dev_destroy(struct psp_device *psp); void sev_pci_init(void); void sev_pci_exit(void); +struct sev_asid_data { + void *snp_context; +}; + +/* Extern to be shared with firmware_upload API implementation if configured. */ +extern struct sev_asid_data *sev_asid_data; +extern u32 nr_asids, sev_min_asid, sev_max_asid, sev_es_max_asid; + #endif /* __SEV_DEV_H */ diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 903ddfea85850..ac36b5ddf717d 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -942,6 +942,58 @@ int sev_guest_decommission(struct sev_data_decommission *data, int *error); */ int sev_do_cmd(int cmd, void *data, int *psp_ret); +/** + * sev_snp_create_context - allocates an SNP context firmware page + * + * Associates the created context with the ASID that an activation + * call after SNP_LAUNCH_START will commit. The association is needed + * to track active guest context pages to refresh during firmware hotload. + * + * @asid: The ASID allocated to the caller that will be used in a subsequent SNP_ACTIVATE. + * @psp_ret: sev command return code. + * + * Returns: + * A pointer to the SNP context page, or an ERR_PTR of + * -%ENODEV if the PSP device is not available + * -%ENOTSUPP if PSP device does not support SEV + * -%ETIMEDOUT if the SEV command timed out + * -%EIO if PSP device returned a non-zero return code + */ +void *sev_snp_create_context(int asid, int *psp_ret); + +/** + * sev_snp_activate_asid - issues SNP_ACTIVATE for the ASID and associated guest context page. + * + * @asid: The ASID to activate. + * @psp_ret: sev command return code. + * + * Returns: + * 0 if the SEV device successfully processed the command + * -%ENODEV if the PSP device is not available + * -%ENOTSUPP if PSP device does not support SEV + * -%ETIMEDOUT if the SEV command timed out + * -%EIO if PSP device returned a non-zero return code + */ +int sev_snp_activate_asid(int asid, int *psp_ret); + +/** + * sev_snp_guest_decommission - issues SNP_DECOMMISSION for an ASID's guest context page, and frees + * it. + * + * The caller must ensure mutual exclusion with any process that may deactivate ASIDs. + * + * @asid: The ASID to activate. + * @psp_ret: sev command return code. + * + * Returns: + * 0 if the SEV device successfully processed the command + * -%ENODEV if the PSP device is not available + * -%ENOTSUPP if PSP device does not support SEV + * -%ETIMEDOUT if the SEV command timed out + * -%EIO if PSP device returned a non-zero return code + */ +int sev_snp_guest_decommission(int asid, int *psp_ret); + void *psp_copy_user_blob(u64 uaddr, u32 len); void *snp_alloc_firmware_page(gfp_t mask); void snp_free_firmware_page(void *addr); From patchwork Thu Nov 7 23:24:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dionna Amalie Glaze X-Patchwork-Id: 13867282 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1A9FF21C168 for ; Thu, 7 Nov 2024 23:28:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731022139; cv=none; b=bbcQxt+Y7oiOHr3b/bj/luWmwbr0xModCa+SOIk2Z/8YTSPHdM9TJ0tFG4tVzQYEOax7DwrnbgzyEMSUb7rf9konQp1srfvkm+Z/+eZ0xJrmwkGHzhHVAGEvuEkeEq5eT6upGmQJiplIOTLph98cuymZMsJhYHWrV38sNcPauWQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731022139; c=relaxed/simple; bh=w3t8NcSQiVYaQfeYvSCaf2u6NWnmvxc6sDv1D1acAXg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Bm2mF22rzKuxNEYYMGZvvkGVM45iXpcD7mCZqa8Ruv0ZbIpTeBJ3kDoYgwOZdQFOxga1Y5/cxIbeFk1tZW2lqk565XQ9OgaTrvfS1ogmqh52exEdgUAo9D5pE7p3Cc6UupQn8edMBRadGuRL5bTmsi7kHWBJIj34XjIWExZ/cao= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dionnaglaze.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=yimFO5U0; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--dionnaglaze.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="yimFO5U0" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ea8a5e862eso19842297b3.0 for ; Thu, 07 Nov 2024 15:28:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731022136; x=1731626936; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=evZMQJVjS281y7mgq9nxAoLoqFGzpTjLyXpt70+Vz8s=; b=yimFO5U0MTJLy/JWdJkCgLoS6algMwOsLaNIP7SepwVHmObgH/RdK2iXn3mlpXQ2QL EnJ3e1u6utC2dfLEsS+AicqP+4q32TfUwsrDpJsH+PBq5uFcx92zyhZSYcOkVAEtS0rg sTvlg69Z1WM2D6cbxc/BPvZOy8V2tc/bxq8J2iqZ1suAE360ye2un10394rCVXjyu6E/ pnqb6Fn2E5JHYAicMtqeqB/m1ShkrdBnCfI7NSy+nSE4gQQAJD7EhH5UhbxqzeKmBUFA 9VvNLfu6Pb3PW5b+1zmx8YffdwwY89upE/272qatt05lt7Mtt/rm4EQ7SsyEGpY4Crl1 74PQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731022136; x=1731626936; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=evZMQJVjS281y7mgq9nxAoLoqFGzpTjLyXpt70+Vz8s=; b=saiLutOkG4vhwgzhxvaecn/dlZvG/K9FXpoPU/aKkJJM/qPi4jK+yZOHliydQILkcj shOQDRHQ/S7Xqma5XmLbZF2zxmY9N4e6dbShvIyPD0VfIVAJYyijEC9H0ixxgYg4brJb JYuuVUGgvzikZBHfacDRntr3Fjhsk1HBFPURDBvQQHIgS26tTE8Bv18BM5QPknTn24ht lxY1woz0Ee7j56C4ESFOHsHGhZDM/01mf+iNW3PHyTLYqX/r+uQZwmcWMU61rggxEluh UL/47wgbZ+EpLgZdK97a066feiMqo0Q7Z/E9BbVHJOEbRvYMwx61vRjgg6YnD95Y1AVk mYoQ== X-Forwarded-Encrypted: i=1; AJvYcCWvoN4J2ZkFkz9vBWnxSHyzwdnFGp9RDKnk+St+oThvK4SEroZxHwBSU0ywzeu+4N7XvPGrCXCLfBi+jr0=@vger.kernel.org X-Gm-Message-State: AOJu0Yw+/Pr5dvTQ2wXpZaHgUGg5oyK7cWYkJw+KC6TsOuUxC7P3k+5i TcaLyB02Am8yYZ53U5LBYlDLcTLjQLl5fr60Sc+0szj4r45f20al3OUpjshgHs6EjrJrjykeYc2 GO0Q1hd/St/IIOpKvTr5XIA== X-Google-Smtp-Source: AGHT+IEiBxXecpAfPF7zI+QvhULCB09LvuRbBRIF4g+L4zEf+jnx5PwIqzjq4HHLbfzMQXezeXScEP4InHgW2gK7Gw== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:36:e7b8:ac13:c9e8]) (user=dionnaglaze job=sendgmr) by 2002:a0d:c302:0:b0:6be:523:af53 with SMTP id 00721157ae682-6eadc114281mr126617b3.3.1731022135974; Thu, 07 Nov 2024 15:28:55 -0800 (PST) Date: Thu, 7 Nov 2024 23:24:46 +0000 In-Reply-To: <20241107232457.4059785-1-dionnaglaze@google.com> Precedence: bulk X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241107232457.4059785-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog Message-ID: <20241107232457.4059785-7-dionnaglaze@google.com> Subject: [PATCH v5 06/10] crypto: ccp: Add DOWNLOAD_FIRMWARE_EX support From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org, Tom Lendacky , John Allen , Herbert Xu , "David S. Miller" , Ashish Kalra Cc: linux-coco@lists.linux.dev, Dionna Glaze , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Michael Roth , Luis Chamberlain , Russ Weight , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Tianfei zhang , Alexey Kardashevskiy , linux-crypto@vger.kernel.org In order to support firmware hotloading, the DOWNLOAD_FIRMWARE_EX command must be available. The DOWNLOAD_FIRMWARE_EX command requires cache flushing and introduces new error codes that could be returned to user space. Access to the command is through the firmware_upload API rather than through the ioctl interface to prefer a common interface. On init, the ccp device will make /sys/class/firmware/amd/loading etc firmware upload API attributes available to late-load a SEV-SNP firmware binary. The firmware_upload API errors reported are actionable in the following ways: * FW_UPLOAD_ERR_HW_ERROR: the machine is in an unstable state and must be reset. * FW_UPLOAD_ERR_RW_ERROR: the firmware update went bad but can be recovered by hotloading the previous firmware version. Also used in the case that the kernel used the API wrong (bug). * FW_UPLOAD_ERR_FW_INVALID: user error with the data provided, but no instability is expected and no recovery actions are needed. * FW_UPLOAD_ERR_BUSY: upload attempted at a bad time either due to overload or the machine is in the wrong platform state. synthetic_restore_required: Instead of tracking the status of whether an individual GCTX is safe for use in a firmware command, force all following commands to fail with an error that is indicative of needing a firmware rollback. To test: 1. Build the kernel enabling SEV-SNP as normal and add CONFIG_FW_UPLOAD=y. 2. Add the following to your kernel_cmdline: ccp.psp_init_on_probe=0. 3.Get an AMD SEV-SNP firmware sbin appropriate to your Epyc chip model at https://www.amd.com/en/developer/sev.html and extract to get a .sbin file. 4. Run the following with your sbinfile in FW: echo 1 > /sys/class/firmware/snp_dlfw_ex/loading cat "${FW?}" > /sys/class/firmware/snp_dlfw_ex/data echo 0 > /sys/class/firmware/snp_dlfw_ex/loading 5. Verify the firmware update message in dmesg. CC: Sean Christopherson CC: Paolo Bonzini CC: Thomas Gleixner CC: Ingo Molnar CC: Borislav Petkov CC: Dave Hansen CC: Ashish Kalra CC: Tom Lendacky CC: John Allen CC: Herbert Xu CC: "David S. Miller" CC: Michael Roth CC: Luis Chamberlain CC: Russ Weight CC: Danilo Krummrich CC: Greg Kroah-Hartman CC: "Rafael J. Wysocki" CC: Tianfei zhang CC: Alexey Kardashevskiy Signed-off-by: Dionna Glaze --- drivers/crypto/ccp/Kconfig | 10 ++ drivers/crypto/ccp/Makefile | 1 + drivers/crypto/ccp/sev-dev.c | 22 +-- drivers/crypto/ccp/sev-dev.h | 27 ++++ drivers/crypto/ccp/sev-fw.c | 267 +++++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 17 +++ 6 files changed, 334 insertions(+), 10 deletions(-) create mode 100644 drivers/crypto/ccp/sev-fw.c diff --git a/drivers/crypto/ccp/Kconfig b/drivers/crypto/ccp/Kconfig index f394e45e11ab4..40be991f15d28 100644 --- a/drivers/crypto/ccp/Kconfig +++ b/drivers/crypto/ccp/Kconfig @@ -46,6 +46,16 @@ config CRYPTO_DEV_SP_PSP along with software-based Trusted Execution Environment (TEE) to enable third-party trusted applications. +config CRYPTO_DEV_SP_PSP_FW_UPLOAD + bool "Platform Security Processor (PSP) device with firmware hotloading" + default y + depends on CRYPTO_DEV_SP_PSP && FW_LOADER && FW_UPLOAD + help + Provide support for AMD Platform Security Processor firmware. + The PSP firmware can be updated while no SEV or SEV-ES VMs are active. + Users of this feature should be aware of the error modes that indicate + required manual rollback or reset due to instablity. + config CRYPTO_DEV_CCP_DEBUGFS bool "Enable CCP Internals in DebugFS" default n diff --git a/drivers/crypto/ccp/Makefile b/drivers/crypto/ccp/Makefile index 394484929dae3..5ce69134ec48b 100644 --- a/drivers/crypto/ccp/Makefile +++ b/drivers/crypto/ccp/Makefile @@ -14,6 +14,7 @@ ccp-$(CONFIG_CRYPTO_DEV_SP_PSP) += psp-dev.o \ platform-access.o \ dbc.o \ hsti.o +ccp-$(CONFIG_CRYPTO_DEV_SP_PSP_FW_UPLOAD) += sev-fw.o obj-$(CONFIG_CRYPTO_DEV_CCP_CRYPTO) += ccp-crypto.o ccp-crypto-objs := ccp-crypto-main.o \ diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 036e8d5054fcc..498ec8a0deeca 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -227,6 +227,7 @@ static int sev_cmd_buffer_len(int cmd) case SEV_CMD_SNP_GUEST_REQUEST: return sizeof(struct sev_data_snp_guest_request); case SEV_CMD_SNP_CONFIG: return sizeof(struct sev_user_data_snp_config); case SEV_CMD_SNP_COMMIT: return sizeof(struct sev_data_snp_commit); + case SEV_CMD_SNP_DOWNLOAD_FIRMWARE_EX: return sizeof(struct sev_data_download_firmware_ex); default: return 0; } @@ -488,7 +489,7 @@ void snp_free_firmware_page(void *addr) } EXPORT_SYMBOL_GPL(snp_free_firmware_page); -static void *sev_fw_alloc(unsigned long len) +void *sev_fw_alloc(unsigned long len) { struct page *page; @@ -856,6 +857,10 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) if (WARN_ON_ONCE(!data != !buf_len)) return -EINVAL; + ret = sev_snp_synthetic_error(sev, psp_ret); + if (ret) + return ret; + /* * Copy the incoming data to driver's scratch buffer as __pa() will not * work for some memory, e.g. vmalloc'd addresses, and @data may not be @@ -1632,7 +1637,7 @@ void *psp_copy_user_blob(u64 uaddr, u32 len) } EXPORT_SYMBOL_GPL(psp_copy_user_blob); -static int sev_get_api_version(void) +int sev_get_api_version(void) { struct sev_device *sev = psp_master->sev_data; struct sev_user_data_status status; @@ -1707,14 +1712,7 @@ static int sev_update_firmware(struct device *dev) return -1; } - /* - * SEV FW expects the physical address given to it to be 32 - * byte aligned. Memory allocated has structure placed at the - * beginning followed by the firmware being passed to the SEV - * FW. Allocate enough memory for data structure + alignment - * padding + SEV FW. - */ - data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32); + data_size = ALIGN(sizeof(struct sev_data_download_firmware), SEV_FW_ALIGNMENT); order = get_order(firmware->size + data_size); p = alloc_pages(GFP_KERNEL, order); @@ -2378,6 +2376,8 @@ int sev_dev_init(struct psp_device *psp) if (ret) goto e_irq; + sev_snp_dev_init_firmware_upload(sev); + dev_notice(dev, "sev enabled\n"); return 0; @@ -2459,6 +2459,8 @@ void sev_dev_destroy(struct psp_device *psp) kref_put(&misc_dev->refcount, sev_exit); psp_clear_sev_irq_handler(psp); + + sev_snp_dev_init_firmware_upload(sev); } static int snp_shutdown_on_panic(struct notifier_block *nb, diff --git a/drivers/crypto/ccp/sev-dev.h b/drivers/crypto/ccp/sev-dev.h index 7d0fdfdda30b6..db65d2c7afe9b 100644 --- a/drivers/crypto/ccp/sev-dev.h +++ b/drivers/crypto/ccp/sev-dev.h @@ -29,6 +29,15 @@ #define SEV_CMD_COMPLETE BIT(1) #define SEV_CMDRESP_IOC BIT(0) +/* + * SEV FW expects the physical address given to it to be 32 + * byte aligned. Memory allocated has structure placed at the + * beginning followed by the firmware being passed to the SEV + * FW. Allocate enough memory for data structure + alignment + * padding + SEV FW. + */ +#define SEV_FW_ALIGNMENT 32 + struct sev_misc_dev { struct kref refcount; struct miscdevice misc; @@ -57,6 +66,11 @@ struct sev_device { bool cmd_buf_backup_active; bool snp_initialized; + +#ifdef CONFIG_FW_UPLOAD + struct fw_upload *fwl; + bool fw_cancel; +#endif /* CONFIG_FW_UPLOAD */ }; int sev_dev_init(struct psp_device *psp); @@ -73,4 +87,17 @@ struct sev_asid_data { extern struct sev_asid_data *sev_asid_data; extern u32 nr_asids, sev_min_asid, sev_max_asid, sev_es_max_asid; +void *sev_fw_alloc(unsigned long len); +int sev_get_api_version(void); + +#ifdef CONFIG_CRYPTO_DEV_SP_PSP_FW_UPLOAD +void sev_snp_dev_init_firmware_upload(struct sev_device *sev); +void sev_snp_destroy_firmware_upload(struct sev_device *sev); +int sev_snp_synthetic_error(struct sev_device *sev, int *psp_ret); +#else +static inline void sev_snp_dev_init_firmware_upload(struct sev_device *sev) { } +static inline void sev_snp_destroy_firmware_upload(struct sev_device *sev) { } +static inline int sev_snp_synthetic_error(struct sev_device *sev, int *psp_ret) { return 0; } +#endif /* CONFIG_CRYPTO_DEV_SP_PSP_FW_UPLOAD */ + #endif /* __SEV_DEV_H */ diff --git a/drivers/crypto/ccp/sev-fw.c b/drivers/crypto/ccp/sev-fw.c new file mode 100644 index 0000000000000..6a87872174ee5 --- /dev/null +++ b/drivers/crypto/ccp/sev-fw.c @@ -0,0 +1,267 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * AMD Secure Encrypted Virtualization (SEV) firmware upload API + */ + +#include +#include +#include + +#include + +#include "sev-dev.h" + +static bool synthetic_restore_required; + +int sev_snp_synthetic_error(struct sev_device *sev, int *psp_ret) +{ + if (synthetic_restore_required) { + *psp_ret = SEV_RET_RESTORE_REQUIRED; + return -EIO; + } + return 0; +} + +static int sev_snp_download_firmware_ex(struct sev_device *sev, const u8 *data, u32 size, + int *error) +{ + struct sev_data_download_firmware_ex *data_ex; + int ret, order; + struct page *p; + u64 data_size; + void *fw_dest; + + data_size = ALIGN(sizeof(struct sev_data_download_firmware_ex), SEV_FW_ALIGNMENT); + + order = get_order(size + data_size); + p = alloc_pages(GFP_KERNEL, order); + if (!p) + return -ENOMEM; + + /* + * Copy firmware data to a kernel allocated contiguous + * memory region. + */ + data_ex = page_address(p); + fw_dest = page_address(p) + data_size; + memset(data_ex, 0, data_size); + memcpy(fw_dest, data, size); + + data_ex->fw_paddr = __psp_pa(fw_dest); + data_ex->fw_len = size; + data_ex->length = sizeof(struct sev_data_download_firmware_ex); + /* commit is purposefully unset for GCTX update failure to advise rollback */ + + ret = sev_do_cmd(SEV_CMD_SNP_DOWNLOAD_FIRMWARE_EX, data_ex, error); + + if (ret) + goto free_err; + + /* Need to do a DF_FLUSH after live firmware update */ + wbinvd_on_all_cpus(); + ret = sev_do_cmd(SEV_CMD_SNP_DF_FLUSH, NULL, error); + if (ret) + dev_dbg(sev->dev, "DF_FLUSH error %d\n", *error); + +free_err: + __free_pages(p, order); + return ret; +} + +static enum fw_upload_err snp_dlfw_ex_prepare(struct fw_upload *fw_upload, + const u8 *data, u32 size) +{ + return FW_UPLOAD_ERR_NONE; +} + +static enum fw_upload_err snp_dlfw_ex_poll_complete(struct fw_upload *fw_upload) +{ + return FW_UPLOAD_ERR_NONE; +} + +/* Cancel can be called asynchronously, but DOWNLOAD_FIRMWARE_EX is atomic and cannot + * be canceled. There is no need to synchronize updates to fw_cancel. + */ +static void snp_dlfw_ex_cancel(struct fw_upload *fw_upload) +{ + /* fw_upload not-NULL guaranteed by firmware_upload API */ + struct sev_device *sev = fw_upload->dd_handle; + + sev->fw_cancel = true; +} + +static enum fw_upload_err snp_dlfw_ex_err_translate(struct sev_device *sev, int psp_ret) +{ + dev_dbg(sev->dev, "Failed to update SEV firmware: %#x\n", psp_ret); + /* + * Operation error: + * HW_ERROR: Critical error. Machine needs repairs now. + * RW_ERROR: Severe error. Roll back to the prior version to recover. + * User error: + * FW_INVALID: Bad input for this interface. + * BUSY: Wrong machine state to run download_firmware_ex. + */ + switch (psp_ret) { + case SEV_RET_RESTORE_REQUIRED: + dev_warn(sev->dev, "Firmware updated but unusable\n"); + dev_warn(sev->dev, "Need to do manual firmware rollback!!!\n"); + return FW_UPLOAD_ERR_RW_ERROR; + case SEV_RET_SHUTDOWN_REQUIRED: + /* No state changes made. Not a hardware error. */ + dev_warn(sev->dev, "Firmware image cannot be live updated\n"); + return FW_UPLOAD_ERR_FW_INVALID; + case SEV_RET_BAD_VERSION: + /* No state changes made. Not a hardware error. */ + dev_warn(sev->dev, "Firmware image is not well formed\n"); + return FW_UPLOAD_ERR_FW_INVALID; + /* SEV-specific errors that can still happen. */ + case SEV_RET_BAD_SIGNATURE: + /* No state changes made. Not a hardware error. */ + dev_warn(sev->dev, "Firmware image signature is bad\n"); + return FW_UPLOAD_ERR_FW_INVALID; + case SEV_RET_INVALID_PLATFORM_STATE: + /* Calling at the wrong time. Not a hardware error. */ + dev_warn(sev->dev, "Firmware not updated as SEV in INIT state\n"); + return FW_UPLOAD_ERR_BUSY; + case SEV_RET_HWSEV_RET_UNSAFE: + dev_err(sev->dev, "Firmware is unstable. Reset your machine!!!\n"); + return FW_UPLOAD_ERR_HW_ERROR; + /* Kernel bug cases. */ + case SEV_RET_INVALID_PARAM: + dev_err(sev->dev, "Download-firmware-EX invalid parameter\n"); + return FW_UPLOAD_ERR_RW_ERROR; + case SEV_RET_INVALID_ADDRESS: + dev_err(sev->dev, "Download-firmware-EX invalid address\n"); + return FW_UPLOAD_ERR_RW_ERROR; + default: + dev_err(sev->dev, "Unhandled download_firmware_ex err %d\n", psp_ret); + return FW_UPLOAD_ERR_HW_ERROR; + } +} + +static enum fw_upload_err snp_update_guest_statuses(struct sev_device *sev) +{ + struct sev_data_snp_guest_status status_data; + void *snp_guest_status; + enum fw_upload_err ret; + int error; + + /* + * Force an update of guest context pages after SEV firmware + * live update by issuing SNP_GUEST_STATUS on all guest + * context pages. + */ + snp_guest_status = sev_fw_alloc(PAGE_SIZE); + if (!snp_guest_status) + return FW_UPLOAD_ERR_INVALID_SIZE; + + /* + * After the last bound asid-to-gctx page is snp_unbound_gctx_end-many + * unbound gctx pages that also need updating. + */ + for (int i = 1; i <= sev_es_max_asid; i++) { + if (!sev_asid_data[i].snp_context) + continue; + + status_data.gctx_paddr = __psp_pa(sev_asid_data[i].snp_context); + status_data.address = __psp_pa(snp_guest_status); + ret = sev_do_cmd(SEV_CMD_SNP_GUEST_STATUS, &status_data, &error); + if (ret) { + /* + * Handle race with SNP VM being destroyed/decommissoned, + * if guest context page invalid error is returned, + * assume guest has been destroyed. + */ + if (error == SEV_RET_INVALID_GUEST) + continue; + synthetic_restore_required = true; + dev_err(sev->dev, "SNP GCTX update error requires rollback: %#x\n", + error); + ret = FW_UPLOAD_ERR_RW_ERROR; + goto fw_err; + } + } +fw_err: + snp_free_firmware_page(snp_guest_status); + return ret; +} + +static enum fw_upload_err snp_dlfw_ex_write(struct fw_upload *fwl, const u8 *data, + u32 offset, u32 size, u32 *written) +{ + /* fwl not-NULL guaranteed by firmware_upload API */ + struct sev_device *sev = fwl->dd_handle; + u8 api_major, api_minor, build; + int ret, error; + + if (!sev) + return FW_UPLOAD_ERR_HW_ERROR; + + if (sev->fw_cancel) + return FW_UPLOAD_ERR_CANCELED; + + /* + * SEV firmware update is a one-shot update operation, the write() + * callback to be invoked multiple times for the same update is + * unexpected. + */ + if (offset) + return FW_UPLOAD_ERR_INVALID_SIZE; + + if (sev_get_api_version()) + return FW_UPLOAD_ERR_HW_ERROR; + + api_major = sev->api_major; + api_minor = sev->api_minor; + build = sev->build; + + ret = sev_snp_download_firmware_ex(sev, data, size, &error); + if (ret) + return snp_dlfw_ex_err_translate(sev, error); + + ret = snp_update_guest_statuses(sev); + if (ret) + return ret; + + sev_get_api_version(); + if (api_major != sev->api_major || api_minor != sev->api_minor || + build != sev->build) { + dev_info(sev->dev, "SEV firmware updated from %d.%d.%d to %d.%d.%d\n", + api_major, api_minor, build, + sev->api_major, sev->api_minor, sev->build); + } else { + dev_info(sev->dev, "SEV firmware same as old %d.%d.%d\n", + api_major, api_minor, build); + } + + *written = size; + return FW_UPLOAD_ERR_NONE; +} + +static const struct fw_upload_ops snp_dlfw_ex_ops = { + .prepare = snp_dlfw_ex_prepare, + .write = snp_dlfw_ex_write, + .poll_complete = snp_dlfw_ex_poll_complete, + .cancel = snp_dlfw_ex_cancel, +}; + +void sev_snp_dev_init_firmware_upload(struct sev_device *sev) +{ + struct fw_upload *fwl; + + fwl = firmware_upload_register(THIS_MODULE, sev->dev, "snp_dlfw_ex", &snp_dlfw_ex_ops, sev); + + if (IS_ERR(fwl)) + dev_err(sev->dev, "SEV firmware upload initialization error %ld\n", PTR_ERR(fwl)); + else + sev->fwl = fwl; +} + +void sev_snp_destroy_firmware_upload(struct sev_device *sev) +{ + if (!sev || !sev->fwl) + return; + + firmware_upload_unregister(sev->fwl); +} + diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index ac36b5ddf717d..b91cbdc208f49 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -185,6 +185,23 @@ struct sev_data_download_firmware { u32 len; /* In */ } __packed; +/** + * struct sev_data_download_firmware_ex - DOWNLOAD_FIRMWARE_EX command parameters + * + * @length: length of this command buffer + * @fw_paddr: physical address of firmware image + * @fw_len: len of the firmware image + * @commit: automatically commit the newly installed image + */ +struct sev_data_download_firmware_ex { + u32 length; /* In */ + u32 reserved; /* In */ + u64 fw_paddr; /* In */ + u32 fw_len; /* In */ + u32 commit:1; /* In */ + u32 reserved2:31; /* In */ +} __packed; + /** * struct sev_data_get_id - GET_ID command parameters * From patchwork Thu Nov 7 23:24:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dionna Amalie Glaze X-Patchwork-Id: 13867283 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 136DC21C174 for ; Thu, 7 Nov 2024 23:28:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731022141; cv=none; b=jkNT9eaxsMLi+Al1eH5MV9nLLhUh9GHCx/rAPthMjbVttwGMInoM+ZnXw2eM+/+OZO0zVEI+CB33T6owOjXb/zD/Tmkz5M52LjLpIuoNfVnIARSoACbTqE/U9xYEsuk1LFFOfT8mFd+2nd5+AGfbQSfmfPPae3APruJcqN52luA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731022141; c=relaxed/simple; bh=GeMVM4Ec+fn//3H9389VcZZMcj62SCQA+XOQ+JtdlTk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Tsll0LI3IqZI4NW6mqP3fxBf9wejvDI7pAfXWt8TUVgyd9VGK6mCPDRVrF9N+B8KWIUk+8fhNin+yv4ZNzs93Ah7qp8mMz9kX/uEx4WE0gRuwiLAnvAYsgZXxR7KNqn+nB6CNujz/GTG3M0bREqKFveBzANd62v5FbQIW5dxDSg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dionnaglaze.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=JX9FZUk/; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--dionnaglaze.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="JX9FZUk/" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6ea8901dff1so32237587b3.1 for ; Thu, 07 Nov 2024 15:28:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731022138; x=1731626938; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=/mtW86B3DSmlFKT0lotGzdj7a1N3s2uwCyGHDKQX9vU=; b=JX9FZUk/e6j59NxGLo6eMoMD/5JJ1GIZ6T4TqiczfVL7UibG3f1PN5pCvADJ4MhnjS GDJ3eg6ns0z2X2juoctL3TXICjhQNPE/Nayy+BmJS19l0BpAb5WK/9/Gf4ecgkpxV22n ER5LlqX24cy2DbAy8NBG7kLnvFeivLklM9M3HkJSb9txouWmN2I739qR11Hqj0u5FrNi 3m3BccRQexo5+G77PYL4uVjiw1+7bWCnzLEVXttAOiWulXsWxveaAa9cVAZo/w/lIEjg VBPKC/Y+Xtq47hTQLijVBIdmKGTFjQ7DeEVMDvTDLlGPTIF8OdU+PsMkFWPYuUAfATzS HdFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731022138; x=1731626938; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/mtW86B3DSmlFKT0lotGzdj7a1N3s2uwCyGHDKQX9vU=; b=XAGXZ4PMouLEa3g82HQWef8QxW0n8N0sgVkVkE0tPPVqX+VzKx6NR0ObZFjmgW7l3k MUGyDV1zBew21HBaBx+xuwiasMWYx40XDiePzpLfVLIyiwxeR/3Be3lPrqMCUyn5UwvC /9mcngXD65pAFqC8TufxkhnxrfX02k8rz7BL2tSuDDDp6HzB+JH6xy1H09a721ZnT0Ss LTT4aNnXyqWrOmZzMPTXQ1krgyQrPCWnPspzXVMyI9WJvmRTw+KtLIO5wu3yRmrE/3b4 tsky1xxE+UF4vfcGfM9P0v97DfU3sA3Xdd2usSkd1Dp30L38xGXMENWJNmyvaWw8nWGC q2zg== X-Forwarded-Encrypted: i=1; AJvYcCULXKdPsu2mn5+3TMPVMyqqJUVQhwU4HPsVMj/CqR2g6LzTBvG7LbO5UmQMCyVbTM5Hw8oE0Cq30xqyIpM=@vger.kernel.org X-Gm-Message-State: AOJu0Yx4FeIIaFXkTkf9K9eqUhG+OlRdvWNOE7Zcc49f4fCZcQHMXZb3 1l6lOo5nvLp4PSAWYUHI1TSKMXx7dAa1gPf9VV3AkaOeb2yuFzDdgPKWkRimLut2xyX8n+HMq+Y 15QGE5KtDAn/6O4orK90WpQ== X-Google-Smtp-Source: AGHT+IHrNNzZrarS/YQjs+xV1rDR0GePDAFsw74nsrSHNCOrLqUNJv5SPe4CSAD/IfeNYkWr5gdo2XMmDenU30uPTw== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:36:e7b8:ac13:c9e8]) (user=dionnaglaze job=sendgmr) by 2002:a05:690c:3501:b0:62c:f976:a763 with SMTP id 00721157ae682-6eaddd8b094mr187867b3.1.1731022138141; Thu, 07 Nov 2024 15:28:58 -0800 (PST) Date: Thu, 7 Nov 2024 23:24:47 +0000 In-Reply-To: <20241107232457.4059785-1-dionnaglaze@google.com> Precedence: bulk X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241107232457.4059785-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog Message-ID: <20241107232457.4059785-8-dionnaglaze@google.com> Subject: [PATCH v5 07/10] crypto: ccp: Add preferred access checking method From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org, Ashish Kalra , Tom Lendacky , John Allen , Herbert Xu , "David S. Miller" Cc: linux-coco@lists.linux.dev, Dionna Glaze , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Michael Roth , Luis Chamberlain , Russ Weight , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Tianfei zhang , Alexey Kardashevskiy , linux-crypto@vger.kernel.org sev_issue_cmd_external_user is the only function that checks permissions before performing its task. With the new GCTX API, it's important to establish permission once and have that determination dominate later API uses. This is implicitly how ccp has been used by dominating uses of sev_do_cmd by a successful sev_issue_cmd_external_user call. Consider sev_issue_cmd_external_user deprecated by checking if a held file descriptor passes file_is_sev, similar to the file_is_kvm function. This also fixes the header comment that the bad file error code is -%EINVAL when in fact it is -%EBADF. CC: Sean Christopherson CC: Paolo Bonzini CC: Thomas Gleixner CC: Ingo Molnar CC: Borislav Petkov CC: Dave Hansen CC: Ashish Kalra CC: Tom Lendacky CC: John Allen CC: Herbert Xu CC: "David S. Miller" CC: Michael Roth CC: Luis Chamberlain CC: Russ Weight CC: Danilo Krummrich CC: Greg Kroah-Hartman CC: "Rafael J. Wysocki" CC: Tianfei zhang CC: Alexey Kardashevskiy Signed-off-by: Dionna Glaze --- drivers/crypto/ccp/sev-dev.c | 13 +++++++++++-- include/linux/psp-sev.h | 11 ++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 498ec8a0deeca..f92e6a222da8a 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -8,6 +8,7 @@ */ #include +#include #include #include #include @@ -2486,11 +2487,19 @@ static struct notifier_block snp_panic_notifier = { .notifier_call = snp_shutdown_on_panic, }; +bool file_is_sev(struct file *p) +{ + return p && p->f_op == &sev_fops; +} +EXPORT_SYMBOL_GPL(file_is_sev); + int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, void *data, int *error) { - if (!filep || filep->f_op != &sev_fops) - return -EBADF; + int rc = file_is_sev(filep) ? 0 : -EBADF; + + if (rc) + return rc; return sev_do_cmd(cmd, data, error); } diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index b91cbdc208f49..ed85c0cfcfcbe 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -879,11 +879,18 @@ int sev_platform_status(struct sev_user_data_status *status, int *error); * -%ENOTSUPP if the SEV does not support SEV * -%ETIMEDOUT if the SEV command timed out * -%EIO if the SEV returned a non-zero return code - * -%EINVAL if the SEV file descriptor is not valid + * -%EBADF if the file pointer is bad or does not grant access */ int sev_issue_cmd_external_user(struct file *filep, unsigned int id, void *data, int *error); +/** + * file_is_sev - returns whether a file pointer is for the SEV device + * + * @filep - SEV device file pointer + */ +bool file_is_sev(struct file *filep); + /** * sev_guest_deactivate - perform SEV DEACTIVATE command * @@ -1039,6 +1046,8 @@ static inline int sev_guest_df_flush(int *error) { return -ENODEV; } static inline int sev_issue_cmd_external_user(struct file *filep, unsigned int id, void *data, int *error) { return -ENODEV; } +static inline bool file_is_sev(struct file *filep) { return false; } + static inline void *psp_copy_user_blob(u64 __user uaddr, u32 len) { return ERR_PTR(-EINVAL); } static inline void *snp_alloc_firmware_page(gfp_t mask) From patchwork Thu Nov 7 23:24:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dionna Amalie Glaze X-Patchwork-Id: 13867284 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3391221C189 for ; Thu, 7 Nov 2024 23:29:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731022142; cv=none; b=gdyZ2JiaHaqtgSjYmthZdrg9Qrm1xUo4y668oeIL7tM/megcLJhvGo+B6Eqqt0GRB+U/u3JJg9BcFWJd2iBrUPnTTzFpeZNtm0wHOO+bR2BL7VaMRJzoIKY9hZMV3Nuiav8bSq2Bo1B8M2Md+i5YQ4H9Q/HyswWFigBl6y26/3M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731022142; c=relaxed/simple; bh=6WWSW1vJ35TJDlbJJB5qq6nYTs9fwiNRbSVBdPgx3NQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=t2bncRw7crBGKqjXaxI7u5n14Ger3m9IWm6Papf40Rd++Fwv9LdHhWQ2A94nhHKGcBttL1Ih7WFdCVdgh9TGooLP+2kSxWYk3x6ArqPuRjgQ0KXn2TXrmEGmNS3H++nvHH/T11dTxfB7ObLtliFry8EE5+k3uOCbjB7odx6pD5I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dionnaglaze.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=t9n5jryM; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--dionnaglaze.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="t9n5jryM" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-71e7858820eso1602781b3a.2 for ; Thu, 07 Nov 2024 15:29:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731022140; x=1731626940; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Vd/jtJnQGf9IB3+kXNQoV6T2je6UfO6DAehiOVa2s+o=; b=t9n5jryMvuxkkLWzOEvxOf01La2djguOC4Dy8Pi7tlZFHGjTvc3fxMJXG70s6oFVdo LQKfu6pHNNaGvzbwgppBXuvphJU8kBe/+DE+Q3pV27iNIO9NUwxZkprJumGYosp2BELU tB5RZ1YgLlSGRWoXc8CdWsritPKklLh4GMjBpjExxdv33XtSYfv9dzdw0JzQ9AQqD5Qh LaRjJQN8dSeasmH3IIJodo84UiRgM7hQwcf1G08fBQN7HK/A+AbG45BE0sgfBiFKpSus TbXH+dckazAnqosLEulcJKWWVZghweLe+jGQrsm1A492gIV06nkEYJO+/7Qonbjxcysq 5SaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731022140; x=1731626940; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Vd/jtJnQGf9IB3+kXNQoV6T2je6UfO6DAehiOVa2s+o=; b=jLHmnpHgw/t3XgUOZ55hm05lNDC+JZjaLuX6fpAUj9yT8x4Dh95Ko5Wv0625S/G0T9 hw/dGGK0R4kbBFJySW2/2sXcDVoAdOEqE8PLN2w6PBdsAh3m9Q4k7v/ocfZ+0JZ42rac nmW6yp0vJLDTGg/FmMf7s4kMHQuLKaijSGyMhl+mnsx+FglbVuPktWwBIW9PW8lYUoDj jTg4ilVdEOMLUd1hToF5CHftU7uvsfK1vNTHPBHxAGMjgR8svhCUmm3vB/pqRl1kN2Wq IuJq/QbbMIFxtCImLpPniLl9tfjN9ki+nrYSM9ReN8qgnzzHzg0jcynMxPiQOFogQR1i 3/Lw== X-Forwarded-Encrypted: i=1; AJvYcCVM7WkqhDPJYStgaVw622yxWBqLvosTG69l1xzeTzs4c8GJRytAhFLS71RLug9y4yi5FncgUGxdmR6arpI=@vger.kernel.org X-Gm-Message-State: AOJu0YyBCtvrtrZJQHCiI7bLnMV0I1wsdGYbbZssa9hC9phQNwrDUGZn sxbjMOc9gvgOUt/Y7TMMwn+umPrhWilMzjQHcHTO0CpEV+UYiUP66UuAzwrrk7h4osOs+NF3dKm FXwJ4Antis+fLUx/u/8cILg== X-Google-Smtp-Source: AGHT+IG3FN+utE5DtZk1yqn4OdrPXgq7d6fljLLlDC+Gkt63/91sIHkgMPRqozoEz65kcTQskewlqi/wialMoW0yqA== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:36:e7b8:ac13:c9e8]) (user=dionnaglaze job=sendgmr) by 2002:a62:b401:0:b0:71e:5f55:86f1 with SMTP id d2e1a72fcca58-724133cd470mr5045b3a.5.1731022140524; Thu, 07 Nov 2024 15:29:00 -0800 (PST) Date: Thu, 7 Nov 2024 23:24:48 +0000 In-Reply-To: <20241107232457.4059785-1-dionnaglaze@google.com> Precedence: bulk X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241107232457.4059785-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog Message-ID: <20241107232457.4059785-9-dionnaglaze@google.com> Subject: [PATCH v5 08/10] KVM: SVM: move sev_issue_cmd_external_user to new API From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org, Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Ashish Kalra , Tom Lendacky , John Allen , Herbert Xu , "David S. Miller" Cc: linux-coco@lists.linux.dev, Dionna Glaze , Michael Roth , Luis Chamberlain , Russ Weight , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Tianfei zhang , Alexey Kardashevskiy , kvm@vger.kernel.org, linux-crypto@vger.kernel.org ccp now prefers all calls from external drivers to dominate all calls into the driver on behalf of a user with a successful sev_check_external_user call. CC: Sean Christopherson CC: Paolo Bonzini CC: Thomas Gleixner CC: Ingo Molnar CC: Borislav Petkov CC: Dave Hansen CC: Ashish Kalra CC: Tom Lendacky CC: John Allen CC: Herbert Xu CC: "David S. Miller" CC: Michael Roth CC: Luis Chamberlain CC: Russ Weight CC: Danilo Krummrich CC: Greg Kroah-Hartman CC: "Rafael J. Wysocki" CC: Tianfei zhang CC: Alexey Kardashevskiy Signed-off-by: Dionna Glaze --- arch/x86/kvm/svm/sev.c | 18 +++++++++++++++--- drivers/crypto/ccp/sev-dev.c | 12 ------------ include/linux/psp-sev.h | 27 --------------------------- 3 files changed, 15 insertions(+), 42 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index d0e0152aefb32..cea41b8cdabe4 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -528,21 +528,33 @@ static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error) return ret; } -static int __sev_issue_cmd(int fd, int id, void *data, int *error) +static int sev_check_external_user(int fd) { struct fd f; - int ret; + int ret = 0; f = fdget(fd); if (!fd_file(f)) return -EBADF; - ret = sev_issue_cmd_external_user(fd_file(f), id, data, error); + if (!file_is_sev(fd_file(f))) + ret = -EBADF; fdput(f); return ret; } +static int __sev_issue_cmd(int fd, int id, void *data, int *error) +{ + int ret; + + ret = sev_check_external_user(fd); + if (ret) + return ret; + + return sev_do_cmd(id, data, error); +} + static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index f92e6a222da8a..67f6425b7ed07 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -2493,18 +2493,6 @@ bool file_is_sev(struct file *p) } EXPORT_SYMBOL_GPL(file_is_sev); -int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, - void *data, int *error) -{ - int rc = file_is_sev(filep) ? 0 : -EBADF; - - if (rc) - return rc; - - return sev_do_cmd(cmd, data, error); -} -EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user); - void sev_pci_init(void) { struct sev_device *sev = psp_master->sev_data; diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index ed85c0cfcfcbe..b4164d3600702 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -860,30 +860,6 @@ int sev_platform_init(struct sev_platform_init_args *args); */ int sev_platform_status(struct sev_user_data_status *status, int *error); -/** - * sev_issue_cmd_external_user - issue SEV command by other driver with a file - * handle. - * - * This function can be used by other drivers to issue a SEV command on - * behalf of userspace. The caller must pass a valid SEV file descriptor - * so that we know that it has access to SEV device. - * - * @filep - SEV device file pointer - * @cmd - command to issue - * @data - command buffer - * @error: SEV command return code - * - * Returns: - * 0 if the SEV successfully processed the command - * -%ENODEV if the SEV device is not available - * -%ENOTSUPP if the SEV does not support SEV - * -%ETIMEDOUT if the SEV command timed out - * -%EIO if the SEV returned a non-zero return code - * -%EBADF if the file pointer is bad or does not grant access - */ -int sev_issue_cmd_external_user(struct file *filep, unsigned int id, - void *data, int *error); - /** * file_is_sev - returns whether a file pointer is for the SEV device * @@ -1043,9 +1019,6 @@ sev_guest_activate(struct sev_data_activate *data, int *error) { return -ENODEV; static inline int sev_guest_df_flush(int *error) { return -ENODEV; } -static inline int -sev_issue_cmd_external_user(struct file *filep, unsigned int id, void *data, int *error) { return -ENODEV; } - static inline bool file_is_sev(struct file *filep) { return false; } static inline void *psp_copy_user_blob(u64 __user uaddr, u32 len) { return ERR_PTR(-EINVAL); }