drm/amdgpu: Block MMR_READ IOCTL in reset
Subsystems (2)
PATCH 1/1 drm/amdgpu: Block MMR_READ IOCTL in reset
Register access from userspace should be blocked until reset is complete.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 44 ++++++++++++++++++------- 1 file changed, 32 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +32 -12
@@ -778,6 +778,7 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) ? -EFAULT :0 ; }case AMDGPU_INFO_READ_MMR_REG : { +int ret =0 ;unsigned int n, alloc_size;uint32_t *regs; unsigned int se_num = (info->read_mmr_reg.instance >>
@@ -787,24 +788,39 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) AMDGPU_INFO_MMR_SH_INDEX_SHIFT) &AMDGPU_INFO_MMR_SH_INDEX_MASK ; +if (!down_read_trylock (&adev->reset_domain->sem)) +return -ENOENT ; +/* set full masks if the userspace set all bits * in the bitfields */ -if (se_num ==AMDGPU_INFO_MMR_SE_INDEX_MASK ) +if (se_num ==AMDGPU_INFO_MMR_SE_INDEX_MASK ) { se_num =0xffffffff ; -else if (se_num >=AMDGPU_GFX_MAX_SE ) -return -EINVAL ; -if (sh_num ==AMDGPU_INFO_MMR_SH_INDEX_MASK ) + } +else if (se_num >=AMDGPU_GFX_MAX_SE ) { + ret = -EINVAL ; + goto out; + } + +if (sh_num ==AMDGPU_INFO_MMR_SH_INDEX_MASK ) { sh_num =0xffffffff ; -else if (sh_num >=AMDGPU_GFX_MAX_SH_PER_SE ) -return -EINVAL ; + } +else if (sh_num >=AMDGPU_GFX_MAX_SH_PER_SE ) { + ret = -EINVAL ; + goto out; + } -if (info->read_mmr_reg.count >128 ) -return -EINVAL ; +if (info->read_mmr_reg.count >128 ) { + ret = -EINVAL ; + goto out; + } regs =kmalloc_array (info->read_mmr_reg.count,sizeof (*regs),GFP_KERNEL ); -if (!regs) -return -ENOMEM ; +if (!regs) { + ret = -ENOMEM ; + goto out; + } + alloc_size = info->read_mmr_reg.count *sizeof (*regs);amdgpu_gfx_off_ctrl (adev, false);
@@ -816,13 +832,17 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) info->read_mmr_reg.dword_offset + i);kfree (regs);amdgpu_gfx_off_ctrl (adev, true); -return -EFAULT ; + ret = -EFAULT ; + goto out; } }amdgpu_gfx_off_ctrl (adev, true); n =copy_to_user (out, regs,min (size, alloc_size));kfree (regs); -return n ? -EFAULT :0 ; + ret = (n ? -EFAULT :0 ); +out: +up_read (&adev->reset_domain->sem); +return ret; }case AMDGPU_INFO_DEV_INFO : {struct drm_amdgpu_info_device *dev_info;
Signature · 1 line
2.34.1
Quoted 3 lines · Victor Skvortsov
Register access from userspace should be blocked until reset is complete.
While you are at it, you should protect AMDGPU_INFO_SENSOR as well. That queries PMFW which doesn't make sense during reset. A few minor comments below as well.
Quoted 26 lines
Signed-off-by Victor Skvortsov <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 44 ++++++++++++++++++------- 1 file changed, 32 insertions(+), 12 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +4
-0@@ -778,6 +778,7 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) ? -EFAULT :0 ; }case AMDGPU_INFO_READ_MMR_REG : { +int ret =0 ;unsigned int n, alloc_size;uint32_t *regs; unsigned int se_num = (info->read_mmr_reg.instance >>@@ -787,24 +788,39 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) AMDGPU_INFO_MMR_SH_INDEX_SHIFT) &AMDGPU_INFO_MMR_SH_INDEX_MASK ; +if (!down_read_trylock (&adev->reset_domain->sem)) +return -ENOENT ; +/* set full masks if the userspace set all bits
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) · line 800
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +3
-4* in the bitfields */ -if (se_num ==AMDGPU_INFO_MMR_SE_INDEX_MASK ) +if (se_num ==AMDGPU_INFO_MMR_SE_INDEX_MASK ) { se_num =0xffffffff ; -else if (se_num >=AMDGPU_GFX_MAX_SE ) -return -EINVAL ; -if (sh_num ==AMDGPU_INFO_MMR_SH_INDEX_MASK ) + } +else if (se_num >=AMDGPU_GFX_MAX_SE ) {
Kernel coding style. Put the } on the same line as the else.
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) · line 808
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +7
-2+ ret = -EINVAL ; + goto out; + } + +if (sh_num ==AMDGPU_INFO_MMR_SH_INDEX_MASK ) { sh_num =0xffffffff ; - else if (sh_num >= AMDGPU_GFX_MAX_SH_PER_SE) - return -EINVAL; + } +else if (sh_num >=AMDGPU_GFX_MAX_SH_PER_SE ) {
Same here.
Quoted 45 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +18
-7+ ret = -EINVAL ; + goto out; + } -if (info->read_mmr_reg.count >128 ) -return -EINVAL ; +if (info->read_mmr_reg.count >128 ) { + ret = -EINVAL ; + goto out; + } regs =kmalloc_array (info->read_mmr_reg.count,sizeof (*regs),GFP_KERNEL ); -if (!regs) -return -ENOMEM ; +if (!regs) { + ret = -ENOMEM ; + goto out; + } + alloc_size = info->read_mmr_reg.count *sizeof (*regs);amdgpu_gfx_off_ctrl (adev, false);@@ -816,13 +832,17 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) info->read_mmr_reg.dword_offset + i);kfree (regs);amdgpu_gfx_off_ctrl (adev, true); -return -EFAULT ; + ret = -EFAULT ; + goto out; } }amdgpu_gfx_off_ctrl (adev, true); n =copy_to_user (out, regs,min (size, alloc_size));kfree (regs); -return n ? -EFAULT :0 ; + ret = (n ? -EFAULT :0 ); +out: +up_read (&adev->reset_domain->sem); +return ret; }case AMDGPU_INFO_DEV_INFO : {struct drm_amdgpu_info_device *dev_info; --2.34.1
[AMD Official Use Only - AMD Internal Distribution Only]
Quoted 7 lines
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 1 line · Victor Skvortsov
Quoted 6 lines
Quoted 2 lines
Register access from userspace should be blocked until reset is complete.
While you are at it, you should protect
AMDGPU_INFO_SENSORas well. That queries PMFW which doesn't make sense during reset. A few minor comments below as well.
In general, all PMFW queries are already blocked by pm.dpm_enabled flag. This flag is toggled off in pre_asic_reset, so generally I don't think it will be a problem. Only edge cases I can think of is GFX clock gating that is setup later in hw_resume sequence. But I don't think it warrants this lock.
Quoted 104 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c int amdgpu_info_ioctl(struct drm_device *dev, void
Quoted 14 lines
Signed-off-by Victor Skvortsov <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 44 ++++++++++++++++++------- 1 file changed, 32 insertions(+), 12 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +0
-0@@ -778,6 +778,7 @@ int amdgpu_info_ioctl(struct drm_device *dev, void*data, struct
drm_file*filp)drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c int amdgpu_info_ioctl(struct drm_device *dev,
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +1
-0? -EFAULT :0 ; }case AMDGPU_INFO_READ_MMR_REG : { +int ret =0 ;unsigned int n, alloc_size;uint32_t *regs; unsigned int se_num = (info->read_mmr_reg.instance >>@@ -787,24 +788,39 @@ int amdgpu_info_ioctl(struct drm_device *dev,void *data, struct
drm_file*filp)Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +3
-0AMDGPU_INFO_MMR_SH_INDEX_SHIFT) &AMDGPU_INFO_MMR_SH_INDEX_MASK ; +if (!down_read_trylock (&adev->reset_domain->sem)) +return -ENOENT ; +/* set full masks if the userspace set all bits drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) · line 800
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +3
-4* in the bitfields */ -if (se_num ==AMDGPU_INFO_MMR_SE_INDEX_MASK ) +if (se_num ==AMDGPU_INFO_MMR_SE_INDEX_MASK ) { se_num =0xffffffff ; -else if (se_num >=AMDGPU_GFX_MAX_SE ) -return -EINVAL ; -if (sh_num ==AMDGPU_INFO_MMR_SH_INDEX_MASK ) + } +else if (se_num >=AMDGPU_GFX_MAX_SE ) {Kernel coding style. Put the } on the same line as the else.
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) · line 808
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +7
-2+ ret = -EINVAL ; + goto out; + } + +if (sh_num ==AMDGPU_INFO_MMR_SH_INDEX_MASK ) { sh_num =0xffffffff ; - else if (sh_num >= AMDGPU_GFX_MAX_SH_PER_SE) - return -EINVAL; + } +else if (sh_num >=AMDGPU_GFX_MAX_SH_PER_SE ) {Same here.
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +7
-2+ ret = -EINVAL ; + goto out; + } -if (info->read_mmr_reg.count >128 ) -return -EINVAL ; +if (info->read_mmr_reg.count >128 ) { + ret = -EINVAL ; + goto out; + } regs = kmalloc_array(info->read_mmr_reg.count, sizeof(*regs),
GFP_KERNEL);Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +5
-2-if (!regs) -return -ENOMEM ; +if (!regs) { + ret = -ENOMEM ; + goto out; + } + alloc_size = info->read_mmr_reg.count *sizeof (*regs); amdgpu_gfx_off_ctrl(adev, false); @@ -816,13 +832,17@@ int amdgpu_info_ioctl(struct
drm_device*dev, void *data, struct
drm_file*filp)Quoted 22 lines
info->read_mmr_reg.dword_offset + i); kfree(regs); amdgpu_gfx_off_ctrl(adev, true);drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +6
-3-return -EFAULT ; + ret = -EFAULT ; + goto out; } }amdgpu_gfx_off_ctrl (adev, true); n =copy_to_user (out, regs,min (size, alloc_size));kfree (regs); -return n ? -EFAULT :0 ; + ret = (n ? -EFAULT :0 ); +out: +up_read (&adev->reset_domain->sem); +return ret; }case AMDGPU_INFO_DEV_INFO : {struct drm_amdgpu_info_device *dev_info; --2.34.1
Alex Deucher
Reviewed-byQuoted 10 lines · Victor
[AMD Official Use Only - AMD Internal Distribution Only]
Quoted 7 lines
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 1 line · Victor Skvortsov
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Register access from userspace should be blocked until reset is complete.
While you are at it, you should protect
AMDGPU_INFO_SENSORas well. That queries PMFW which doesn't make sense during reset. A few minor comments below as well.In general, all PMFW queries are already blocked by pm.dpm_enabled flag. This flag is toggled off in
pre_asic_reset, so generally I don't think it will be a problem. Only edge cases I can think of is GFX clock gating that is setup later inhw_resumesequence. But I don't think it warrants this lock.
Fair enough. With the coding style comments below fixed, this patch is:
Quoted 105 lines
Quoted 105 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c int amdgpu_info_ioctl(struct drm_device *dev, void
Quoted 15 lines
Signed-off-by Victor Skvortsov <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 44 ++++++++++++++++++------- 1 file changed, 32 insertions(+), 12 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +0
-0@@ -778,6 +778,7 @@ int amdgpu_info_ioctl(struct drm_device *dev, void*data, struct
drm_file*filp)drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c int amdgpu_info_ioctl(struct drm_device *dev,
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +1
-0? -EFAULT :0 ; }case AMDGPU_INFO_READ_MMR_REG : { +int ret =0 ;unsigned int n, alloc_size;uint32_t *regs; unsigned int se_num = (info->read_mmr_reg.instance >>@@ -787,24 +788,39 @@ int amdgpu_info_ioctl(struct drm_device *dev,void *data, struct
drm_file*filp)Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +3
-0AMDGPU_INFO_MMR_SH_INDEX_SHIFT) &AMDGPU_INFO_MMR_SH_INDEX_MASK ; +if (!down_read_trylock (&adev->reset_domain->sem)) +return -ENOENT ; +/* set full masks if the userspace set all bits drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) · line 800
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +3
-4* in the bitfields */ -if (se_num ==AMDGPU_INFO_MMR_SE_INDEX_MASK ) +if (se_num ==AMDGPU_INFO_MMR_SE_INDEX_MASK ) { se_num =0xffffffff ; -else if (se_num >=AMDGPU_GFX_MAX_SE ) -return -EINVAL ; -if (sh_num ==AMDGPU_INFO_MMR_SH_INDEX_MASK ) + } +else if (se_num >=AMDGPU_GFX_MAX_SE ) {Kernel coding style. Put the } on the same line as the else.
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) · line 808
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +7
-2+ ret = -EINVAL ; + goto out; + } + +if (sh_num ==AMDGPU_INFO_MMR_SH_INDEX_MASK ) { sh_num =0xffffffff ; - else if (sh_num >= AMDGPU_GFX_MAX_SH_PER_SE) - return -EINVAL; + } +else if (sh_num >=AMDGPU_GFX_MAX_SH_PER_SE ) {Same here.
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +7
-2+ ret = -EINVAL ; + goto out; + } -if (info->read_mmr_reg.count >128 ) -return -EINVAL ; +if (info->read_mmr_reg.count >128 ) { + ret = -EINVAL ; + goto out; + } regs = kmalloc_array(info->read_mmr_reg.count, sizeof(*regs),
GFP_KERNEL);Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +5
-2-if (!regs) -return -ENOMEM ; +if (!regs) { + ret = -ENOMEM ; + goto out; + } + alloc_size = info->read_mmr_reg.count *sizeof (*regs); amdgpu_gfx_off_ctrl(adev, false); @@ -816,13 +832,17@@ int amdgpu_info_ioctl(struct
drm_device*dev, void *data, struct
drm_file*filp)Quoted 22 lines
info->read_mmr_reg.dword_offset + i); kfree(regs); amdgpu_gfx_off_ctrl(adev, true);drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +6
-3-return -EFAULT ; + ret = -EFAULT ; + goto out; } }amdgpu_gfx_off_ctrl (adev, true); n =copy_to_user (out, regs,min (size, alloc_size));kfree (regs); -return n ? -EFAULT :0 ; + ret = (n ? -EFAULT :0 ); +out: +up_read (&adev->reset_domain->sem); +return ret; }case AMDGPU_INFO_DEV_INFO : {struct drm_amdgpu_info_device *dev_info; --2.34.1
Christian König
maintainerAm 08.08.24 um 19:48 schrieb Victor Skvortsov:
Quoted 13 lines
Register access from userspace should be blocked until reset is complete.
Signed-off-by Victor Skvortsov <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 44 ++++++++++++++++++------- 1 file changed, 32 insertions(+), 12 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) · line 781
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +1
-0@@ -778,6 +778,7 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) ? -EFAULT :0 ; }case AMDGPU_INFO_READ_MMR_REG : { +int ret =0 ;
In general a good idea, but please fix the coding style. E.g. for example don't initialize ret here.
Regards,
Christian.
Quoted 73 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +31
-12unsigned int n, alloc_size;uint32_t *regs; unsigned int se_num = (info->read_mmr_reg.instance >>@@ -787,24 +788,39 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) AMDGPU_INFO_MMR_SH_INDEX_SHIFT) &AMDGPU_INFO_MMR_SH_INDEX_MASK ; +if (!down_read_trylock (&adev->reset_domain->sem)) +return -ENOENT ; +/* set full masks if the userspace set all bits * in the bitfields */ -if (se_num ==AMDGPU_INFO_MMR_SE_INDEX_MASK ) +if (se_num ==AMDGPU_INFO_MMR_SE_INDEX_MASK ) { se_num =0xffffffff ; -else if (se_num >=AMDGPU_GFX_MAX_SE ) -return -EINVAL ; -if (sh_num ==AMDGPU_INFO_MMR_SH_INDEX_MASK ) + } +else if (se_num >=AMDGPU_GFX_MAX_SE ) { + ret = -EINVAL ; + goto out; + } + +if (sh_num ==AMDGPU_INFO_MMR_SH_INDEX_MASK ) { sh_num =0xffffffff ; -else if (sh_num >=AMDGPU_GFX_MAX_SH_PER_SE ) -return -EINVAL ; + } +else if (sh_num >=AMDGPU_GFX_MAX_SH_PER_SE ) { + ret = -EINVAL ; + goto out; + } -if (info->read_mmr_reg.count >128 ) -return -EINVAL ; +if (info->read_mmr_reg.count >128 ) { + ret = -EINVAL ; + goto out; + } regs =kmalloc_array (info->read_mmr_reg.count,sizeof (*regs),GFP_KERNEL ); -if (!regs) -return -ENOMEM ; +if (!regs) { + ret = -ENOMEM ; + goto out; + } + alloc_size = info->read_mmr_reg.count *sizeof (*regs);amdgpu_gfx_off_ctrl (adev, false);@@ -816,13 +832,17 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) info->read_mmr_reg.dword_offset + i);kfree (regs);amdgpu_gfx_off_ctrl (adev, true); -return -EFAULT ; + ret = -EFAULT ; + goto out; } }amdgpu_gfx_off_ctrl (adev, true); n =copy_to_user (out, regs,min (size, alloc_size));kfree (regs); -return n ? -EFAULT :0 ; + ret = (n ? -EFAULT :0 ); +out: +up_read (&adev->reset_domain->sem); +return ret; }case AMDGPU_INFO_DEV_INFO : {struct drm_amdgpu_info_device *dev_info;