Skip to content

Commit e709615

Browse files
superm1jwrdegoede
authored andcommitted
platform/x86/amd/pmf: Fixup error handling for amd_pmf_init_smart_pc()
amd_pmf_init_smart_pc() calls out to amd_pmf_get_bios_buffer() but the error handling flow doesn't clean everything up allocated memory. As amd_pmf_get_bios_buffer() is only called by amd_pmf_init_smart_pc(), fold it into the function and add labels to clean up any step that can fail along the way. Explicitly set everything allocated to NULL as there are other features that may access some of the same variables. Fixes: 7c45534 ("platform/x86/amd/pmf: Add support for PMF Policy Binary") Signed-off-by: Mario Limonciello <[email protected]> Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Hans de Goede <[email protected]> Signed-off-by: Hans de Goede <[email protected]>
1 parent 20545af commit e709615

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

drivers/platform/x86/amd/pmf/tee-if.c

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -338,25 +338,6 @@ static void amd_pmf_remove_pb(struct amd_pmf_dev *dev) {}
338338
static void amd_pmf_hex_dump_pb(struct amd_pmf_dev *dev) {}
339339
#endif
340340

341-
static int amd_pmf_get_bios_buffer(struct amd_pmf_dev *dev)
342-
{
343-
dev->policy_buf = kzalloc(dev->policy_sz, GFP_KERNEL);
344-
if (!dev->policy_buf)
345-
return -ENOMEM;
346-
347-
dev->policy_base = devm_ioremap(dev->dev, dev->policy_addr, dev->policy_sz);
348-
if (!dev->policy_base)
349-
return -ENOMEM;
350-
351-
memcpy(dev->policy_buf, dev->policy_base, dev->policy_sz);
352-
353-
amd_pmf_hex_dump_pb(dev);
354-
if (pb_side_load)
355-
amd_pmf_open_pb(dev, dev->dbgfs_dir);
356-
357-
return amd_pmf_start_policy_engine(dev);
358-
}
359-
360341
static int amd_pmf_amdtee_ta_match(struct tee_ioctl_version_data *ver, const void *data)
361342
{
362343
return ver->impl_id == TEE_IMPL_ID_AMDTEE;
@@ -455,22 +436,56 @@ int amd_pmf_init_smart_pc(struct amd_pmf_dev *dev)
455436
return ret;
456437

457438
INIT_DELAYED_WORK(&dev->pb_work, amd_pmf_invoke_cmd);
458-
amd_pmf_set_dram_addr(dev, true);
459-
amd_pmf_get_bios_buffer(dev);
439+
440+
ret = amd_pmf_set_dram_addr(dev, true);
441+
if (ret)
442+
goto error;
443+
444+
dev->policy_base = devm_ioremap(dev->dev, dev->policy_addr, dev->policy_sz);
445+
if (!dev->policy_base) {
446+
ret = -ENOMEM;
447+
goto error;
448+
}
449+
450+
dev->policy_buf = kzalloc(dev->policy_sz, GFP_KERNEL);
451+
if (!dev->policy_buf) {
452+
ret = -ENOMEM;
453+
goto error;
454+
}
455+
456+
memcpy(dev->policy_buf, dev->policy_base, dev->policy_sz);
457+
458+
amd_pmf_hex_dump_pb(dev);
459+
if (pb_side_load)
460+
amd_pmf_open_pb(dev, dev->dbgfs_dir);
461+
460462
dev->prev_data = kzalloc(sizeof(*dev->prev_data), GFP_KERNEL);
461463
if (!dev->prev_data)
462-
return -ENOMEM;
464+
goto error;
463465

464-
return dev->smart_pc_enabled;
466+
ret = amd_pmf_start_policy_engine(dev);
467+
if (ret)
468+
goto error;
469+
470+
return 0;
471+
472+
error:
473+
amd_pmf_deinit_smart_pc(dev);
474+
475+
return ret;
465476
}
466477

467478
void amd_pmf_deinit_smart_pc(struct amd_pmf_dev *dev)
468479
{
469-
if (pb_side_load)
480+
if (pb_side_load && dev->esbin)
470481
amd_pmf_remove_pb(dev);
471482

483+
cancel_delayed_work_sync(&dev->pb_work);
472484
kfree(dev->prev_data);
485+
dev->prev_data = NULL;
473486
kfree(dev->policy_buf);
474-
cancel_delayed_work_sync(&dev->pb_work);
487+
dev->policy_buf = NULL;
488+
kfree(dev->buf);
489+
dev->buf = NULL;
475490
amd_pmf_tee_deinit(dev);
476491
}

0 commit comments

Comments
 (0)