Improved cleanup handling for amd-pmf
Subsystems (2)
v5 cover Improved cleanup handling for amd-pmf
From: Mario Limonciello <[email protected]>
I noticed some memory problems with unloading and reloading amd-pmf. These were root caused with a double free.
This series cleans up that double free by switching to device managed allocations and also fixes other problems observed in earlier iterations of the patches.
Mario Limonciello (3):
platform/x86/amd: pmf: Use device managed allocations platform/x86/amd: pmf: Prevent amd_pmf_tee_deinit() from running twice platform/x86/amd: pmf: Simplify error flow in amd_pmf_init_smart_pc()
drivers/platform/x86/amd/pmf/core.c | 3 +- drivers/platform/x86/amd/pmf/tee-if.c | 108 +++++++++----------------- 2 files changed, 39 insertions(+), 72 deletions(-)
Signature · 1 line
2.43.0
PATCH v5 1/3 platform/x86/amd: pmf: Use device managed allocations
From: Mario Limonciello <[email protected]>
If setting up smart PC fails for any reason then this can lead to a double free when unloading amd-pmf. This is because dev->buf was freed but never set to NULL and is again freed in amd_pmf_remove().
To avoid subtle allocation bugs in failures leading to a double free change all allocations into device managed allocations.
---
v5:
* Keep same flow as before patch for amd_pmf_get_pb_data()
--- drivers/platform/x86/amd/pmf/core.c | 3 +- drivers/platform/x86/amd/pmf/tee-if.c | 56 ++++++++++----------------- 2 files changed, 22 insertions(+), 37 deletions(-)
drivers/platform/x86/amd/pmf/core.c +1 -2
@@ -280,7 +280,7 @@ int amd_pmf_set_dram_addr(struct amd_pmf_dev *dev, bool alloc_buffer)dev_err (dev->dev,"Invalid CPU id: 0x%x" , dev->cpu_id); } - dev->buf =kzalloc (dev->mtable_size,GFP_KERNEL ); + dev->buf =devm_kzalloc (dev->dev, dev->mtable_size,GFP_KERNEL );if (!dev->buf)return -ENOMEM ; }
@@ -493,7 +493,6 @@ static void amd_pmf_remove(struct platform_device *pdev)mutex_destroy (&dev->lock);mutex_destroy (&dev->update_mutex);mutex_destroy (&dev->cb_mutex); -kfree (dev->buf); }static const struct attribute_group *amd_pmf_driver_groups[] = {
drivers/platform/x86/amd/pmf/tee-if.c +21 -35
122 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v5 2/3 platform/x86/amd: pmf: Prevent amd_pmf_tee_deinit() from running twice
In reply to Mario LimoncielloFrom: Mario Limonciello <[email protected]>
If any of the tee init fails, pass up the errors and clear the tee_ctx pointer. This will prevent cleaning up multiple times.
--- drivers/platform/x86/amd/pmf/tee-if.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/platform/x86/amd/pmf/tee-if.c +8 -3
@@ -420,12 +420,12 @@ static int amd_pmf_ta_open_session(struct tee_context *ctx, u32 *id, const uuid_ rc =tee_client_open_session (ctx, &sess_arg,NULL );if (rc <0 || sess_arg.ret !=0 ) {pr_err ("Failed to open TEE session err:%#x, rc:%d\n" , sess_arg.ret, rc); -return rc; +return rc ?: -EINVAL ; } *id = sess_arg.session; -return rc; +return 0 ; }static int amd_pmf_register_input_device (struct amd_pmf_dev *dev)
@@ -460,7 +460,9 @@ static int amd_pmf_tee_init(struct amd_pmf_dev *dev, const uuid_t *uuid) dev->tee_ctx =tee_client_open_context (NULL , amd_pmf_amdtee_ta_match,NULL ,NULL );if (IS_ERR (dev->tee_ctx)) {dev_err (dev->dev,"Failed to open TEE context\n" ); -return PTR_ERR (dev->tee_ctx); + ret =PTR_ERR (dev->tee_ctx); + dev->tee_ctx =NULL ; +return ret; } ret =amd_pmf_ta_open_session (dev->tee_ctx, &dev->session_id, uuid);
@@ -500,9 +502,12 @@ static int amd_pmf_tee_init(struct amd_pmf_dev *dev, const uuid_t *uuid)static void amd_pmf_tee_deinit (struct amd_pmf_dev *dev) { +if (!dev->tee_ctx) +return ;tee_shm_free (dev->fw_shm_pool);tee_client_close_session (dev->tee_ctx, dev->session_id);tee_client_close_context (dev->tee_ctx); + dev->tee_ctx =NULL ; }int amd_pmf_init_smart_pc (struct amd_pmf_dev *dev)
Signature · 1 line
2.43.0
PATCH v5 3/3 platform/x86/amd: pmf: Simplify error flow in amd_pmf_init_smart_pc()
In reply to Mario LimoncielloFrom: Mario Limonciello <[email protected]>
commit 5b1122fc4995f ("platform/x86/amd/pmf: fix cleanup in amd_pmf_init_smart_pc()") adjusted the error handling flow to use a ladder but this isn't actually needed because work is only scheduled in amd_pmf_start_policy_engine() and with device managed cleanups pointers for allocations don't need to be freed.
Adjust the error flow to a single call to amd_pmf_deinit_smart_pc() for the cases that need to clean up.
--- drivers/platform/x86/amd/pmf/tee-if.c | 55 ++++++++------------------- 1 file changed, 16 insertions(+), 39 deletions(-)
drivers/platform/x86/amd/pmf/tee-if.c +16 -39
@@ -530,64 +530,45 @@ int amd_pmf_init_smart_pc(struct amd_pmf_dev *dev) ret =amd_pmf_set_dram_addr (dev, true);if (ret) - goto err_cancel_work; +return ret; dev->policy_base =devm_ioremap_resource (dev->dev, dev->res); -if (IS_ERR (dev->policy_base)) { - ret =PTR_ERR (dev->policy_base); - goto err_cancel_work; - } +if (IS_ERR (dev->policy_base)) +return PTR_ERR (dev->policy_base); dev->policy_buf =devm_kzalloc (dev->dev, dev->policy_sz,GFP_KERNEL ); -if (!dev->policy_buf) { - ret = -ENOMEM ; - goto err_cancel_work; - } +if (!dev->policy_buf) +return -ENOMEM ;memcpy_fromio (dev->policy_buf, dev->policy_base, dev->policy_sz);if (!amd_pmf_pb_valid (dev)) {dev_info (dev->dev,"No Smart PC policy present\n" ); - ret = -EINVAL ; - goto err_cancel_work; +return -EINVAL ; }amd_pmf_hex_dump_pb (dev); dev->prev_data =devm_kzalloc (dev->dev,sizeof (*dev->prev_data),GFP_KERNEL ); -if (!dev->prev_data) { - ret = -ENOMEM ; - goto err_cancel_work; - } +if (!dev->prev_data) +return -ENOMEM ;for (i =0 ; i <ARRAY_SIZE (amd_pmf_ta_uuid); i++) { ret =amd_pmf_tee_init (dev, &amd_pmf_ta_uuid[i]);if (ret) - goto err_cancel_work; +return ret; ret =amd_pmf_start_policy_engine (dev); -switch (ret) { -case TA_PMF_TYPE_SUCCESS : - status = true; -break ; -case TA_ERROR_CRYPTO_INVALID_PARAM : -case TA_ERROR_CRYPTO_BIN_TOO_LARGE : -amd_pmf_tee_deinit (dev); - status = false; -break ; -default : - ret = -EINVAL ; -amd_pmf_tee_deinit (dev); - goto err_cancel_work; - } - +dev_dbg (dev->dev,"start policy engine ret: %d\n" , ret); + status = ret ==TA_PMF_TYPE_SUCCESS ;if (status)break ; +amd_pmf_tee_deinit (dev); }if (!status && !pb_side_load) { ret = -EINVAL ; - goto err_cancel_work; + goto err; }if (pb_side_load)
@@ -595,16 +576,12 @@ int amd_pmf_init_smart_pc(struct amd_pmf_dev *dev) ret =amd_pmf_register_input_device (dev);if (ret) - goto err_pmf_remove_pb; + goto err;return 0 ; -err_pmf_remove_pb: -if (pb_side_load && dev->esbin) -amd_pmf_remove_pb (dev); -amd_pmf_tee_deinit (dev); -err_cancel_work: -cancel_delayed_work_sync (&dev->pb_work); +err: +amd_pmf_deinit_smart_pc (dev);return ret; }
Signature · 1 line
2.43.0
Ilpo Järvinen
maintainerQuoted 8 lines · Mario Limonciello
I noticed some memory problems with unloading and reloading amd-pmf. These were root caused with a double free.
This series cleans up that double free by switching to device managed allocations and also fixes other problems observed in earlier iterations of the patches.
[...]
Thank you for your contribution, it has been applied to my local review-ilpo-fixes branch. Note it will show up in the public platform-drivers-x86/review-ilpo-fixes branch only once I've pushed my local branch there, which might take a while.
The list of commits applied:
[1/3] platform/x86/amd: pmf: Use device managed allocations
commit: d9db3a941270d92bbd1a6a6b54a10324484f2f2d
[2/3] platform/x86/amd: pmf: Prevent amd_pmf_tee_deinit() from running twice
commit: 93103d56650d7a38ed37ba4041578310f82776ae
[3/3] platform/x86/amd: pmf: Simplify error flow in amd_pmf_init_smart_pc()
commit: d7186dfd41924ef01ef490be5b82ab63cc417b31--
i.