Skip to contentSkip to conversation

drm/amdkfd: potential NULL dereference in kfd_set/reset_event()

PATCH In review Dan Carpenter 2 participants 2 messages Apr 13, 2022

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

PATCH 1/1 drm/amdkfd: potential NULL dereference in kfd_set/reset_event()

If lookup_event_by_id() returns a NULL "ev" pointer then the spin_lock(&ev->lock) will crash. This was detected by Smatch:

drivers/gpu/drm/amd/amdgpu/../amdkfd/kfd_events.c:644 kfd_set_event()
error: we previously assumed 'ev' could be null (see line 639)

Fixes 5273e82c5f47 ("drm/amdkfd: Improve concurrency of event handling")
---
 drivers/gpu/drm/amd/amdkfd/kfd_events.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_events.c +12 -2
index 0fef24b0b915..15570e72a5dd 100644
@@ -634,14 +634,19 @@ int kfd_set_event(struct kfd_process *p, uint32_t event_id)
 	rcu_read_lock();
 
 	ev = lookup_event_by_id(p, event_id);
+	if (!ev) {
+		ret = -EINVAL;
+		goto unlock_rcu;
+	}
 	spin_lock(&ev->lock);
 
-	if (ev && event_can_be_cpu_signaled(ev))
+	if (event_can_be_cpu_signaled(ev))
 		set_event(ev);
 	else
 		ret = -EINVAL;
 
 	spin_unlock(&ev->lock);
+unlock_rcu:
 	rcu_read_unlock();
 	return ret;
 }
@@ -660,14 +665,19 @@ int kfd_reset_event(struct kfd_process *p, uint32_t event_id)
 	rcu_read_lock();
 
 	ev = lookup_event_by_id(p, event_id);
+	if (!ev) {
+		ret = -EINVAL;
+		goto unlock_rcu;
+	}
 	spin_lock(&ev->lock);
 
-	if (ev && event_can_be_cpu_signaled(ev))
+	if (event_can_be_cpu_signaled(ev))
 		reset_event(ev);
 	else
 		ret = -EINVAL;
 
 	spin_unlock(&ev->lock);
+unlock_rcu:
 	rcu_read_unlock();
 	return ret;
 
Signature · 1 line
2.20.1
maintainer Reviewed-by on v1 1/1
#1 lore raw

Am 2022-04-13 um 03:36 schrieb Dan Carpenter:

Quoted 8 lines

If lookup_event_by_id() returns a NULL "ev" pointer then the spin_lock(&ev->lock) will crash. This was detected by Smatch:

drivers/gpu/drm/amd/amdgpu/../amdkfd/kfd_events.c:644 kfd_set_event()
error: we previously assumed 'ev' could be null (see line 639)

Fixes 5273e82c5f47 ("drm/amdkfd: Improve concurrency of event handling")

The patch is

I will apply it to amd-staging-drm-next. Thank you!

Quoted 50 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_events.c | 14 ++++++++++++--
  1 file changed, 12 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_events.c +12 -2
index 0fef24b0b915..15570e72a5dd 100644
@@ -634,14 +634,19 @@ int kfd_set_event(struct kfd_process *p, uint32_t event_id)
  	rcu_read_lock();
 
  	ev = lookup_event_by_id(p, event_id);
+	if (!ev) {
+		ret = -EINVAL;
+		goto unlock_rcu;
+	}
  	spin_lock(&ev->lock);
 
-	if (ev && event_can_be_cpu_signaled(ev))
+	if (event_can_be_cpu_signaled(ev))
  		set_event(ev);
  	else
  		ret = -EINVAL;
 
  	spin_unlock(&ev->lock);
+unlock_rcu:
  	rcu_read_unlock();
  	return ret;
  }
@@ -660,14 +665,19 @@ int kfd_reset_event(struct kfd_process *p, uint32_t event_id)
  	rcu_read_lock();
 
  	ev = lookup_event_by_id(p, event_id);
+	if (!ev) {
+		ret = -EINVAL;
+		goto unlock_rcu;
+	}
  	spin_lock(&ev->lock);
 
-	if (ev && event_can_be_cpu_signaled(ev))
+	if (event_can_be_cpu_signaled(ev))
  		reset_event(ev);
  	else
  		ret = -EINVAL;
 
  	spin_unlock(&ev->lock);
+unlock_rcu:
  	rcu_read_unlock();
  	return ret;