Implement SNP live firmware update support
Subsystems (18)
v4 cover Implement SNP live firmware update support
Introducing support for live update on AMD SEV-SNP platforms via DOWNLOAD_FIRMWARE_EX.
This patchset is an extension of the RFC patchset from Tycho Andersen.
This series also fronts firmware_loader patches from Dan Williams[1] which majorly cleans up refcount issues so that registering the upload interface no longer pins the module. Without which having live firmware update will cause failure to reload the ccp module as well as break kexec.
Patches based on cryptodev-2.6
v4:
* Collected reviewed-by tags
* Cleanup snp_alloc_firmware_pages() error handing - Tom
* Cleanups to comments and code formatting - Tom
* Change dev prints to ratelimited - Tom
* Add comment about releasing sev_release_firmware_buffers() - Tom
* Add FW_UPLOAD and FW_LOADER to CRYPTO_DEV_SP_PSP config and clean up #ifdef-erry - Shantanu
* On failure of sev_get_api_version() return an error only if PSP failed in all other cases DLFW_EX succeeded - Shantanu
* Free TMR if we were already in UNINIT too - Local Sashiko instance
Responding to Sashiko review:
* Possible RMP violation when live fireware update races with snp_get_platform_data()
- Both the dlfw_ex and snp_get_platform_data() are called within a mutex guard so feat_info can only be accessed once it is no longer in the firmware owned state and the mutex is dropped.
* Register-unregister failure
- The ccp PCI driver probes synchronously, so psp_master is set before sev_pci_init() runs. The uploader follows the same lifecycle as the rest of the SEV/SNP initialization, including the TSM and verify-mitigation interfaces. The unregister in sev_dev_destroy() is needed so the upload device doesn't outlive sev.
v3: https://lore.kernel.org/linux-crypto/[email protected]/
* Print psp return codes along with driver returns for easier parsing - Shantanu
* Clean up error codes to map the right firmware/driver error condition to the firmware error codes. - Shantanu
* Report -ETIMEOUT case by checking for psp_dead after DLFW_EX command is called instead of reporting unknown error - Shantanu
* __sev_do_snp_platform_status() call can race and incorrect cached platform states may be supplied. Guard the call under a mutex - Sashiko
* firmware unregister can race when called from sev_dev_destroy() or sev_pci_exit(). Guard the call under a mutex - Sashiko
v2: https://lore.kernel.org/lkml/[email protected]/
* In snp_get_platform_data() use snp_[alloc|free]_firmware_page() to transition pages to and from firmware-owned state, instead of open-coding rmp_mark_pages_firmware()/snp_reclaim_pages() around the SNP_FEATURE_INFO command - Tom, Sashiko
* Claim sev->fwl atomically with xchg() in unregister_sev_fw_uploader() so a concurrent module removal and sysfs unbind cannot both reach firmware_upload_unregister() - Sashiko
Responding to other Sashiko comments:
* __sev_release_firmware_buffers() called with panic=true can cause deadlock as another stopped CPU might currently hold zone->lock or other page allocator locks.
- I think having a panic param within __snp_free_firmware_pages which checks and returns before __free_pages() should resolve this issue. Since this is a pre-existing issue not relevant to this patchset, plan to send this as a seperate patch
* The sev_download_firmware_ex() function is called with the lock held and performs a GFP_KERNEL allocation. Since GFP_KERNEL can trigger direct memory reclaim, could this invoke SEV page reclaim operations that attempt to acquire the already-held sev_cmd_mutex, resulting in a deadlocked state?
- GFP_KERNEL may perform direct MM reclaim, but SNP page reclamation is not registered as an MM shrinker or normal page-reclaim callback. snp_reclaim_pages() acquires sev_cmd_mutex only when explicitly called with locked=false
* "If snp_reclaim_cmd_buf() fails and returns early, the code skips resetting sev->cmd_buf_active and sev->cmd_buf_backup_active. Will this permanently leak the bounce buffer slots and result in future -EBUSY failures?"
- Since the command buffer may still be firmware owned, I think declaring psp_dead = true is the way to ensure we do not make it available for reuse after a failed reclaim. Pre-existing issue, plan to send this as a seperate patch
v1: https://lore.kernel.org/linux-crypto/[email protected]/
* firmware-loader patches are rebased as-is and only incorporates fixes for minor build issues
* Dropped crypto/ccp: Hoist kernel part of SNP_PLATFORM_STATUS as that patch has been merged since
* Dropped crypto/ccp: Reclaim command buffer when the PSP dies and subsequent handling since firmware quirk is now resolved and need not be handled in the OS
* Use guard(mutex) for the locked region, which the split makes possible since sev_get_api_version() acquires sev_cmd_mutex - Maxwell
* Added a patch factoring out the TMR and INIT_EX teardown, so the update path and __sev_firmware_shutdown() share it - Shantanu
* Skip the re-init when the PSP is dead, not just when a rollback is outstanding - Shantanu
* Re-initialize the platform before refreshing the cached status - Shantanu
* Split the monolithic .write into a locked update helper, an error translation helper, and a thin .write that refreshes the cached status once the lock is dropped and clean up various allocations
* Convert rollback required and reinit required globals to reside in sev_dev struct to avoid carrying over states if manually reloaded
RFC: https://lore.kernel.org/all/[email protected]/
[1]: https://lore.kernel.org/lkml/[email protected]/
Dan Williams (3):
firmware_loader: Stop pinning modules on registration
firmware_loader: Stop pinning parent device per workqueue invocation
treewide: firmware_loader: Drop the unused @module argument
Pratik R. Sampat (4):
crypto: ccp - Factor out the release of the SEV firmware buffers
crypto: ccp - Allow SNP platform data to be queried after SNP INIT crypto/ccp: Register with fw_uploader and always fail crypto/ccp: Implement SNP Download Firmware EX
.../driver-api/firmware/fw_upload.rst | 2 +- drivers/base/firmware_loader/sysfs_upload.c | 50 +- drivers/base/firmware_loader/sysfs_upload.h | 1 - drivers/crypto/ccp/Kconfig | 2 + drivers/crypto/ccp/sev-dev.c | 448 ++++++++++++++++-- drivers/crypto/ccp/sev-dev.h | 4 + drivers/cxl/core/memdev.c | 4 +- drivers/firmware/microchip/mpfs-auto-update.c | 2 +- drivers/fpga/intel-m10-bmc-sec-update.c | 4 +- drivers/greybus/gb-beagleplay.c | 2 +- drivers/media/i2c/thp7312.c | 2 +- drivers/net/pse-pd/pd692x0.c | 4 +- drivers/virt/coco/tdx-host/tdx-host.c | 4 +- include/linux/firmware.h | 15 +- include/linux/psp-sev.h | 19 + lib/test_firmware.c | 3 +- 16 files changed, 475 insertions(+), 91 deletions(-)
Signature · 1 line
2.43.0
PATCH v4 1/7 firmware_loader: Stop pinning modules on registration
From: Dan Williams <[email protected]>
The module reference counting can result in callers pinning themselves in a circular loop. The module reference counting is unnecessary. firmware_upload_unregister() must be able to guarantee that all ops are idle at return.
All ops are either called from sysfs or the workqueue, so unregister sysfs to stop submissions, cancel any started transfers, flush cancelled transfers, and then release the device.
This also solves a theoretical race of new submissions starting between flush_work() and device_unregister(). The module reference was not protecting against that race.
---
v3..v4:
No change
--- drivers/base/firmware_loader/sysfs_upload.c | 31 ++++++++++----------- 1 file changed, 14 insertions(+), 17 deletions(-)
drivers/base/firmware_loader/sysfs_upload.c +14 -17
@@ -312,14 +312,9 @@ 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_obj (*fw_upload); -if (!fw_upload) { - ret = -ENOMEM ; - goto exit_module_put; - } +if (!fw_upload) +return ERR_PTR (-ENOMEM ); fw_upload_priv =kzalloc_obj (*fw_upload_priv);if (!fw_upload_priv) {
@@ -361,7 +356,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; +return ERR_PTR (ret); }return fw_upload;
@@ -372,9 +367,6 @@ firmware_upload_register(struct module *module, struct device *parent, free_fw_upload:kfree (fw_upload); -exit_module_put: -module_put (module); -return ERR_PTR (ret); }EXPORT_SYMBOL_GPL (firmware_upload_register);
@@ -386,23 +378,28 @@ EXPORT_SYMBOL_GPL(firmware_upload_register);void firmware_upload_unregister (struct fw_upload *fw_upload) {struct fw_sysfs *fw_sysfs = fw_upload->priv; +struct device *parent = fw_sysfs->dev.parent;struct fw_upload_priv *fw_upload_priv = fw_sysfs->fw_upload_priv; -struct module *module = fw_upload_priv->module; + +/* hold a parent reference while child is unregistered */ +get_device (parent); + +/* shutdown the sysfs interface to block new requests */ +device_del (&fw_sysfs->dev);mutex_lock (&fw_upload_priv->lock);if (fw_upload_priv->progress ==FW_UPLOAD_PROG_IDLE ) {mutex_unlock (&fw_upload_priv->lock); - goto unregister; + goto release; } fw_upload_priv->ops->cancel (fw_upload);mutex_unlock (&fw_upload_priv->lock); +release:/* Ensure lower-level device-driver is finished */ flush_work (&fw_upload_priv->work); - -unregister: -device_unregister (&fw_sysfs->dev); -module_put (module); +put_device (&fw_sysfs->dev); +put_device (parent); }EXPORT_SYMBOL_GPL (firmware_upload_unregister);
Signature · 1 line
2.43.0
PATCH v4 2/7 firmware_loader: Stop pinning parent device per workqueue invocation
In reply to Pratik R. SampatFrom: Dan Williams <[email protected]>
The device core pins parent devices while children are registered. As long as all usage of the parent device by the firmware_loader ends at firmware_upload_unregister(), no per queue_work() reference is needed.
Now that firmware_upload_unregister() holds its own parent device reference over the child device_del() and flush_work() events, the per queue_work() reference can be deleted.
---
v3..v4:
No change
--- drivers/base/firmware_loader/sysfs_upload.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/base/firmware_loader/sysfs_upload.c +2 -7
@@ -176,7 +176,7 @@ static void fw_upload_main(struct work_struct *work) ret = fwlp->ops->prepare (fwl, fwlp->data, fwlp->remaining_size);if (ret !=FW_UPLOAD_ERR_NONE ) {fw_upload_set_error (fwlp, ret); - goto putdev_exit; + goto out; }fw_upload_update_progress (fwlp,FW_UPLOAD_PROG_TRANSFERRING );
@@ -205,9 +205,7 @@ static void fw_upload_main(struct work_struct *work)if (fwlp->ops->cleanup) fwlp->ops->cleanup (fwl); -putdev_exit: -put_device (fw_dev->parent); - +out:/* * Note: fwlp->remaining_size is left unmodified here to provide * additional information on errors. It will be reinitialized when
@@ -228,7 +226,6 @@ static void fw_upload_main(struct work_struct *work)int fw_upload_start (struct fw_sysfs *fw_sysfs) {struct fw_priv *fw_priv = fw_sysfs->fw_priv; -struct device *fw_dev = &fw_sysfs->dev;struct fw_upload_priv *fwlp;if (!fw_sysfs->fw_upload_priv)
@@ -249,8 +246,6 @@ int fw_upload_start(struct fw_sysfs *fw_sysfs)return -EBUSY ; } -get_device (fw_dev->parent);/* released in fw_upload_main */ - fwlp->progress =FW_UPLOAD_PROG_RECEIVING ; fwlp->err_code =0 ; fwlp->remaining_size = fw_priv->size;
Signature · 1 line
2.43.0
PATCH v4 3/7 treewide: firmware_loader: Drop the unused @module argument
In reply to Pratik R. SampatFrom: Dan Williams <[email protected]>
Now that the firmware loader properly ceases all operations at firmware_upload_unregister() and no longer takes module references, clean up the unused parameter.
[[email protected]: drop leftover fw_upload_priv->module assignment
and the unused @module field from struct fw_upload_priv]
[[email protected]: convert the tdx-host caller added since the original
posting]
---
v3..v4:
No change
--- Documentation/driver-api/firmware/fw_upload.rst | 2 +- drivers/base/firmware_loader/sysfs_upload.c | 10 ++++------ drivers/base/firmware_loader/sysfs_upload.h | 1 - drivers/cxl/core/memdev.c | 4 ++-- drivers/firmware/microchip/mpfs-auto-update.c | 2 +- drivers/fpga/intel-m10-bmc-sec-update.c | 4 ++-- drivers/greybus/gb-beagleplay.c | 2 +- drivers/media/i2c/thp7312.c | 2 +- drivers/net/pse-pd/pd692x0.c | 4 ++-- drivers/virt/coco/tdx-host/tdx-host.c | 4 ++-- include/linux/firmware.h | 15 +++++++-------- lib/test_firmware.c | 3 +-- 12 files changed, 24 insertions(+), 29 deletions(-)
Documentation/driver-api/firmware/fw_upload.rst +1 -1
@@ -57,7 +57,7 @@ function calls firmware_upload_unregister() such as:: len = (truncate) ? truncate - fw_name : strlen(fw_name); sec->fw_name = kmemdup_nul(fw_name, len, GFP_KERNEL); - fwl = firmware_upload_register(THIS_MODULE, sec->dev, sec->fw_name, + fwl = firmware_upload_register(sec->dev, sec->fw_name, &m10bmc_ops, sec); if (IS_ERR(fwl)) { dev_err(sec->dev, "Firmware Upload driver failed to start\n");
drivers/base/firmware_loader/sysfs_upload.c +4 -6
@@ -273,7 +273,6 @@ void fw_upload_free(struct fw_sysfs *fw_sysfs)/** * firmware_upload_register() - register for the firmware upload sysfs API -* @module: kernel module of this device * @parent: parent device instantiating firmware upload * @name: firmware name to be associated with this device * @ops: pointer to structure of firmware upload ops
@@ -285,10 +284,10 @@ void fw_upload_free(struct fw_sysfs *fw_sysfs)* Return: struct fw_upload pointer or ERR_PTR() * **/ -struct fw_upload * -firmware_upload_register (struct module *module,struct device *parent, -const char *name,const struct fw_upload_ops *ops, -void *dd_handle) +struct fw_upload *firmware_upload_register (struct device *parent, +const char *name, +const struct fw_upload_ops *ops, +void *dd_handle) {u32 opt_flags =FW_OPT_NOCACHE ;struct fw_upload *fw_upload;
@@ -320,7 +319,6 @@ firmware_upload_register(struct module *module, struct device *parent, fw_upload_priv->fw_upload = fw_upload; fw_upload_priv->ops = ops;mutex_init (&fw_upload_priv->lock); - fw_upload_priv->module = module; fw_upload_priv->name = name; fw_upload_priv->err_code =0 ; fw_upload_priv->progress =FW_UPLOAD_PROG_IDLE ;
drivers/base/firmware_loader/sysfs_upload.h +0 -1
@@ -26,7 +26,6 @@ enum fw_upload_prog {struct fw_upload_priv {struct fw_upload *fw_upload; -struct module *module;const char *name;const struct fw_upload_ops *ops;struct mutex lock;/* protect data structure contents */
drivers/cxl/core/memdev.c +2 -2
@@ -1151,8 +1151,8 @@ int devm_cxl_setup_fw_upload(struct device *host, struct cxl_memdev_state *mds)if (!test_bit (CXL_MEM_COMMAND_ID_GET_FW_INFO , cxl_mbox->enabled_cmds))return 0 ; - fwl =firmware_upload_register (THIS_MODULE , dev,dev_name (dev), - &cxl_memdev_fw_ops, mds); + fwl =firmware_upload_register (dev,dev_name (dev), &cxl_memdev_fw_ops, + mds);if (IS_ERR (fwl))return PTR_ERR (fwl);return devm_add_action_or_reset (host, cxl_remove_fw_upload, fwl);
drivers/firmware/microchip/mpfs-auto-update.c +1 -1
@@ -437,7 +437,7 @@ static int mpfs_auto_update_probe(struct platform_device *pdev)return dev_err_probe (dev, ret,"The current bitstream does not support auto-update\n" ); - fw_uploader =firmware_upload_register (THIS_MODULE , dev,"mpfs-auto-update" , + fw_uploader =firmware_upload_register (dev,"mpfs-auto-update" , &mpfs_auto_update_ops, priv);if (IS_ERR (fw_uploader)) return dev_err_probe(dev, PTR_ERR(fw_uploader),
drivers/fpga/intel-m10-bmc-sec-update.c +2 -2
@@ -712,8 +712,8 @@ static int m10bmc_sec_probe(struct platform_device *pdev) goto fw_name_fail; } - fwl =firmware_upload_register (THIS_MODULE , sec->dev, sec->fw_name, - &m10bmc_ops, sec); + fwl =firmware_upload_register (sec->dev, sec->fw_name, &m10bmc_ops, + sec);if (IS_ERR (fwl)) {dev_err (sec->dev,"Firmware Upload driver failed to start\n" ); ret =PTR_ERR (fwl);
drivers/greybus/gb-beagleplay.c +1 -1
@@ -1172,7 +1172,7 @@ static int gb_fw_init(struct gb_beagleplay *bg)return PTR_ERR (desc); bg->rst_gpio = desc; - fwl =firmware_upload_register (THIS_MODULE , &bg->sd->dev,"cc1352p7" , + fwl =firmware_upload_register (&bg->sd->dev,"cc1352p7" , &cc1352_bootloader_ops, bg);if (IS_ERR (fwl))return PTR_ERR (fwl);
drivers/media/i2c/thp7312.c +1 -1
@@ -1909,7 +1909,7 @@ static int thp7312_register_flash_mode(struct thp7312_device *thp7312) goto error; } - fwl =firmware_upload_register (THIS_MODULE , dev,"thp7312-firmware" , + fwl =firmware_upload_register (dev,"thp7312-firmware" , &thp7312_fw_upload_ops, thp7312);if (IS_ERR (fwl)) { ret =PTR_ERR (fwl);
drivers/net/pse-pd/pd692x0.c +2 -2
@@ -1841,8 +1841,8 @@ static int pd692x0_i2c_probe(struct i2c_client *client)return dev_err_probe (dev, ret,"failed to register PSE controller\n" ); - fwl =firmware_upload_register (THIS_MODULE , dev,dev_name (dev), - &pd692x0_fw_ops, priv); + fwl =firmware_upload_register (dev,dev_name (dev), &pd692x0_fw_ops, + priv);if (IS_ERR (fwl))return dev_err_probe (dev,PTR_ERR (fwl),"failed to register to the Firmware Upload API\n" );
drivers/virt/coco/tdx-host/tdx-host.c +2 -2
@@ -194,8 +194,8 @@ static int seamldr_init(struct device *dev)if (!supports_runtime_update ())return 0 ; - tdx_fwl =firmware_upload_register (THIS_MODULE , dev,"tdx_module" , - &tdx_fw_ops,NULL ); + tdx_fwl =firmware_upload_register (dev,"tdx_module" , &tdx_fw_ops, +NULL );if (IS_ERR (tdx_fwl))return PTR_ERR (tdx_fwl);
include/linux/firmware.h +7 -8
@@ -197,20 +197,19 @@ static inline int request_partial_firmware_into_buf#ifdef CONFIG_FW_UPLOAD -struct fw_upload * -firmware_upload_register (struct module *module,struct device *parent, -const char *name,const struct fw_upload_ops *ops, -void *dd_handle); +struct fw_upload *firmware_upload_register (struct device *parent, +const char *name, +const struct fw_upload_ops *ops, +void *dd_handle);void firmware_upload_unregister (struct fw_upload *fw_upload);#else static inline struct fw_upload * -firmware_upload_register (struct module *module,struct device *parent, -const char *name,const struct fw_upload_ops *ops, -void *dd_handle) +firmware_upload_register (struct device *parent,const char *name, +const struct fw_upload_ops *ops,void *dd_handle) { -return ERR_PTR (-EINVAL ); +return ERR_PTR (-EINVAL ); }static inline void firmware_upload_unregister (struct fw_upload *fw_upload)
lib/test_firmware.c +1 -2
@@ -1322,8 +1322,7 @@ static ssize_t upload_register_store(struct device *dev, goto free_tst; } - fwl =firmware_upload_register (THIS_MODULE , dev, tst->name, - &upload_test_ops, tst); + fwl =firmware_upload_register (dev, tst->name, &upload_test_ops, tst);if (IS_ERR (fwl)) { ret =PTR_ERR (fwl); goto free_buf;
Signature · 1 line
2.43.0
PATCH v4 4/7 crypto: ccp - Factor out the release of the SEV firmware buffers
In reply to Pratik R. Sampat__sev_firmware_shutdown() reclaims and frees the TMR and the INIT_EX NV area once the platform has been taken down. Move that into a helper so that other paths which de-initialize the platform can reuse the same path.
No functional change intended.
---
v3..v4:
No change
--- drivers/crypto/ccp/sev-dev.c | 55 ++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 25 deletions(-)
drivers/crypto/ccp/sev-dev.c +30 -25
@@ -1881,6 +1881,35 @@ static int __sev_platform_shutdown_locked(int *error)return ret; } +static void __sev_release_firmware_buffers (bool panic) +{ +if (sev_es_tmr) { +/* +* The TMR area was encrypted, flush it from the cache. +* +* If invoked during panic handling, local interrupts are +* disabled and all CPUs are stopped, so wbinvd_on_all_cpus() +* can't be used. In that case, wbinvd() is done on remote CPUs +* via the NMI callback, and done for this CPU later during +* SNP shutdown, so wbinvd_on_all_cpus() can be skipped. +*/ +if (!panic) +wbinvd_on_all_cpus (); + +__snp_free_firmware_pages (virt_to_page (sev_es_tmr), +get_order (sev_es_tmr_size), + true); + sev_es_tmr =NULL ; + } + +if (sev_init_ex_buffer) { +__snp_free_firmware_pages (virt_to_page (sev_init_ex_buffer), +get_order (NV_LENGTH ), + true); + sev_init_ex_buffer =NULL ; + } +} +static int sev_get_platform_state (int *state,int *error) {struct sev_user_data_status data;
@@ -2906,31 +2935,7 @@ static void __sev_firmware_shutdown(struct sev_device *sev, bool panic)__sev_platform_shutdown_locked (&error); -if (sev_es_tmr) { -/* -* The TMR area was encrypted, flush it from the cache. -* -* If invoked during panic handling, local interrupts are -* disabled and all CPUs are stopped, so wbinvd_on_all_cpus() -* can't be used. In that case, wbinvd() is done on remote CPUs -* via the NMI callback, and done for this CPU later during -* SNP shutdown, so wbinvd_on_all_cpus() can be skipped. -*/ -if (!panic) -wbinvd_on_all_cpus (); - -__snp_free_firmware_pages (virt_to_page (sev_es_tmr), -get_order (sev_es_tmr_size), - true); - sev_es_tmr =NULL ; - } - -if (sev_init_ex_buffer) { -__snp_free_firmware_pages (virt_to_page (sev_init_ex_buffer), -get_order (NV_LENGTH ), - true); - sev_init_ex_buffer =NULL ; - } +__sev_release_firmware_buffers (panic);__sev_snp_shutdown_locked (&error, panic); }
Signature · 1 line
2.43.0
Re: [Patch v4 4/7] crypto: ccp - Factor out the release of the SEV firmware buffers
Quoted 9 lines · Pratik R. Sampat
__sev_firmware_shutdown()reclaims and frees the TMR and theINIT_EXNV area once the platform has been taken down. Move that into a helper so that other paths which de-initialize the platform can reuse the same path.No functional change intended.
Suggested-by Shantanu Sinha <[email protected]>Signed-off-by Pratik R. Sampat <[email protected]>
Quoted 80 lines
---
v3..v4:
No change--- drivers/crypto/ccp/sev-dev.c | 55 ++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 25 deletions(-)drivers/crypto/ccp/sev-dev.c +30
-25@@ -1881,6 +1881,35 @@ static int __sev_platform_shutdown_locked(int *error)return ret; } +static void __sev_release_firmware_buffers (bool panic) +{ +if (sev_es_tmr) { +/* +* The TMR area was encrypted, flush it from the cache. +* +* If invoked during panic handling, local interrupts are +* disabled and all CPUs are stopped, so wbinvd_on_all_cpus() +* can't be used. In that case, wbinvd() is done on remote CPUs +* via the NMI callback, and done for this CPU later during +* SNP shutdown, so wbinvd_on_all_cpus() can be skipped. +*/ +if (!panic) +wbinvd_on_all_cpus (); + +__snp_free_firmware_pages (virt_to_page (sev_es_tmr), +get_order (sev_es_tmr_size), + true); + sev_es_tmr =NULL ; + } + +if (sev_init_ex_buffer) { +__snp_free_firmware_pages (virt_to_page (sev_init_ex_buffer), +get_order (NV_LENGTH ), + true); + sev_init_ex_buffer =NULL ; + } +} +static int sev_get_platform_state (int *state,int *error) {struct sev_user_data_status data;@@ -2906,31 +2935,7 @@ static void __sev_firmware_shutdown(struct sev_device *sev, bool panic)__sev_platform_shutdown_locked (&error); -if (sev_es_tmr) { -/* -* The TMR area was encrypted, flush it from the cache. -* -* If invoked during panic handling, local interrupts are -* disabled and all CPUs are stopped, so wbinvd_on_all_cpus() -* can't be used. In that case, wbinvd() is done on remote CPUs -* via the NMI callback, and done for this CPU later during -* SNP shutdown, so wbinvd_on_all_cpus() can be skipped. -*/ -if (!panic) -wbinvd_on_all_cpus (); - -__snp_free_firmware_pages (virt_to_page (sev_es_tmr), -get_order (sev_es_tmr_size), - true); - sev_es_tmr =NULL ; - } - -if (sev_init_ex_buffer) { -__snp_free_firmware_pages (virt_to_page (sev_init_ex_buffer), -get_order (NV_LENGTH ), - true); - sev_init_ex_buffer =NULL ; - } +__sev_release_firmware_buffers (panic);__sev_snp_shutdown_locked (&error, panic); }
PATCH v4 5/7 crypto: ccp - Allow SNP platform data to be queried after SNP INIT
In reply to Pratik R. SampatIn preparation for refreshing the cached SNP platform status and feature information after a successful firmware live update from DOWNLOAD_FIRMWARE_EX, allow snp_get_platform_data() to be called while the SNP firmware is in the INIT state.
Once SNP is initialized the firmware requires the output page of both commands to be firmware-owned. sev->snp_plat_status cannot satisfy that as it is embedded in struct sev_device, so use __sev_do_snp_platform_status(), which stages the output through a dedicated page. Allocate the SNP_FEATURE_INFO output page with __snp_alloc_firmware_pages(), which transitions the page to firmware-owned when SNP is initialized, and release it with __snp_free_firmware_pages(), which reclaims it before freeing.
Typically this was only ever exercised during initialization. However DOWNLOAD_FIRMWARE_EX can change that. Live firmware update can race with IOCTLs such as SEV_FACTORY_RESET which could cause operations performed on incorrect cached platform states. Avoid this race by wrapping the function under the sev_cmd_mutex.
---
v3..v4:
* Collected reviewed-by tags
* Simplify snp_alloc_firmware_pages() error handing - Tom
--- drivers/crypto/ccp/sev-dev.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)
drivers/crypto/ccp/sev-dev.c +10 -13
@@ -131,6 +131,8 @@ static void __sev_firmware_shutdown(struct sev_device *sev, bool panic);static int snp_shutdown_on_panic (struct notifier_block *nb,unsigned long reason,void *arg); +static int __sev_do_snp_platform_status (struct sev_user_data_snp_status *status, +int *error);static struct notifier_block snp_panic_notifier = { .notifier_call = snp_shutdown_on_panic,
@@ -1261,19 +1263,12 @@ static int snp_get_platform_data(struct sev_device *sev, int *error) {struct sev_data_snp_feature_info snp_feat_info;struct snp_feature_info *feat_info; -struct sev_data_snp_addr buf;struct page *page;int rc; -/* -* This function is expected to be called before SNP is -* initialized. -*/ -if (sev->snp_initialized) -return -EINVAL ; +lockdep_assert_held (&sev_cmd_mutex); - buf.address =__psp_pa (&sev->snp_plat_status); - rc =sev_do_cmd (SEV_CMD_SNP_PLATFORM_STATUS , &buf, error); + rc =__sev_do_snp_platform_status (&sev->snp_plat_status, error);if (rc) {dev_err (sev->dev,"SNP PLATFORM_STATUS command failed, ret = %d, error = %#x\n" , rc, *error);
@@ -1297,7 +1292,7 @@ static int snp_get_platform_data(struct sev_device *sev, int *error)* command to ensure structure is 8-byte aligned, and does not * cross a page boundary. */ - page =alloc_page (GFP_KERNEL ); + page =__snp_alloc_firmware_pages (GFP_KERNEL ,0 , true);if (!page)return -ENOMEM ;
@@ -1306,14 +1301,14 @@ static int snp_get_platform_data(struct sev_device *sev, int *error) snp_feat_info.ecx_in =0 ; snp_feat_info.feature_info_paddr =__psp_pa (feat_info); - rc =sev_do_cmd (SEV_CMD_SNP_FEATURE_INFO , &snp_feat_info, error); + rc =__sev_do_cmd_locked (SEV_CMD_SNP_FEATURE_INFO , &snp_feat_info, error);if (!rc) sev->snp_feat_info_0 = *feat_info;else dev_err (sev->dev,"SNP FEATURE_INFO command failed, ret = %d, error = %#x\n" , rc, *error); -__free_page (page); +__snp_free_firmware_pages (page,0 , true);return rc; }
@@ -2097,6 +2092,8 @@ static int sev_get_api_version(void)struct sev_user_data_status status;int error =0 , ret; +guard (mutex)(&sev_cmd_mutex); +/* * Cache SNP platform status and SNP feature information * if SNP is available.
@@ -2107,7 +2104,7 @@ static int sev_get_api_version(void)return 1 ; } - ret =sev_platform_status (&status, &error); + ret =__sev_do_cmd_locked (SEV_CMD_PLATFORM_STATUS , &status, &error);if (ret) {dev_err (sev->dev,"SEV: failed to get status. Error: %#x\n" , error);
Signature · 1 line
2.43.0
Tom Lendacky
maintainerRe: [Patch v4 5/7] crypto: ccp - Allow SNP platform data to be queried after SNP INIT
Quoted 20 lines · Pratik R. Sampat
In preparation for refreshing the cached SNP platform status and feature information after a successful firmware live update from
DOWNLOAD_FIRMWARE_EX, allowsnp_get_platform_data()to be called while the SNP firmware is in the INIT state.Once SNP is initialized the firmware requires the output page of both commands to be firmware-owned.
sev->snp_plat_statuscannot satisfy that as it is embedded in structsev_device, so use__sev_do_snp_platform_status(), which stages the output through a dedicated page. Allocate theSNP_FEATURE_INFOoutput page with__snp_alloc_firmware_pages(), which transitions the page to firmware-owned when SNP is initialized, and release it with__snp_free_firmware_pages(), which reclaims it before freeing.Typically this was only ever exercised during initialization. However
DOWNLOAD_FIRMWARE_EXcan change that. Live firmware update can race with IOCTLs such asSEV_FACTORY_RESETwhich could cause operations performed on incorrect cached platform states. Avoid this race by wrapping the function under thesev_cmd_mutex.
Quoted 4 lines
Reviewed-by Tom Lendacky <[email protected]>Co-developed-by Tycho Andersen (AMD) <[email protected]>Signed-off-by Tycho Andersen (AMD) <[email protected]>Signed-off-by Pratik R. Sampat <[email protected]>
In light of this series:
https://lore.kernel.org/linux-crypto/[email protected]/T/#m43170233cc3ba3c53b319427b404564e2c8fe70c
you may want to verify that the guard() that has been added to sev_get_api_version() is ok. Just a quick reply if everything looks ok.
Thanks,
Tom
Quoted 88 lines
---
v3..v4:* Collected reviewed-by tags
* Simplifysnp_alloc_firmware_pages()error handing - Tom--- drivers/crypto/ccp/sev-dev.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)drivers/crypto/ccp/sev-dev.c +10
-13@@ -131,6 +131,8 @@ static void __sev_firmware_shutdown(struct sev_device *sev, bool panic);static int snp_shutdown_on_panic (struct notifier_block *nb,unsigned long reason,void *arg); +static int __sev_do_snp_platform_status (struct sev_user_data_snp_status *status, +int *error);static struct notifier_block snp_panic_notifier = { .notifier_call = snp_shutdown_on_panic,@@ -1261,19 +1263,12 @@ static int snp_get_platform_data(struct sev_device *sev, int *error) {struct sev_data_snp_feature_info snp_feat_info;struct snp_feature_info *feat_info; -struct sev_data_snp_addr buf;struct page *page;int rc; -/* -* This function is expected to be called before SNP is -* initialized. -*/ -if (sev->snp_initialized) -return -EINVAL ; +lockdep_assert_held (&sev_cmd_mutex); - buf.address =__psp_pa (&sev->snp_plat_status); - rc =sev_do_cmd (SEV_CMD_SNP_PLATFORM_STATUS , &buf, error); + rc =__sev_do_snp_platform_status (&sev->snp_plat_status, error);if (rc) {dev_err (sev->dev,"SNP PLATFORM_STATUS command failed, ret = %d, error = %#x\n" , rc, *error);@@ -1297,7 +1292,7 @@ static int snp_get_platform_data(struct sev_device *sev, int *error)* command to ensure structure is 8-byte aligned, and does not * cross a page boundary. */ - page =alloc_page (GFP_KERNEL ); + page =__snp_alloc_firmware_pages (GFP_KERNEL ,0 , true);if (!page)return -ENOMEM ;@@ -1306,14 +1301,14 @@ static int snp_get_platform_data(struct sev_device *sev, int *error) snp_feat_info.ecx_in =0 ; snp_feat_info.feature_info_paddr =__psp_pa (feat_info); - rc =sev_do_cmd (SEV_CMD_SNP_FEATURE_INFO , &snp_feat_info, error); + rc =__sev_do_cmd_locked (SEV_CMD_SNP_FEATURE_INFO , &snp_feat_info, error);if (!rc) sev->snp_feat_info_0 = *feat_info;else dev_err (sev->dev,"SNP FEATURE_INFO command failed, ret = %d, error = %#x\n" , rc, *error); -__free_page (page); +__snp_free_firmware_pages (page,0 , true);return rc; }@@ -2097,6 +2092,8 @@ static int sev_get_api_version(void)struct sev_user_data_status status;int error =0 , ret; +guard (mutex)(&sev_cmd_mutex); +/* * Cache SNP platform status and SNP feature information * if SNP is available. @@ -2107,7 +2104,7 @@ static int sev_get_api_version(void)return 1 ; } - ret =sev_platform_status (&status, &error); + ret =__sev_do_cmd_locked (SEV_CMD_PLATFORM_STATUS , &status, &error);if (ret) {dev_err (sev->dev,"SEV: failed to get status. Error: %#x\n" , error);
PATCH v4 6/7 crypto/ccp: Register with fw_uploader and always fail
In reply to Pratik R. SampatIn preparation for SEV-SNP DOWNLOAD_FIRMWARE_EX live firmware update support, add an 'sev' firmware loader that always fails with EBUSY.
---
v3..v4:
* Collected Reviewed-by tags
* Cleanups for multiline error reporting - Tom
* Add FW_UPLOAD and FW_LOADER to CRYPTO_DEV_SP_PSP config- Shantanu
--- drivers/crypto/ccp/Kconfig | 2 + drivers/crypto/ccp/sev-dev.c | 72 ++++++++++++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 2 + 3 files changed, 76 insertions(+)
drivers/crypto/ccp/Kconfig +2 -0
@@ -40,6 +40,8 @@ config CRYPTO_DEV_SP_PSP default y depends on CRYPTO_DEV_CCP_DD && X86_64 && AMD_IOMMU select PCI_TSM if PCI + 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 +72 -0
96 lines not on this page · raw message
drivers/crypto/ccp/sev-dev.h +2 -0
@@ -69,6 +69,8 @@ struct sev_device {struct tsm_dev *tsmdev;struct sev_tio_status *tio_status; + +struct fw_upload *fwl; };int sev_dev_init (struct psp_device *psp);
Signature · 1 line
2.43.0
PATCH v4 7/7 crypto/ccp: Implement SNP Download Firmware EX
In reply to Pratik R. SampatImplement SNP live firmware update using the DOWNLOAD_FIRMWARE_EX command.
DOWNLOAD_FIRMWARE_EX requires the legacy SEV platform to be UNINIT. If it is WORKING then legacy guests are running and the update is refused as busy. If it is INIT, shut it down, release the buffers the firmware owns across that shutdown, run the update, and bring the platform back up afterwards. SNP is never taken down, so SNP guests are unaffected.
To test run the following with your sbin file in FW:
echo 1 > /sys/class/firmware/sev/loading cat <firmware.sbin> > /sys/class/firmware/sev/data echo 0 > /sys/class/firmware/sev/loading
The COMMIT bit is left clear, so the image is only loaded provisionally and the admin decides when to make it permanent with ioctl(/dev/sev, SNP_COMMIT). To roll back, do not commit and upload the previous image the same way.
---
v3..v4:
* Change dev prints to ratelimited since they occur due to userspace interactions - Tom
* Add comment about releasing sev_release_firmware_buffers() - Tom
* Consistent use of DLFW_EX rather than DOWNLOAD_FIRMWARE_EX in all comments - Tom
* Name error codes by command, firmware and psp within sev_fw_upload_update() - Tom
* Drop #ifdef CONFIG_FW_UPLOAD in lieu of electing FW_UPLOAD - Shantanu
* On failure of sev_get_api_version() return an error only if PSP failed in all other cases DLFW_EX succeeded - Shantanu
* Free TMR when platform state already in UNINIT too - local Sashiko instance
--- drivers/crypto/ccp/sev-dev.c | 300 ++++++++++++++++++++++++++++++++++- drivers/crypto/ccp/sev-dev.h | 2 + include/linux/psp-sev.h | 19 +++ 3 files changed, 320 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +299 -1
@@ -29,6 +29,7 @@#include <linux/psp.h> #include <linux/amd-iommu.h> #include <linux/crash_dump.h> +#include <linux/sizes.h> #include <asm/smp.h> #include <asm/cacheflush.h>
@@ -252,6 +253,7 @@ static int sev_cmd_buffer_len(int cmd)case SEV_CMD_SNP_PLATFORM_STATUS :return sizeof (struct sev_data_snp_addr );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_DOWNLOAD_FIRMWARE_EX :return sizeof (struct sev_data_download_firmware_ex );case SEV_CMD_SNP_COMMIT :return sizeof (struct sev_data_snp_commit );case SEV_CMD_SNP_FEATURE_INFO :return sizeof (struct sev_data_snp_feature_info );case SEV_CMD_SNP_VLEK_LOAD :return sizeof (struct sev_user_data_snp_vlek_load );
@@ -2212,17 +2214,313 @@ static int sev_update_firmware(struct device *dev)return ret; } +/* Largest image the firmware accepts, anything above is rejected */ +#define SEV_FW_IMAGE_MAX_SIZE SZ_512K +static enum fw_upload_err sev_fw_upload_prepare (struct fw_upload *fw_upload,const u8 *data,u32 size) { +struct sev_device *sev = fw_upload->dd_handle; + +if (size >SEV_FW_IMAGE_MAX_SIZE ) { +dev_err_ratelimited (sev->dev, +"DLFW_EX: image of %u bytes exceeds the %u byte maximum\n" , + size,SEV_FW_IMAGE_MAX_SIZE ); +return FW_UPLOAD_ERR_INVALID_SIZE ; + } +return FW_UPLOAD_ERR_NONE ; } +static int sev_download_firmware_ex (const u8 *data,u32 size,int *psp_ret) +{ +struct sev_data_download_firmware_ex sev_data = {0 }; +int ret, order; +struct page *p; +void *fw_blob; + + order =get_order (size); + p =alloc_pages (GFP_KERNEL | __GFP_ZERO, order); +if (!p) +return -ENOMEM ; + + fw_blob =page_address (p); +memcpy (fw_blob, data, size); + + sev_data.len =sizeof (sev_data); + sev_data.fw_paddr =__psp_pa (fw_blob); + sev_data.fw_len = size; +/* +* Don't commit the new firmware, an explicit SNP_COMMIT is required +* after update. +*/ + sev_data.commit =0 ; + + ret =__sev_do_cmd_locked (SEV_CMD_SNP_DOWNLOAD_FIRMWARE_EX , &sev_data, psp_ret); + +__free_pages (p, order); + +return ret; +} + +static enum fw_upload_err sev_fw_upload_handle_err (struct sev_device *sev, +int cmd_ret,int psp_ret) +{ +enum fw_upload_err fwl_ret =FW_UPLOAD_ERR_FW_INVALID ; +const char *msg; + +if (!cmd_ret) +return FW_UPLOAD_ERR_NONE ; + +if (psp_dead) { +/* +* The SEV command timed out and marked the ASP dead, there is +* no status to decode. +*/ +dev_err_ratelimited (sev->dev, +"DLFW_EX: PSP not responding (failed %d, error %#x)\n" , + cmd_ret, psp_ret); + sev->fwl_rollback_required = false; + +return FW_UPLOAD_ERR_TIMEOUT ; + } + +switch (psp_ret) { +case SEV_RET_INVALID_PARAM : + msg ="Provided image is not well formed" ; +break ; +case SEV_RET_INVALID_LEN : + fwl_ret =FW_UPLOAD_ERR_INVALID_SIZE ; + msg ="Provided image has an unusable length" ; +break ; +case SEV_RET_SHUTDOWN_REQUIRED : + msg ="Provided image cannot be live-updated, shutdown required" ; +break ; +case SEV_RET_BAD_VERSION : + msg ="Provided image < committed version" ; +break ; +case SEV_RET_INVALID_PLATFORM_STATE : + msg ="Platform not in UNINIT state" ; +break ; +case SEV_RET_INVALID_ADDRESS : + msg ="Unaligned address provided" ; +break ; +case SEV_RET_UNSUPPORTED : + msg ="Feature not supported" ; +break ; +case SEV_RET_INVALID_CONFIG : + msg ="Image rejected, unsupported configuration" ; +break ; +case SEV_RET_BAD_SVN : + msg ="Image rejected, SVN < committed SVN" ; +break ; +case SEV_RET_BAD_SIGNATURE : + msg ="Bad firmware signature" ; +break ; +case SEV_RET_UPDATE_FAILED : +/* The previous firmware is still running, a retry is safe. */ + fwl_ret =FW_UPLOAD_ERR_BUSY ; + msg ="Upgrade failed, automatically reverted" ; +break ; +case SEV_RET_RESTORE_REQUIRED : +/* +* Firmware requested a roll-back. Declare the PSP dead so +* nothing else tries to use it, and let the next upload through +* so the admin can restore the previous image. +*/ + sev->fwl_rollback_required = true; + psp_dead = true; + fwl_ret =FW_UPLOAD_ERR_RW_ERROR ; + msg ="Live upgrade failed, please roll back" ; +break ; +case SEV_RET_HWSEV_RET_UNSAFE : +/* +* Following a return of HARDWARE_UNSAFE, operation of the SEV +* firmware is indeterminate and the recommendation is to reboot +* the platform. Declare the PSP dead so the driver stops +* issuing commands to it while the reboot is pending. +*/ + sev->fwl_rollback_required = false; + psp_dead = true; + fwl_ret =FW_UPLOAD_ERR_HW_ERROR ; + msg ="SEV firmware no longer safe. Reboot recommended" ; +break ; +case SEV_RET_NO_FW_CALL : +/* The command never reached the firmware. */ + fwl_ret =FW_UPLOAD_ERR_BUSY ; + msg ="Driver error" ; +break ; +default : + fwl_ret =FW_UPLOAD_ERR_HW_ERROR ; + msg ="Unknown SEV firmware error" ; +break ; + } + +dev_err_ratelimited (sev->dev,"DLFW_EX: %s (failed %d, error %#x)\n" , + msg, cmd_ret, psp_ret); + +return fwl_ret; +} + +static int sev_fw_upload_shutdown_platform (struct sev_device *sev) +{ +int rc, error =SEV_RET_NO_FW_CALL , sev_plat_state; + +lockdep_assert_held (&sev_cmd_mutex); + + rc =sev_get_platform_state (&sev_plat_state, &error); +if (rc) { +dev_err_ratelimited (sev->dev,"SEV get platform state failed %d, error %#x\n" , + rc, error); +return rc; + } + +switch (sev_plat_state) { +case SEV_STATE_UNINIT : +break ; +case SEV_STATE_WORKING : +/* Legacy guests are running, the update cannot proceed. */ +return -EBUSY ; +case SEV_STATE_INIT : + rc =__sev_platform_shutdown_locked (&error); +if (rc) { +dev_err_ratelimited (sev->dev,"SEV platform shutdown failed %d, error %#x\n" , + rc, error); +return rc; + } + + sev->fwl_reinit_required = true; +break ; +default : +dev_err_ratelimited (sev->dev,"Unknown SEV firmware state %d\n" , sev_plat_state); +return -EINVAL ; + } + +/* +* After DLFW_EX, INIT under the new image can fail with +* SEV_RET_INVALID_PAGE_STATE if it is handed the TMR or INIT_EX NV +* buffer that the previous image left in the firmware state. +* +* Reclaim and free them rather than keeping them for reuse. The +* re-init allocates new buffers and moves them into the firmware +* state itself, and a skipped or failed re-init leaves no stale +* buffers behind for a later INIT or teardown to trip over. If the +* new 2M TMR allocation fails, INIT is issued without SEV-ES and +* only a warning is logged. +*/ +__sev_release_firmware_buffers (false); + +return 0 ; +} + +static void sev_fw_upload_reinit_platform (struct sev_device *sev) +{ +int rc, error =SEV_RET_NO_FW_CALL ; + +lockdep_assert_held (&sev_cmd_mutex); + +if (!sev->fwl_reinit_required) +return ; + + rc =__sev_platform_init_locked (&error); +if (rc) { +dev_err_ratelimited (sev->dev,"SEV platform re-init failed %d, error %#x\n" , + rc, error); +return ; + } + + sev->fwl_reinit_required = false; +} + +static enum fw_upload_err sev_fw_upload_update (struct sev_device *sev, +const u8 *data,u32 size, +u32 *written) +{ +int cmd_ret, psp_ret =SEV_RET_NO_FW_CALL ; +enum fw_upload_err fwl_ret; + +guard (mutex)(&sev_cmd_mutex); + +/* +* SEV firmware will only successfully execute the DLFW_EX command if a +* firmware rollback is required. Other commands may be processed, but +* may not execute properly. Use the psp_dead boolean to restrict +* execution to this path. +*/ +if (psp_dead && !sev->fwl_rollback_required) { +dev_err_ratelimited (sev->dev,"DLFW_EX: PSP is not responding\n" ); +return FW_UPLOAD_ERR_HW_ERROR ; + } + +/* +* If the last firmware update returned RESTORE_REQUIRED, retry DLFW_EX. +* Being in this state means that the legacy firmware has previously +* been shut down, so no need to do it again. +*/ +if (sev->fwl_rollback_required) { + psp_dead = false; + }else { + cmd_ret =sev_fw_upload_shutdown_platform (sev); +if (cmd_ret) +return psp_dead ?FW_UPLOAD_ERR_HW_ERROR :FW_UPLOAD_ERR_BUSY ; + } + + cmd_ret =sev_download_firmware_ex (data, size, &psp_ret); + fwl_ret =sev_fw_upload_handle_err (sev, cmd_ret, psp_ret); +if (fwl_ret ==FW_UPLOAD_ERR_NONE ) { + *written = size; + sev->fwl_rollback_required = false; + } + +/* +* Firmware either just requested a rollback or a rollback attempt +* failed. Keep the PSP dead for all other commands, only another +* DLFW_EX attempt is allowed through. +*/ +if (sev->fwl_rollback_required) { + psp_dead = true; +if (fwl_ret !=FW_UPLOAD_ERR_HW_ERROR ) + fwl_ret =FW_UPLOAD_ERR_RW_ERROR ; + } + +if (!sev->fwl_rollback_required && !psp_dead) +sev_fw_upload_reinit_platform (sev); + +return fwl_ret; +} +static enum fw_upload_err sev_fw_upload_write (struct fw_upload *fw_upload,const u8 *data,u32 offset,u32 size,u32 *written) { -return FW_UPLOAD_ERR_BUSY ; +struct sev_device *sev = fw_upload->dd_handle; +u8 old_major, old_minor, old_build; +enum fw_upload_err fwl_ret; + + old_major = sev->api_major; + old_minor = sev->api_minor; + old_build = sev->build; + + fwl_ret =sev_fw_upload_update (sev, data, size, written); +if (fwl_ret !=FW_UPLOAD_ERR_NONE ) +return fwl_ret; + +if (sev_get_api_version ()) { +dev_warn_ratelimited (sev->dev, +"SNP platform data refresh after firmware update failed\n" ); +return psp_dead ?FW_UPLOAD_ERR_HW_ERROR :FW_UPLOAD_ERR_NONE ; + } + +if (sev->api_major != old_major || sev->api_minor != old_minor || sev->build != old_build) { +dev_info_ratelimited (sev->dev,"SEV firmware updated from %d.%d.%d to %d.%d.%d\n" , + old_major, old_minor, old_build, + sev->api_major, sev->api_minor, sev->build); + }else { +dev_info_ratelimited (sev->dev,"SEV firmware version unchanged: %d.%d.%d\n" , + sev->api_major, sev->api_minor, sev->build); + } + +return fwl_ret; }static enum fw_upload_err sev_fw_upload_poll_complete (struct fw_upload *fw_upload)
drivers/crypto/ccp/sev-dev.h +2 -0
@@ -71,6 +71,8 @@ struct sev_device {struct sev_tio_status *tio_status;struct fw_upload *fwl; +bool fwl_rollback_required; +bool fwl_reinit_required; };int sev_dev_init (struct psp_device *psp);
include/linux/psp-sev.h +19 -0
@@ -890,6 +890,25 @@ struct sev_platform_init_args {unsigned int max_snp_asid; }; +/** +* struct sev_data_download_firmware_ex - SNP_DOWNLOAD_FIRMWARE_EX structure +* +* @len: length of the command buffer read by the PSP +* @rsvd0: reserved +* @fw_paddr: system physical address of the start of the firmware blob +* @fw_len: length of the firmware blob +* @commit: whether to immediately commit the firmware update +* @rsvd1: reserved +*/ +struct sev_data_download_firmware_ex { +u32 len;/* In */ +u32 rsvd0; +u64 fw_paddr;/* In */ +u32 fw_len;/* In */ +u32 commit:1 ;/* In */ +u32 rsvd1:31 ; +} __packed; +/** * struct sev_data_snp_commit - SNP_COMMIT structure *
Signature · 1 line
2.43.0
Re: [Patch v4 7/7] crypto/ccp: Implement SNP Download Firmware EX
Hi Pratik,
Two small comments if you happen to respin this one, no worries if not.
Quoted 15 lines · Pratik R. Sampat
drivers/crypto/ccp/sev-dev.c +15
-0+static int sev_download_firmware_ex (const u8 *data,u32 size,int *psp_ret) +{ +struct sev_data_download_firmware_ex sev_data = {0 }; +int ret, order; +struct page *p; +void *fw_blob; + + order =get_order (size); + p =alloc_pages (GFP_KERNEL | __GFP_ZERO, order); +if (!p) +return -ENOMEM ; + + fw_blob =page_address (p); +memcpy (fw_blob, data, size); +
drivers/crypto/ccp/sev-dev.c static int sev_update_firmware(struct device *dev) · line 2259
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+ sev_data.len =sizeof (sev_data); + sev_data.fw_paddr =__psp_pa (fw_blob); + sev_data.fw_len = size; +/* +* Don't commit the new firmware, an explicit SNP_COMMIT is required +* after update. +*/ + sev_data.commit =0 ; + + ret =__sev_do_cmd_locked (SEV_CMD_SNP_DOWNLOAD_FIRMWARE_EX , &sev_data, psp_ret);
I guess this function could use a lockdep_assert_held(&sev_cmd_mutex), but maybe we should not do that and instead put it in __sev_do_cmd_locked()...
Quoted 39 lines
drivers/crypto/ccp/sev-dev.c +39
-0+static enum fw_upload_err sev_fw_upload_update (struct sev_device *sev, +const u8 *data,u32 size, +u32 *written) +{ +int cmd_ret, psp_ret =SEV_RET_NO_FW_CALL ; +enum fw_upload_err fwl_ret; + +guard (mutex)(&sev_cmd_mutex); + +/* +* SEV firmware will only successfully execute the DLFW_EX command if a +* firmware rollback is required. Other commands may be processed, but +* may not execute properly. Use the psp_dead boolean to restrict +* execution to this path. +*/ +if (psp_dead && !sev->fwl_rollback_required) { +dev_err_ratelimited (sev->dev,"DLFW_EX: PSP is not responding\n" ); +return FW_UPLOAD_ERR_HW_ERROR ; + } + +/* +* If the last firmware update returned RESTORE_REQUIRED, retry DLFW_EX. +* Being in this state means that the legacy firmware has previously +* been shut down, so no need to do it again. +*/ +if (sev->fwl_rollback_required) { + psp_dead = false; + }else { + cmd_ret =sev_fw_upload_shutdown_platform (sev); +if (cmd_ret) +return psp_dead ?FW_UPLOAD_ERR_HW_ERROR :FW_UPLOAD_ERR_BUSY ; + } + + cmd_ret =sev_download_firmware_ex (data, size, &psp_ret); + fwl_ret =sev_fw_upload_handle_err (sev, cmd_ret, psp_ret); +if (fwl_ret ==FW_UPLOAD_ERR_NONE ) { + *written = size; + sev->fwl_rollback_required = false; + }
drivers/crypto/ccp/sev-dev.c static int sev_update_firmware(struct device *dev) · line 2483
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+ +/* +* Firmware either just requested a rollback or a rollback attempt +* failed. Keep the PSP dead for all other commands, only another +* DLFW_EX attempt is allowed through. +*/ +if (sev->fwl_rollback_required) { + psp_dead = true; +if (fwl_ret !=FW_UPLOAD_ERR_HW_ERROR ) + fwl_ret =FW_UPLOAD_ERR_RW_ERROR ;
I think this error switcharoo could use a comment (or maybe just be dropped?). e.g. if the user does something to set ->fwl_rollback_required and then tries another firmware upload that is the wrong size, ERR_INVALID_SIZE -> ERR_RW_ERROR, which seems misleading to me.
I saw some discussion about all this:
https://lore.kernel.org/lkml/[email protected]/
but it's not clear to me why you need to reset the error code here.
Thanks,
Tycho
Re: [Patch v4 7/7] crypto/ccp: Implement SNP Download Firmware EX
Hi Tycho
Quoted 27 lines · Tycho Andersen
Hi Pratik,
Two small comments if you happen to respin this one, no worries if not.
Quoted 21 lines · Pratik R. Sampat
drivers/crypto/ccp/sev-dev.c +21
-0+static int sev_download_firmware_ex (const u8 *data,u32 size,int *psp_ret) +{ +struct sev_data_download_firmware_ex sev_data = {0 }; +int ret, order; +struct page *p; +void *fw_blob; + + order =get_order (size); + p =alloc_pages (GFP_KERNEL | __GFP_ZERO, order); +if (!p) +return -ENOMEM ; + + fw_blob =page_address (p); +memcpy (fw_blob, data, size); + + sev_data.len =sizeof (sev_data); + sev_data.fw_paddr =__psp_pa (fw_blob); + sev_data.fw_len = size; +/* +* Don't commit the new firmware, an explicit SNP_COMMIT is required +* after update.
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c static int sev_update_firmware(struct device *dev) · line 2259
Quoted 4 lines
drivers/crypto/ccp/sev-dev.c +4
-0+*/ + sev_data.commit =0 ; + + ret =__sev_do_cmd_locked (SEV_CMD_SNP_DOWNLOAD_FIRMWARE_EX , &sev_data, psp_ret);I guess this function could use a
lockdep_assert_held(&sev_cmd_mutex), but maybe we should not do that and instead put it in__sev_do_cmd_locked()...
Yeah doing the latter seems like a better idea for quite a few calls that use this. And I think maybe better so in a separate cleanup patch.
Quoted 57 lines
Quoted 40 lines
drivers/crypto/ccp/sev-dev.c +39
-0+static enum fw_upload_err sev_fw_upload_update (struct sev_device *sev, +const u8 *data,u32 size, +u32 *written) +{ +int cmd_ret, psp_ret =SEV_RET_NO_FW_CALL ; +enum fw_upload_err fwl_ret; + +guard (mutex)(&sev_cmd_mutex); + +/* +* SEV firmware will only successfully execute the DLFW_EX command if a +* firmware rollback is required. Other commands may be processed, but +* may not execute properly. Use the psp_dead boolean to restrict +* execution to this path. +*/ +if (psp_dead && !sev->fwl_rollback_required) { +dev_err_ratelimited (sev->dev,"DLFW_EX: PSP is not responding\n" ); +return FW_UPLOAD_ERR_HW_ERROR ; + } + +/* +* If the last firmware update returned RESTORE_REQUIRED, retry DLFW_EX. +* Being in this state means that the legacy firmware has previously +* been shut down, so no need to do it again. +*/ +if (sev->fwl_rollback_required) { + psp_dead = false; + }else { + cmd_ret =sev_fw_upload_shutdown_platform (sev); +if (cmd_ret) +return psp_dead ?FW_UPLOAD_ERR_HW_ERROR :FW_UPLOAD_ERR_BUSY ; + } + + cmd_ret =sev_download_firmware_ex (data, size, &psp_ret); + fwl_ret =sev_fw_upload_handle_err (sev, cmd_ret, psp_ret); +if (fwl_ret ==FW_UPLOAD_ERR_NONE ) { + *written = size; + sev->fwl_rollback_required = false; + }drivers/crypto/ccp/sev-dev.c static int sev_update_firmware(struct device *dev) · line 2483
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+ +/* +* Firmware either just requested a rollback or a rollback attempt +* failed. Keep the PSP dead for all other commands, only another +* DLFW_EX attempt is allowed through. +*/ +if (sev->fwl_rollback_required) { + psp_dead = true; +if (fwl_ret !=FW_UPLOAD_ERR_HW_ERROR ) + fwl_ret =FW_UPLOAD_ERR_RW_ERROR ;I think this error switcharoo could use a comment (or maybe just be dropped?). e.g. if the user does something to set ->fwl_rollback_required and then tries another firmware upload that is the wrong size,
ERR_INVALID_SIZE->ERR_RW_ERROR, which seems misleading to me.
Quoted 5 lines
I saw some discussion about all this:
https://lore.kernel.org/lkml/[email protected]/but it's not clear to me why you need to reset the error code here.
My understanding from that discussion was that userspace errors should fall into clean buckets. read-write-error would mean "roll back", whether it came from the first failure or a later rollback attempt, since a failed rollback still leaves the PSP dead and needing a rollback. dmesg would still log the exact reason for the failure.
That said, I see how it can be misleading, and I'm fine with dropping the override or putting a comment and re-spinning this.
Thanks
--Pratik
Quoted 3 lines
Thanks,
Tycho
Re: [Patch v4 7/7] crypto/ccp: Implement SNP Download Firmware EX
Quoted 22 lines · Pratik R. Sampat
Quoted 16 lines · Tycho Andersen
drivers/crypto/ccp/sev-dev.c static int sev_update_firmware(struct device *dev) · line 2483
Quoted 9 lines
drivers/crypto/ccp/sev-dev.c +9
-0+/* +* Firmware either just requested a rollback or a rollback attempt +* failed. Keep the PSP dead for all other commands, only another +* DLFW_EX attempt is allowed through. +*/ +if (sev->fwl_rollback_required) { + psp_dead = true; +if (fwl_ret !=FW_UPLOAD_ERR_HW_ERROR ) + fwl_ret =FW_UPLOAD_ERR_RW_ERROR ;I think this error switcharoo could use a comment (or maybe just be dropped?). e.g. if the user does something to set ->fwl_rollback_required and then tries another firmware upload that is the wrong size,
ERR_INVALID_SIZE->ERR_RW_ERROR, which seems misleading to me.Quoted 4 lines
I saw some discussion about all this:
https://lore.kernel.org/lkml/[email protected]/
but it's not clear to me why you need to reset the error code here.
Quoted 8 lines
My understanding from that discussion was that userspace errors should fall into clean buckets. read-write-error would mean "roll back", whether it came from the first failure or a later rollback attempt, since a failed rollback still leaves the PSP dead and needing a rollback. dmesg would still log the exact reason for the failure.
That said, I see how it can be misleading, and I'm fine with dropping the override or putting a comment and re-spinning this.
I do see how it can be misleading.
I think dropping it makes sense. When I suggested the changes for v2, psp_ret was only logged via dev_dbg(), so the sysfs error was the only clean signal userspace had. Since the error code is reported in dmesg now, I think it's fine to keep the original error and have the sysfs error reflect what happened on the upload rather than the action tooling should take.
Thanks,
Shantanu
Re: [Patch v4 7/7] crypto/ccp: Implement SNP Download Firmware EX
Quoted 32 lines · Shantanu Sinha
Quoted 22 lines · Pratik R. Sampat
Quoted 16 lines · Tycho Andersen
drivers/crypto/ccp/sev-dev.c static int sev_update_firmware(struct device *dev) · line 2483
Quoted 9 lines
drivers/crypto/ccp/sev-dev.c +9
-0+/* +* Firmware either just requested a rollback or a rollback attempt +* failed. Keep the PSP dead for all other commands, only another +* DLFW_EX attempt is allowed through. +*/ +if (sev->fwl_rollback_required) { + psp_dead = true; +if (fwl_ret !=FW_UPLOAD_ERR_HW_ERROR ) + fwl_ret =FW_UPLOAD_ERR_RW_ERROR ;I think this error switcharoo could use a comment (or maybe just be dropped?). e.g. if the user does something to set ->fwl_rollback_required and then tries another firmware upload that is the wrong size,
ERR_INVALID_SIZE->ERR_RW_ERROR, which seems misleading to me.Quoted 4 lines
I saw some discussion about all this:
https://lore.kernel.org/lkml/[email protected]/
but it's not clear to me why you need to reset the error code here.Quoted 8 lines
My understanding from that discussion was that userspace errors should fall into clean buckets. read-write-error would mean "roll back", whether it came from the first failure or a later rollback attempt, since a failed rollback still leaves the PSP dead and needing a rollback. dmesg would still log the exact reason for the failure.
That said, I see how it can be misleading, and I'm fine with dropping the override or putting a comment and re-spinning this.
Quoted 9 lines
I do see how it can be misleading.
I think dropping it makes sense. When I suggested the changes for v2,
psp_retwas only logged viadev_dbg(), so the sysfs error was the only clean signal userspace had. Since the error code is reported in dmesg now, I think it's fine to keep the original error and have the sysfs error reflect what happened on the upload rather than the action tooling should take.
Sounds good to me. I'll drop it in the next spin.
Thanks!
--Pratik