From 8ecca4e94cdaecd34da6a18b82cc04a181bdc1cb Mon Sep 17 00:00:00 2001 From: xiongmengbiao Date: Sat, 14 Sep 2024 14:08:34 +0800 Subject: [PATCH] crypto: ccp: add more checks for sev_dev_hooks_installed In some Hygon platform-related functions, `hygon_psp_hooks` must be initialized. Therefore, additional checks are needed to ensure that `sev_dev_hooks_installed` value is true before continuing execution. Signed-off-by: xiongmengbiao --- drivers/crypto/ccp/hygon/csv-dev.c | 20 +++++++++++++++++++- drivers/crypto/ccp/hygon/psp-dev.c | 16 ++++++++++++++-- drivers/crypto/ccp/psp-dev.c | 17 ++++++++++++----- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/drivers/crypto/ccp/hygon/csv-dev.c b/drivers/crypto/ccp/hygon/csv-dev.c index eaacd8868ef6..9ab2005050ba 100644 --- a/drivers/crypto/ccp/hygon/csv-dev.c +++ b/drivers/crypto/ccp/hygon/csv-dev.c @@ -824,6 +824,9 @@ static int vpsp_psp_mutex_trylock(void) { int mutex_enabled = READ_ONCE(hygon_psp_hooks.psp_mutex_enabled); + if (!hygon_psp_hooks.sev_dev_hooks_installed) + return -ENODEV; + if (mutex_enabled) return psp_mutex_trylock(&hygon_psp_hooks.psp_misc->data_pg_aligned->mb_mutex); else @@ -834,6 +837,9 @@ static int vpsp_psp_mutex_unlock(void) { int mutex_enabled = READ_ONCE(hygon_psp_hooks.psp_mutex_enabled); + if (!hygon_psp_hooks.sev_dev_hooks_installed) + return -ENODEV; + if (mutex_enabled) psp_mutex_unlock(&hygon_psp_hooks.psp_misc->data_pg_aligned->mb_mutex); else @@ -850,6 +856,9 @@ static int __vpsp_ring_buffer_enter_locked(int *error) struct csv_ringbuffer_queue *hi_queue; struct sev_device *sev = psp_master->sev_data; + if (!hygon_psp_hooks.sev_dev_hooks_installed) + return -ENODEV; + if (csv_comm_mode == CSV_COMM_RINGBUFFER_ON) return -EEXIST; @@ -886,7 +895,7 @@ static int __vpsp_do_ringbuf_cmds_locked(int *psp_ret, uint8_t prio, int index) unsigned int rb_ctl; struct sev_device *sev; - if (!psp) + if (!psp || !hygon_psp_hooks.sev_dev_hooks_installed) return -ENODEV; if (*hygon_psp_hooks.psp_dead) @@ -949,6 +958,9 @@ static int vpsp_do_ringbuf_cmds_locked(int *psp_ret, uint8_t prio, int index) struct sev_user_data_status data; int rc; + if (!hygon_psp_hooks.sev_dev_hooks_installed) + return -ENODEV; + rc = __vpsp_ring_buffer_enter_locked(psp_ret); if (rc) goto end; @@ -1064,6 +1076,9 @@ int vpsp_try_get_result(uint32_t vid, uint8_t prio, uint32_t index, void *data, int ret = 0; struct csv_cmdptr_entry cmd = {0}; + if (!hygon_psp_hooks.sev_dev_hooks_installed) + return -ENODEV; + /* Get the retult directly if the command has been executed */ if (index >= 0 && vpsp_get_cmd_status(prio, index) != VPSP_CMD_STATUS_RUNNING) { @@ -1129,6 +1144,9 @@ int vpsp_try_do_cmd(uint32_t vid, int cmd, void *data, struct vpsp_ret *psp_ret) int index = -1; uint8_t prio = CSV_COMMAND_PRIORITY_LOW; + if (!hygon_psp_hooks.sev_dev_hooks_installed) + return -ENODEV; + /* ringbuffer mode check and parse command prio*/ rb_supported = vpsp_rb_check_and_cmd_prio_parse(&prio, (struct vpsp_cmd *)&cmd); diff --git a/drivers/crypto/ccp/hygon/psp-dev.c b/drivers/crypto/ccp/hygon/psp-dev.c index 3b1fd8f628e1..620e65228ed9 100644 --- a/drivers/crypto/ccp/hygon/psp-dev.c +++ b/drivers/crypto/ccp/hygon/psp-dev.c @@ -321,6 +321,9 @@ static long ioctl_psp(struct file *file, unsigned int ioctl, unsigned long arg) struct vpsp_dev_ctrl vpsp_ctrl_op; int ret = -EFAULT; + if (!hygon_psp_hooks.sev_dev_hooks_installed) + return -ENODEV; + if (_IOC_TYPE(ioctl) != HYGON_PSP_IOC_TYPE) { printk(KERN_INFO "%s: invalid ioctl type: 0x%x\n", __func__, _IOC_TYPE(ioctl)); return -EINVAL; @@ -381,6 +384,9 @@ int hygon_psp_additional_setup(struct sp_device *sp) struct device *dev = sp->dev; int ret = 0; + if (!hygon_psp_hooks.sev_dev_hooks_installed) + return -ENODEV; + if (!psp_misc) { struct miscdevice *misc; @@ -509,7 +515,7 @@ int __vpsp_do_cmd_locked(uint32_t vid, int cmd, void *data, int *psp_ret) unsigned int phys_lsb, phys_msb; unsigned int reg, ret = 0; - if (!psp || !psp->sev_data) + if (!psp || !psp->sev_data || !hygon_psp_hooks.sev_dev_hooks_installed) return -ENODEV; if (*hygon_psp_hooks.psp_dead) @@ -573,7 +579,10 @@ int vpsp_do_cmd(uint32_t vid, int cmd, void *data, int *psp_ret) int rc; int mutex_enabled = READ_ONCE(hygon_psp_hooks.psp_mutex_enabled); - if (is_vendor_hygon() && mutex_enabled) { + if (!hygon_psp_hooks.sev_dev_hooks_installed) + return -ENODEV; + + if (mutex_enabled) { if (psp_mutex_lock_timeout(&psp_misc->data_pg_aligned->mb_mutex, PSP_MUTEX_TIMEOUT) != 1) { return -EBUSY; @@ -597,6 +606,9 @@ int psp_do_cmd(int cmd, void *data, int *psp_ret) int rc; int mutex_enabled = READ_ONCE(hygon_psp_hooks.psp_mutex_enabled); + if (!hygon_psp_hooks.sev_dev_hooks_installed) + return -ENODEV; + if (mutex_enabled) { if (psp_mutex_lock_timeout(&psp_misc->data_pg_aligned->mb_mutex, PSP_MUTEX_TIMEOUT) != 1) diff --git a/drivers/crypto/ccp/psp-dev.c b/drivers/crypto/ccp/psp-dev.c index 49dde0baae41..ccb605bf8618 100644 --- a/drivers/crypto/ccp/psp-dev.c +++ b/drivers/crypto/ccp/psp-dev.c @@ -199,11 +199,6 @@ int psp_dev_init(struct sp_device *sp) /* Request an irq */ if (is_vendor_hygon()) { - ret = hygon_psp_additional_setup(sp); - if (ret) { - dev_err(dev, "psp: unable to do additional setup\n"); - goto e_err; - } ret = sp_request_hygon_psp_irq(psp->sp, psp_irq_handler, psp->name, psp); } else { ret = sp_request_psp_irq(psp->sp, psp_irq_handler, psp->name, psp); @@ -221,6 +216,18 @@ int psp_dev_init(struct sp_device *sp) if (ret) goto e_irq; + /** + * hygon_psp_additional_setup() needs to wait for + * sev_dev_install_hooks() to complete before it can be called. + */ + if (is_vendor_hygon()) { + ret = hygon_psp_additional_setup(sp); + if (ret) { + dev_err(dev, "psp: unable to do additional setup\n"); + goto e_irq; + } + } + /* Enable interrupt */ iowrite32(-1, psp->io_regs + psp->vdata->inten_reg); -- Gitee