Skip to contentSkip to conversation

drm/amdkfd: sparse: Fix warning in reading SDMA counters

PATCH Superseded Mukul Joshi 2 participants 2 messages Aug 17, 2020 – Aug 18, 2020

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 drm/amdkfd: sparse: Fix warning in reading SDMA counters

Add __user annotation to fix related sparse warning while reading SDMA counters from userland.

---
 drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +3 -6
index e0e60b0d0669..e2894967c372 100644
@@ -157,19 +157,16 @@ int read_sdma_queue_counter(uint64_t q_rptr, uint64_t *val)
 {
 	int ret;
 	uint64_t tmp = 0;
+	uint64_t __user *sdma_usage_cntr;
 
 	if (!val)
 		return -EINVAL;
 	/*
 	 * SDMA activity counter is stored at queue's RPTR + 0x8 location.
 	 */
-	if (!access_ok((const void __user *)(q_rptr +
-					sizeof(uint64_t)), sizeof(uint64_t))) {
-		pr_err("Can't access sdma queue activity counter\n");
-		return -EFAULT;
-	}
+	sdma_usage_cntr = (uint64_t __user *)q_rptr + 1;
 
-	ret = get_user(tmp, (uint64_t *)(q_rptr + sizeof(uint64_t)));
+	ret = get_user(tmp, sdma_usage_cntr);
 	if (!ret) {
 		*val = tmp;
 	}
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
maintainer
#1 lore raw

Sorry, more bike-shedding.

Am 2020-08-17 um 7:58 p.m. schrieb Mukul Joshi:

Quoted 14 lines

Add __user annotation to fix related sparse warning while reading SDMA counters from userland.

---
 drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +0 -0
index e0e60b0d0669..e2894967c372 100644

drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c int read_sdma_queue_counter(uint64_t q_rptr, uint64_t *val) · line 163

Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1 -0
@@ -157,19 +157,16 @@ int read_sdma_queue_counter(uint64_t q_rptr, uint64_t *val)
 {
 	int ret;
 	uint64_t tmp = 0;
+	uint64_t __user *sdma_usage_cntr;
 
 	if (!val)
 		return -EINVAL;

Maybe this check isn't needed. Both callers pass in pointers to local variables. If a caller gets that wrong, how likely are they going to handle the error code correctly?

drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c int read_sdma_queue_counter(uint64_t q_rptr, uint64_t *val) · line 169

Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2 -6
 	/*
 	 * SDMA activity counter is stored at queue's RPTR + 0x8 location.
 	 */
-	if (!access_ok((const void __user *)(q_rptr +
-					sizeof(uint64_t)), sizeof(uint64_t))) {
-		pr_err("Can't access sdma queue activity counter\n");
-		return -EFAULT;
-	}
+	sdma_usage_cntr = (uint64_t __user *)q_rptr + 1;
 
-	ret = get_user(tmp, (uint64_t *)(q_rptr + sizeof(uint64_t)));
+	ret = get_user(tmp, sdma_usage_cntr);

Maybe you don't need sdma_usage_cntr. Just inline the pointer arithmetic. And I'm not sure why you need tmp either. Is it in case the read gets only one dword and fails on the second one? The callers will ignore the value, if you return an error, so I don't think it matters. So this whole function would become very simple:

	return get_user(*val, (uint64_t __user *)q_rptr + 1);

Now it could probably be a static inline function in a kfd_device_queue_manager.h.

If that is the only use of q_rptr in the function, why not make the parameter type uint64_t __user *, so you don't even need the type cast in here? You can also change the type in struct temp_sdma_queue_list to match.

While you're at it, you could also change the types of read_ptr and write_ptr in struct queue_properties, because uint32_t * is really not the correct type. It's a pointer to the wrong size and the wrong address space. Though that change may have a few more ripple effects.

Regards,
  Felix

drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c int read_sdma_queue_counter(uint64_t q_rptr, uint64_t *val) · line 172

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +0 -0
 	if (!ret) {
 		*val = tmp;
 	}

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx