Support SEV firmware hotloading
Subsystems (8)
v2 cover Support SEV firmware hotloading
The SEV-SNP API specifies a command for hotloading the SEV firmware. when no SEV or SEV-ES guests are running. The firmware hotloading support is dependent on the firmware_upload API for better ease-of-use, and to not necessarily require SEV firmware hotloading support when building the ccp driver.
For safety, there are steps the kernel should take before allowing a firmware to be committed:
1. Writeback invalidate all.
2. Data fabric flush.
3. All GCTX pages must be updated successfully with SNP_GUEST_STATUS
The snp_context_create function had the possibility to leak GCTX pages, so the first patch fixes that bug in KVM.
The ccp driver must continue to be unloadable, so the second patch in this series fixes a cyclic refcount bug in firmware_loader.
The third patch adds SEV_CMD_DOWNLOAD_FIRMWARE_EX support with the required safety conditions and adds a new argument to the platform initialization arguments to delay legacy platform initialization. The command allows for firmware to be committed at the time of download, but due to the safety requirements, its better to leave that to a follow-up SNP_COMMIT command.
The fourth patch uses the new platform initialization argument when the vm_type is not legacy SEV/SEV-ES.
The bulk of this series is in the changes to ccp in the third patch.
The KVM_EXIT for requesting certificates on extended guest request is not part of this patch series. Any such support must be designed with races between SNP_COMMIT and servicing extended guest requests such that the REPORTED_TCB in an attestation_report always correctly corresponds to the certificates returned by the extended guest request handler.
Changes from v1:
- Fix double-free with incorrect goto label on error.
- checkpatch cleanup.
- firmware_loader comment cleanup and one-use local variable inlining.
Dionna Glaze (4):
kvm: svm: Fix gctx page leak on invalid inputs
firmware_loader: Move module refcounts to allow unloading
crypto: ccp: Add SNP firmware hotload support
KVM: SVM: Delay legacy platform initialization on SNP
arch/x86/kvm/svm/sev.c | 10 +- drivers/base/firmware_loader/sysfs_upload.c | 16 +- drivers/crypto/ccp/Kconfig | 2 + drivers/crypto/ccp/sev-dev.c | 398 +++++++++++++++++++- drivers/crypto/ccp/sev-dev.h | 14 + include/linux/psp-sev.h | 28 ++ include/uapi/linux/psp-sev.h | 5 + 7 files changed, 452 insertions(+), 21 deletions(-)
Signature · 1 line
2.47.0.163.g1226f6d8fa-goog
PATCH v2 1/4 kvm: svm: Fix gctx page leak on invalid inputs
Ensure that snp gctx page allocation is adequately deallocated on failure during snp_launch_start.
--- arch/x86/kvm/svm/sev.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/sev.c +4 -4
@@ -2212,10 +2212,6 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)if (sev->snp_context)return -EINVAL ; - sev->snp_context =snp_context_create (kvm, argp); -if (!sev->snp_context) -return -ENOTTY ; -if (params.flags)return -EINVAL ;
@@ -2230,6 +2226,10 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)if (params.policy &SNP_POLICY_MASK_SINGLE_SOCKET )return -EINVAL ; + sev->snp_context =snp_context_create (kvm, argp); +if (!sev->snp_context) +return -ENOTTY ; + start.gctx_paddr =__psp_pa (sev->snp_context); start.policy = params.policy;memcpy (start.gosvw, params.gosvw,sizeof (params.gosvw));
Signature · 1 line
2.47.0.163.g1226f6d8fa-goog
PATCH v2 2/4 firmware_loader: Move module refcounts to allow unloading
In reply to Dionna GlazeIf a kernel module registers a firmware upload API ops set, then it's unable to be moved due to effectively a cyclic reference that the module depends on the upload which depends on the module.
Instead, only require the try_module_get when an upload is requested to disallow unloading a module only while the upload is in progress.
--- drivers/base/firmware_loader/sysfs_upload.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/base/firmware_loader/sysfs_upload.c +7 -9
@@ -204,6 +204,7 @@ static void fw_upload_main(struct work_struct *work) fwlp->ops->cleanup (fwl); putdev_exit: +module_put (fwlp->module);put_device (fw_dev->parent);/*
@@ -239,6 +240,9 @@ int fw_upload_start(struct fw_sysfs *fw_sysfs) } fwlp = fw_sysfs->fw_upload_priv; +if (!try_module_get (fwlp->module))/* released in fw_upload_main */ +return -EFAULT ; +mutex_lock (&fwlp->lock);/* Do not interfere with an on-going fw_upload */
@@ -310,13 +314,10 @@ firmware_upload_register(struct module *module, struct device *parent,return ERR_PTR (-EINVAL ); } -if (!try_module_get (module)) -return ERR_PTR (-EFAULT ); - fw_upload =kzalloc (sizeof (*fw_upload),GFP_KERNEL );if (!fw_upload) { ret = -ENOMEM ; - goto exit_module_put; + goto exit_err; } fw_upload_priv =kzalloc (sizeof (*fw_upload_priv),GFP_KERNEL );
@@ -358,7 +359,7 @@ firmware_upload_register(struct module *module, struct device *parent,if (ret) {dev_err (fw_dev,"%s: device_register failed\n" , __func__);put_device (fw_dev); - goto exit_module_put; + goto exit_err; }return fw_upload;
@@ -372,8 +373,7 @@ firmware_upload_register(struct module *module, struct device *parent, free_fw_upload:kfree (fw_upload); -exit_module_put: -module_put (module); +exit_err:return ERR_PTR (ret); }
@@ -387,7 +387,6 @@ void firmware_upload_unregister(struct fw_upload *fw_upload) {struct fw_sysfs *fw_sysfs = fw_upload->priv;struct fw_upload_priv *fw_upload_priv = fw_sysfs->fw_upload_priv; -struct module *module = fw_upload_priv->module;mutex_lock (&fw_upload_priv->lock);if (fw_upload_priv->progress ==FW_UPLOAD_PROG_IDLE ) {
@@ -403,6 +402,5 @@ void firmware_upload_unregister(struct fw_upload *fw_upload) unregister:device_unregister (&fw_sysfs->dev); -module_put (module); }EXPORT_SYMBOL_GPL (firmware_upload_unregister);
Signature · 1 line
2.47.0.163.g1226f6d8fa-goog
Re: [PATCH v2 2/4] firmware_loader: Move module refcounts to allow unloading
Quoted 28 lines · Dionna Glaze
If a kernel module registers a firmware upload API ops set, then it's unable to be moved due to effectively a cyclic reference that the module depends on the upload which depends on the module.
Instead, only require the
try_module_getwhen an upload is requested to disallow unloading a module only while the upload is in progress.Fixes 97730bbb242c ("firmware_loader: Add firmware-upload support")CC Sean Christopherson <[email protected]>CC Paolo Bonzini <[email protected]>CC Thomas Gleixner <[email protected]>CC Ingo Molnar <[email protected]>CC Borislav Petkov <[email protected]>CC Dave Hansen <[email protected]>CC Ashish Kalra <[email protected]>CC Tom Lendacky <[email protected]>CC John Allen <[email protected]>CC Herbert Xu <[email protected]>CC "David S. Miller" <[email protected]>CC Michael Roth <[email protected]>CC Luis Chamberlain <[email protected]>CC Russ Weight <[email protected]>CC Danilo Krummrich <[email protected]>CC Greg Kroah-Hartman <[email protected]>CC "Rafael J. Wysocki" <[email protected]>CC Tianfei zhang <[email protected]>
Quoted 2 lines
Tested-by Ashish Kalra <[email protected]>Signed-off-by Dionna Glaze <[email protected]>
Quoted 78 lines
--- drivers/base/firmware_loader/sysfs_upload.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)drivers/base/firmware_loader/sysfs_upload.c +7
-9@@ -204,6 +204,7 @@ static void fw_upload_main(struct work_struct *work) fwlp->ops->cleanup (fwl); putdev_exit: +module_put (fwlp->module);put_device (fw_dev->parent);/* @@ -239,6 +240,9 @@ int fw_upload_start(struct fw_sysfs *fw_sysfs) } fwlp = fw_sysfs->fw_upload_priv; +if (!try_module_get (fwlp->module))/* released in fw_upload_main */ +return -EFAULT ; +mutex_lock (&fwlp->lock);/* Do not interfere with an on-going fw_upload */ @@ -310,13 +314,10 @@ firmware_upload_register(struct module *module, struct device *parent,return ERR_PTR (-EINVAL ); } -if (!try_module_get (module)) -return ERR_PTR (-EFAULT ); - fw_upload =kzalloc (sizeof (*fw_upload),GFP_KERNEL );if (!fw_upload) { ret = -ENOMEM ; - goto exit_module_put; + goto exit_err; } fw_upload_priv =kzalloc (sizeof (*fw_upload_priv),GFP_KERNEL );@@ -358,7 +359,7 @@ firmware_upload_register(struct module *module, struct device *parent,if (ret) {dev_err (fw_dev,"%s: device_register failed\n" , __func__);put_device (fw_dev); - goto exit_module_put; + goto exit_err; }return fw_upload;@@ -372,8 +373,7 @@ firmware_upload_register(struct module *module, struct device *parent, free_fw_upload:kfree (fw_upload); -exit_module_put: -module_put (module); +exit_err:return ERR_PTR (ret); }@@ -387,7 +387,6 @@ void firmware_upload_unregister(struct fw_upload *fw_upload) {struct fw_sysfs *fw_sysfs = fw_upload->priv;struct fw_upload_priv *fw_upload_priv = fw_sysfs->fw_upload_priv; -struct module *module = fw_upload_priv->module;mutex_lock (&fw_upload_priv->lock);if (fw_upload_priv->progress ==FW_UPLOAD_PROG_IDLE ) {@@ -403,6 +402,5 @@ void firmware_upload_unregister(struct fw_upload *fw_upload) unregister:device_unregister (&fw_sysfs->dev); -module_put (module); }EXPORT_SYMBOL_GPL (firmware_upload_unregister);Signature · 1 line
2.47.0.163.g1226f6d8fa-goog
PATCH v2 3/4 crypto: ccp: Add SNP firmware hotload support
In reply to Dionna Glazeuapi changes: The status values added are defined in AMD's SEV-SNP API documentation as new error modes for the DOWNLOAD_FIRMWARE_EX command.
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.
snp_asid_to_gctx_page_map:
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_edx(0x8000001f)-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.
Given that GCTX page creation and binding through the SNP_ACTIVATE command are separate, the creation operation also tracks pages that are yet to be bound to an asid.
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.
According to SEV-SNP API section 3.3 Firmware Updates, the DOWNLOAD_FIRMWARE_EX command is usable when the SNP firmware is in either UNINIT or INIT states, but SEV-legacy firmware must be in the UNINIT state. A sev_platform_init call when only SEV-SNP VMs are expected should set supports_download_firmware_ex to delay legacy initialization actions.
The firmware can only be hotloaded for SEV VMs when legacy initialization has not happened.
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.
--- drivers/crypto/ccp/Kconfig | 2 + drivers/crypto/ccp/sev-dev.c | 398 ++++++++++++++++++++++++++++++++++- drivers/crypto/ccp/sev-dev.h | 14 ++ include/linux/psp-sev.h | 28 +++ include/uapi/linux/psp-sev.h | 5 + 5 files changed, 439 insertions(+), 8 deletions(-)
drivers/crypto/ccp/Kconfig +2 -0
@@ -39,6 +39,8 @@ config CRYPTO_DEV_SP_PSP bool "Platform Security Processor (PSP) device" default y depends on CRYPTO_DEV_CCP_DD && X86_64 && AMD_IOMMU + select FW_LOADER + select FW_UPLOAD help Provide support for the AMD Platform Security Processor (PSP). The PSP is a dedicated processor that provides support for key
drivers/crypto/ccp/sev-dev.c +390 -8
464 lines not on this page · raw message
drivers/crypto/ccp/sev-dev.h +14 -0
@@ -57,6 +57,20 @@ struct sev_device {bool cmd_buf_backup_active;bool snp_initialized; + +#ifdef CONFIG_FW_UPLOAD +/* Lock to protect fw_cancel */ +struct mutex fw_lock; +struct fw_upload *fwl; +bool fw_cancel; + +/* Firmware update management. */ +u32 last_snp_asid; +bool synthetic_restore_required; +u64 *snp_asid_to_gctx_pages_map; +u64 *snp_unbound_gctx_pages; +u32 snp_unbound_gctx_end; +#endif /* CONFIG_FW_UPLOAD */ };int sev_dev_init (struct psp_device *psp);
include/linux/psp-sev.h +28 -0
@@ -16,6 +16,15 @@#define SEV_FW_BLOB_MAX_SIZE 0x4000/* 16KB */ +/* +* 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 +/** * SEV platform state */
@@ -185,6 +194,22 @@ 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 +* @address: physical address of firmware image +* @len: len of the firmware image +* @commit: automatically commit the newly installed image +*/ +struct sev_data_download_firmware_ex { +u32 cmdlen;/* In */ +u32 reserved;/* in */ +u64 address;/* In */ +u32 len;/* In */ +u32 commit:1 ;/* In */ +} __packed; +/** * struct sev_data_get_id - GET_ID command parameters *
@@ -797,10 +822,13 @@ struct sev_data_snp_shutdown_ex {* @probe: True if this is being called as part of CCP module probe, which * will defer SEV_INIT/SEV_INIT_EX firmware initialization until needed * unless psp_init_on_probe module param is set +* @supports_download_firmware_ex: True if legacy SEV/SEV-ES guests are not +* being launched via KVM */ struct sev_platform_init_args {int error;bool probe; +bool supports_download_firmware_ex; };/**
include/uapi/linux/psp-sev.h +5 -0
@@ -80,6 +80,11 @@ typedef enum {SEV_RET_INVALID_PAGE_OWNER ,SEV_RET_INVALID_PAGE_AEAD_OFLOW ,SEV_RET_RMP_INIT_REQUIRED , +SEV_RET_BAD_SVN , +SEV_RET_BAD_VERSION , +SEV_RET_SHUTDOWN_REQUIRED , +SEV_RET_UPDATE_FAILED , +SEV_RET_RESTORE_REQUIRED ,SEV_RET_MAX , } sev_ret_code;
Signature · 1 line
2.47.0.163.g1226f6d8fa-goog
PATCH v2 4/4 KVM: SVM: Delay legacy platform initialization on SNP
In reply to Dionna GlazeWhen no SEV or SEV-ES guests are active, then the firmware can be updated while (SEV-SNP) VM guests are active.
--- arch/x86/kvm/svm/sev.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/kvm/svm/sev.c +2 -0
@@ -445,6 +445,8 @@ static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp, goto e_no_asid; init_args.probe = false; + init_args.supports_download_firmware_ex = + vm_type !=KVM_X86_SEV_VM && vm_type !=KVM_X86_SEV_ES_VM ; ret =sev_platform_init (&init_args);if (ret) goto e_free;
Signature · 1 line
2.47.0.163.g1226f6d8fa-goog