Skip to contentSkip to conversation

drm/amdgpu: return -EFAULT from AMDGPU_INFO_UQ_FW_AREAS

PATCH In review Jesse Zhang 2 participants 5 messages Oct 9

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 drm/amdgpu: return -EFAULT from AMDGPU_INFO_UQ_FW_AREAS

The UQ_FW_AREAS query computes the copy_to_user() result into ret and then returns 0 anyway, so userspace never learns that the metadata was not written. With an invalid return pointer all three IP types (GFX, COMPUTE, DMA) report success on Navi31.

Return the copy_to_user() result like the other INFO queries do.

Fixes 44b69cf1d35c ("drm/amdgpu: Update AMDGPU_INFO_UQ_FW_AREAS query for compute")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +6 -9
index 1589b6a6575f..b98091f31dda 100644
@@ -1430,25 +1430,22 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 			if (ret)
 				return ret;
 
-			ret = copy_to_user(out, &meta_info,
-						min((size_t)size, sizeof(meta_info))) ? -EFAULT : 0;
-			return 0;
+			return copy_to_user(out, &meta_info,
+					    min((size_t)size, sizeof(meta_info))) ? -EFAULT : 0;
 		case AMDGPU_HW_IP_COMPUTE:
 			ret = amdgpu_userq_metadata_info_compute(adev, info, &meta_info.compute);
 			if (ret)
 				return ret;
 
-			ret = copy_to_user(out, &meta_info,
-						min((size_t)size, sizeof(meta_info))) ? -EFAULT : 0;
-			return 0;
+			return copy_to_user(out, &meta_info,
+					    min((size_t)size, sizeof(meta_info))) ? -EFAULT : 0;
 		case AMDGPU_HW_IP_DMA:
 			ret = amdgpu_userq_metadata_info_sdma(adev, info, &meta_info.sdma);
 			if (ret)
 				return ret;
 
-			ret = copy_to_user(out, &meta_info,
-						min((size_t)size, sizeof(meta_info))) ? -EFAULT : 0;
-			return 0;
+			return copy_to_user(out, &meta_info,
+					    min((size_t)size, sizeof(meta_info))) ? -EFAULT : 0;
 		default:
 			return -EINVAL;
 		}
Signature · 1 line
2.50.1
#1 lore raw

PATCH 2/3 drm/amdgpu: don't drop the runtime PM reference twice in amdgpu_gpr

amdgpu_debugfs_gpr_read() drops its runtime PM reference right after reading the wave registers. When copying them to userspace fails, it jumps to the err label, which drops the reference a second time. A read into a bad buffer on Navi31 either steals a reference somebody else holds (power/runtime_usage goes from 1 to 0) or underflows the count:

amdgpu 0000:03:00.0: Runtime PM usage count underflow!

The registers are already in the kernel buffer at that point, so finish the hardware access first and copy the buffer with a single copy_to_user() instead of the put_user() loop with its own error path.

Fixes 9eee152aab56 ("drm/amdgpu/debugfs: fix ref count leak when pm_runtime_get_sync fails")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +3 -18
index 35cbd358c28c..3dc95d9accf2 100644
@@ -1267,7 +1267,6 @@ static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
 {
 	struct amdgpu_device *adev = f->f_inode->i_private;
 	int r;
-	ssize_t result = 0;
 	uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
 
 	if (size > 4096 || size & 3 || *pos & 3)
@@ -1311,25 +1310,11 @@ static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
 	mutex_unlock(&adev->grbm_idx_mutex);
 
 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
+	amdgpu_virt_disable_access_debugfs(adev);
 
-	while (size) {
-		uint32_t value;
-
-		value = data[result >> 2];
-		r = put_user(value, (uint32_t *)buf);
-		if (r) {
-			amdgpu_virt_disable_access_debugfs(adev);
-			goto err;
-		}
-
-		result += 4;
-		buf += 4;
-		size -= 4;
-	}
-
+	r = copy_to_user(buf, data, size) ? -EFAULT : size;
 	kfree(data);
-	amdgpu_virt_disable_access_debugfs(adev);
-	return result;
+	return r;
 
 err:
 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
Signature · 1 line
2.50.1
#2 lore raw

PATCH 3/3 drm/amdgpu: free the userptr ranges when a CS fails after parser_bos

In reply to Jesse Zhang

amdgpu_cs_parser_bos() allocates an HMM range for every userptr entry of the BO list and looks up the user pages. Those ranges are only freed when amdgpu_cs_parser_bos() itself fails or when amdgpu_cs_submit() checks them under the notifier lock. If amdgpu_cs_patch_jobs(), amdgpu_cs_vm_handling(), amdgpu_cs_sync_rings() or the early part of amdgpu_cs_submit() fails, nothing frees them: neither amdgpu_cs_parser_fini() nor amdgpu_bo_list_free() touch e->range.

Each such CS then leaks the range, its hmm_pfns array (8 bytes per page of the userptr BO) and the BO reference the range holds. An IB on a ring that patches the CS in place (e.g. VCN) with an unmapped va_start fails in amdgpu_cs_patch_ibs() and takes this path.

Free the ranges on those error paths. Ranges amdgpu_cs_submit() already dropped are NULL, so freeing them again is a no-op.

Fixes fec8fdb54e8f ("drm/amdgpu: fix userptr HMM range handling v2")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +11 -4
index d059e0c92866..d3c1b4b9472f 100644
@@ -1435,6 +1435,7 @@ static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser)
 int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 {
 	struct amdgpu_device *adev = drm_to_adev(dev);
+	struct amdgpu_bo_list_entry *e;
 	struct amdgpu_cs_parser parser;
 	int r;
 
@@ -1475,25 +1476,31 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 
 	r = amdgpu_cs_patch_jobs(&parser);
 	if (r)
-		goto error_fini;
+		goto error_free_user_pages;
 
 	r = amdgpu_cs_vm_handling(&parser);
 	if (r)
-		goto error_fini;
+		goto error_free_user_pages;
 
 	r = amdgpu_cs_sync_rings(&parser);
 	if (r)
-		goto error_fini;
+		goto error_free_user_pages;
 
 	trace_amdgpu_cs_ibs(&parser);
 
 	r = amdgpu_cs_submit(&parser, data);
 	if (r)
-		goto error_fini;
+		goto error_free_user_pages;
 
 	amdgpu_cs_parser_fini(&parser);
 	return 0;
 
+error_free_user_pages:
+	/* amdgpu_cs_submit() only drops the ranges once it got that far. */
+	amdgpu_bo_list_for_each_userptr_entry(e, parser.bo_list) {
+		amdgpu_hmm_range_free(e->range);
+		e->range = NULL;
+	}
 error_fini:
 	amdgpu_cs_parser_fini(&parser);
 	return r;
Signature · 1 line
2.50.1
Reviewed-by on v1 3/3
#3 lore raw

Re: [PATCH 3/3] drm/amdgpu: free the userptr ranges when a CS fails after parser_bos

Series is:

Quoted 74 lines · Jesse Zhang

amdgpu_cs_parser_bos() allocates an HMM range for every userptr entry of the BO list and looks up the user pages. Those ranges are only freed when amdgpu_cs_parser_bos() itself fails or when amdgpu_cs_submit() checks them under the notifier lock. If amdgpu_cs_patch_jobs(), amdgpu_cs_vm_handling(), amdgpu_cs_sync_rings() or the early part of amdgpu_cs_submit() fails, nothing frees them: neither amdgpu_cs_parser_fini() nor amdgpu_bo_list_free() touch e->range.

Each such CS then leaks the range, its hmm_pfns array (8 bytes per page of the userptr BO) and the BO reference the range holds. An IB on a ring that patches the CS in place (e.g. VCN) with an unmapped va_start fails in amdgpu_cs_patch_ibs() and takes this path.

Free the ranges on those error paths. Ranges amdgpu_cs_submit() already dropped are NULL, so freeing them again is a no-op.

Fixes fec8fdb54e8f ("drm/amdgpu: fix userptr HMM range handling v2")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +11 -5
index d059e0c92866..d3c1b4b9472f 100644
@@ -1435,6 +1435,7 @@ static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser)
 int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 {
        struct amdgpu_device *adev = drm_to_adev(dev);
+       struct amdgpu_bo_list_entry *e;
        struct amdgpu_cs_parser parser;
        int r;
 
@@ -1475,25 +1476,31 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 
        r = amdgpu_cs_patch_jobs(&parser);
        if (r)
-               goto error_fini;
+               goto error_free_user_pages;
 
        r = amdgpu_cs_vm_handling(&parser);
        if (r)
-               goto error_fini;
+               goto error_free_user_pages;
 
        r = amdgpu_cs_sync_rings(&parser);
        if (r)
-               goto error_fini;
+               goto error_free_user_pages;
 
        trace_amdgpu_cs_ibs(&parser);
 
        r = amdgpu_cs_submit(&parser, data);
        if (r)
-               goto error_fini;
+               goto error_free_user_pages;
 
        amdgpu_cs_parser_fini(&parser);
        return 0;
 
+error_free_user_pages:
+       /* amdgpu_cs_submit() only drops the ranges once it got that far. */
+       amdgpu_bo_list_for_each_userptr_entry(e, parser.bo_list) {
+               amdgpu_hmm_range_free(e->range);
+               e->range = NULL;
+       }
 error_fini:
        amdgpu_cs_parser_fini(&parser);
        return r;
--

2.50.1

Reviewed-by on v1 1/3
#4 lore raw
In reply to Jesse Zhang
Quoted 10 lines · Jesse Zhang

The UQ_FW_AREAS query computes the copy_to_user() result into ret and then returns 0 anyway, so userspace never learns that the metadata was not written. With an invalid return pointer all three IP types (GFX, COMPUTE, DMA) report success on Navi31.

Return the copy_to_user() result like the other INFO queries do.

Fixes 44b69cf1d35c ("drm/amdgpu: Update AMDGPU_INFO_UQ_FW_AREAS query for compute")
Quoted 43 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +6 -10
index 1589b6a6575f..b98091f31dda 100644
@@ -1430,25 +1430,22 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
                        if (ret)
                                return ret;
 
-                       ret = copy_to_user(out, &meta_info,
-                                               min((size_t)size, sizeof(meta_info))) ? -EFAULT : 0;
-                       return 0;
+                       return copy_to_user(out, &meta_info,
+                                           min((size_t)size, sizeof(meta_info))) ? -EFAULT : 0;
                case AMDGPU_HW_IP_COMPUTE:
                        ret = amdgpu_userq_metadata_info_compute(adev, info, &meta_info.compute);
                        if (ret)
                                return ret;
 
-                       ret = copy_to_user(out, &meta_info,
-                                               min((size_t)size, sizeof(meta_info))) ? -EFAULT : 0;
-                       return 0;
+                       return copy_to_user(out, &meta_info,
+                                           min((size_t)size, sizeof(meta_info))) ? -EFAULT : 0;
                case AMDGPU_HW_IP_DMA:
                        ret = amdgpu_userq_metadata_info_sdma(adev, info, &meta_info.sdma);
                        if (ret)
                                return ret;
 
-                       ret = copy_to_user(out, &meta_info,
-                                               min((size_t)size, sizeof(meta_info))) ? -EFAULT : 0;
-                       return 0;
+                       return copy_to_user(out, &meta_info,
+                                           min((size_t)size, sizeof(meta_info))) ? -EFAULT : 0;
                default:
                        return -EINVAL;
                }
--

2.50.1