From nobody Sun Nov 24 06:01:26 2024 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 0C191219C93 for ; Thu, 7 Nov 2024 23:28:53 +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=1731022135; cv=none; b=g6E632kjxhuqx8ev/P3JuUxz1eEOu6dBNkI17eQKwUm4OadORjtM+ZYGrVw0rk9KhJamKHTb3x/n2QIqiZ6TAM99Jg48/fuxjkIOjJAdcUyj+isezUtM2h5PrYrD2Jhi+WLgck3oMXRxxI6SOxn3DA4ey05XyZgVCUncfKCzs9M= 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.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="u/47isQX" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ea8a5e862eso19841777b3.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=JO/YBE/5GRE7Ox7D7CqSGYHmnlUEgKFIx/IN+ss7XinwHWjc2IjOXabYMcSnl88MfR TCmZf1sh7hsjrMQHu7Md8Fwo2cUZ4a/51PSCmFTDdWdfu1ow18jYqikCZVDDwksZJph/ r97Lqnz7HdX9C2Tr38Amo2NiI0NOG6Hva+HZ7kFDO5oOd8Oepgob5/L5v3KRsbWdm/s0 prmIuU+1wm3NiLAzoI1qHhdt+I12nwy9Ea87gUpRS+T0bEHlfd6EM1Ru1wvOEawVaMIn HxEMEj8EMNIn9TPOM8TpI87/nbH4IBk+xMKE4jnf7hooe9aKlyoxB5vYWxCxpsM4RGYA nIUA== X-Gm-Message-State: AOJu0Yw4Sv1P2lW+HCwlbbFSJC+nXQdkUDmj/N7O9lxRop2bwp7RgAKj P+YIDui3X+1xweMulxqYhwEjrC/4kG/TYDRi0MR3Ps3zcsnHGuoZq8V2F8pFeOI7PLGjlji6NWw 9OdrqVtEMsIr+h7JpDa882zGDI8qvP3iHzje0uROxjfk9Isco6LAlVuu1SPL9W+gRwyEdUNtHRc Dn9zTyH7NmGYUBTKgM/RqZo0lwm8WIoUSCxm4gJnBq0g/Rzbc/nGPXKRpoL/AYGRBe1Q4= 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-kernel@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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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; =20 +/* 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 =3D psp_master->sev_data; @@ -1093,6 +1097,81 @@ static int snp_filter_reserved_mem_regions(struct re= source *rs, void *arg) return 0; } =20 +void *sev_snp_create_context(int asid, int *psp_ret) +{ + struct sev_data_snp_addr data =3D {}; + 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 =3D snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT); + if (!context) + return ERR_PTR(-ENOMEM); + + data.address =3D __psp_pa(context); + rc =3D 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 =3D context; + + return context; +} + +int sev_snp_activate_asid(int asid, int *psp_ret) +{ + struct sev_data_snp_activate data =3D {0}; + void *context; + + if (!sev_asid_data) + return -ENODEV; + + context =3D sev_asid_data[asid].snp_context; + if (!context) + return -EINVAL; + + data.gctx_paddr =3D __psp_pa(context); + data.asid =3D 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 =3D {}; + struct sev_asid_data *data =3D &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 =3D __sme_pa(data->snp_context); + ret =3D 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 =3D NULL; + + return 0; +} + static int __sev_snp_init_locked(int *error) { struct psp_device *psp =3D psp_master; @@ -1306,6 +1385,27 @@ static int __sev_platform_init_locked(int *error) return 0; } =20 +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 =3D sev_max_asid + 1; + sev_es_max_asid =3D sev_min_asid - 1; + + sev_asid_data =3D 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_plat= form_init_args *args) if (sev->state =3D=3D SEV_STATE_INIT) return 0; =20 + rc =3D __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 =3D NULL; } =20 + kfree(sev_asid_data); + sev_asid_data =3D NULL; + __sev_snp_shutdown_locked(&error, panic); } =20 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); =20 +struct sev_asid_data { + void *snp_context; +}; + +/* Extern to be shared with firmware_upload API implementation if configur= ed. */ +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_decommissio= n *data, int *error); */ int sev_do_cmd(int cmd, void *data, int *psp_ret); =20 +/** + * 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 subse= quent 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 gues= t context page, and frees + * it. + * + * The caller must ensure mutual exclusion with any process that may deact= ivate 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); --=20 2.47.0.277.g8800431eea-goog