Skip to contentSkip to conversation

protect potential NULL pointer dereferencing

PATCH In review David (Ming Qiang) Wu 3 participants 9 messages Aug 26, 2025 – Aug 27, 2025

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

cover protect potential NULL pointer dereferencing

adding NULL pointer dereferencing protection in these 2 files to address "NULL_RETURNS" found by Coverity scan

David (Ming Qiang) Wu (2):
drm/amdgpu: protect potential NULL pointer dereferencing drm/amdgpu: protect potential NULL pointer dereferencing

 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c     | 22 +++++++++++++------
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   | 12 +++++++---
 2 files changed, 24 insertions(+), 10 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 drm/amdgpu: protect potential NULL pointer dereferencing

to_amdgpu_userq_fence() could return NULL which should be protected.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +9 -3
index a86616c6deeff..01d30544c30d1 100644
@@ -297,15 +297,18 @@ static const char *amdgpu_userq_fence_get_timeline_name(struct dma_fence *f)
 {
 	struct amdgpu_userq_fence *fence = to_amdgpu_userq_fence(f);
 
-	return fence->fence_drv->timeline_name;
+	return fence ? fence->fence_drv->timeline_name : "";
 }
 
 static bool amdgpu_userq_fence_signaled(struct dma_fence *f)
 {
 	struct amdgpu_userq_fence *fence = to_amdgpu_userq_fence(f);
-	struct amdgpu_userq_fence_driver *fence_drv = fence->fence_drv;
+	struct amdgpu_userq_fence_driver *fence_drv;
 	u64 rptr, wptr;
 
+	if (!fence)
+		return false;
+	fence_drv = fence->fence_drv;
 	rptr = amdgpu_userq_fence_read(fence_drv);
 	wptr = fence->base.seqno;
 
@@ -319,8 +322,11 @@ static void amdgpu_userq_fence_free(struct rcu_head *rcu)
 {
 	struct dma_fence *fence = container_of(rcu, struct dma_fence, rcu);
 	struct amdgpu_userq_fence *userq_fence = to_amdgpu_userq_fence(fence);
-	struct amdgpu_userq_fence_driver *fence_drv = userq_fence->fence_drv;
+	struct amdgpu_userq_fence_driver *fence_drv;
 
+	if (!userq_fence)
+		return;
+	fence_drv = userq_fence->fence_drv;
 	/* Release the fence driver reference */
 	amdgpu_userq_fence_driver_put(fence_drv);
 
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/2] drm/amdgpu: protect potential NULL pointer dereferencing

Quoted 3 lines · David (Ming Qiang) Wu

to_amdgpu_userq_fence() could return NULL which should be protected.

Same thing here. The amdgpu_userq_fence is a container for the dma_fence.

Alex

Quoted 47 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +9 -4
index a86616c6deeff..01d30544c30d1 100644
@@ -297,15 +297,18 @@ static const char *amdgpu_userq_fence_get_timeline_name(struct dma_fence *f)
 {
        struct amdgpu_userq_fence *fence = to_amdgpu_userq_fence(f);
 
-       return fence->fence_drv->timeline_name;
+       return fence ? fence->fence_drv->timeline_name : "";
 }
 
 static bool amdgpu_userq_fence_signaled(struct dma_fence *f)
 {
        struct amdgpu_userq_fence *fence = to_amdgpu_userq_fence(f);
-       struct amdgpu_userq_fence_driver *fence_drv = fence->fence_drv;
+       struct amdgpu_userq_fence_driver *fence_drv;
        u64 rptr, wptr;
 
+       if (!fence)
+               return false;
+       fence_drv = fence->fence_drv;
        rptr = amdgpu_userq_fence_read(fence_drv);
        wptr = fence->base.seqno;
 
@@ -319,8 +322,11 @@ static void amdgpu_userq_fence_free(struct rcu_head *rcu)
 {
        struct dma_fence *fence = container_of(rcu, struct dma_fence, rcu);
        struct amdgpu_userq_fence *userq_fence = to_amdgpu_userq_fence(fence);
-       struct amdgpu_userq_fence_driver *fence_drv = userq_fence->fence_drv;
+       struct amdgpu_userq_fence_driver *fence_drv;
 
+       if (!userq_fence)
+               return;
+       fence_drv = userq_fence->fence_drv;
        /* Release the fence driver reference */
        amdgpu_userq_fence_driver_put(fence_drv);
 
--

2.43.0

#3 lore raw

PATCH 2/2 drm/amdgpu: protect potential NULL pointer dereferencing

In reply to David (Ming Qiang) Wu

to_amdgpu_fence() could return NULL pointer which needs to be protected for dereferencing.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +15 -7
index 2d58aefbd68a7..1432b64d379ef 100644
@@ -160,7 +160,9 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f,
 		}
 	}
 
-	to_amdgpu_fence(fence)->start_timestamp = ktime_get();
+	am_fence = to_amdgpu_fence(fence);
+	if (am_fence)
+		am_fence->start_timestamp = ktime_get();
 
 	/* This function can't be called concurrently anyway, otherwise
 	 * emitting the fence would mess up the hardware ring buffer.
@@ -387,6 +389,7 @@ u64 amdgpu_fence_last_unsignaled_time_us(struct amdgpu_ring *ring)
 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
 	struct dma_fence *fence;
 	uint32_t last_seq, sync_seq;
+	struct amdgpu_fence *f;
 
 	last_seq = atomic_read(&ring->fence_drv.last_seq);
 	sync_seq = READ_ONCE(ring->fence_drv.sync_seq);
@@ -399,8 +402,8 @@ u64 amdgpu_fence_last_unsignaled_time_us(struct amdgpu_ring *ring)
 	if (!fence)
 		return 0;
 
-	return ktime_us_delta(ktime_get(),
-		to_amdgpu_fence(fence)->start_timestamp);
+	f = to_amdgpu_fence(fence);
+	return f ? ktime_us_delta(ktime_get(), f->start_timestamp) : 0;
 }
 
 /**
@@ -417,13 +420,16 @@ void amdgpu_fence_update_start_timestamp(struct amdgpu_ring *ring, uint32_t seq,
 {
 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
 	struct dma_fence *fence;
+	struct amdgpu_fence *f;
 
 	seq &= drv->num_fences_mask;
 	fence = drv->fences[seq];
 	if (!fence)
 		return;
 
-	to_amdgpu_fence(fence)->start_timestamp = timestamp;
+	f = to_amdgpu_fence(fence);
+	if (f)
+		f->start_timestamp = timestamp;
 }
 
 /**
@@ -827,7 +833,8 @@ static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence)
 
 static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
 {
-	return (const char *)to_amdgpu_fence(f)->ring->name;
+	struct amdgpu_fence *am_f = to_amdgpu_fence(f);
+	return (const char *) (am_f ? am_f->ring->name : "");
 }
 
 static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f)
@@ -847,8 +854,9 @@ static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f)
  */
 static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
 {
-	if (!timer_pending(&to_amdgpu_fence(f)->ring->fence_drv.fallback_timer))
-		amdgpu_fence_schedule_fallback(to_amdgpu_fence(f)->ring);
+	struct amdgpu_fence *am_f = to_amdgpu_fence(f);
+	if (am_f && !timer_pending(&am_f->ring->fence_drv.fallback_timer))
+		amdgpu_fence_schedule_fallback(am_f->ring);
 
 	return true;
 }
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH 2/2] drm/amdgpu: protect potential NULL pointer dereferencing

Quoted 3 lines · David (Ming Qiang) Wu

to_amdgpu_fence() could return NULL pointer which needs to be protected for dereferencing.

I don't think any of these cases could ever be NULL. The amdgpu_fence is a container for the dma_fence so it will alway be present. See struct amdgpu_fence.

Alex

Quoted 83 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +15 -8
index 2d58aefbd68a7..1432b64d379ef 100644
@@ -160,7 +160,9 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f,
                }
        }
 
-       to_amdgpu_fence(fence)->start_timestamp = ktime_get();
+       am_fence = to_amdgpu_fence(fence);
+       if (am_fence)
+               am_fence->start_timestamp = ktime_get();
 
        /* This function can't be called concurrently anyway, otherwise
         * emitting the fence would mess up the hardware ring buffer.
@@ -387,6 +389,7 @@ u64 amdgpu_fence_last_unsignaled_time_us(struct amdgpu_ring *ring)
        struct amdgpu_fence_driver *drv = &ring->fence_drv;
        struct dma_fence *fence;
        uint32_t last_seq, sync_seq;
+       struct amdgpu_fence *f;
 
        last_seq = atomic_read(&ring->fence_drv.last_seq);
        sync_seq = READ_ONCE(ring->fence_drv.sync_seq);
@@ -399,8 +402,8 @@ u64 amdgpu_fence_last_unsignaled_time_us(struct amdgpu_ring *ring)
        if (!fence)
                return 0;
 
-       return ktime_us_delta(ktime_get(),
-               to_amdgpu_fence(fence)->start_timestamp);
+       f = to_amdgpu_fence(fence);
+       return f ? ktime_us_delta(ktime_get(), f->start_timestamp) : 0;
 }
 
 /**
@@ -417,13 +420,16 @@ void amdgpu_fence_update_start_timestamp(struct amdgpu_ring *ring, uint32_t seq,
 {
        struct amdgpu_fence_driver *drv = &ring->fence_drv;
        struct dma_fence *fence;
+       struct amdgpu_fence *f;
 
        seq &= drv->num_fences_mask;
        fence = drv->fences[seq];
        if (!fence)
                return;
 
-       to_amdgpu_fence(fence)->start_timestamp = timestamp;
+       f = to_amdgpu_fence(fence);
+       if (f)
+               f->start_timestamp = timestamp;
 }
 
 /**
@@ -827,7 +833,8 @@ static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence)
 
 static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
 {
-       return (const char *)to_amdgpu_fence(f)->ring->name;
+       struct amdgpu_fence *am_f = to_amdgpu_fence(f);
+       return (const char *) (am_f ? am_f->ring->name : "");
 }
 
 static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f)
@@ -847,8 +854,9 @@ static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f)
  */
 static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
 {
-       if (!timer_pending(&to_amdgpu_fence(f)->ring->fence_drv.fallback_timer))
-               amdgpu_fence_schedule_fallback(to_amdgpu_fence(f)->ring);
+       struct amdgpu_fence *am_f = to_amdgpu_fence(f);
+       if (am_f && !timer_pending(&am_f->ring->fence_drv.fallback_timer))
+               amdgpu_fence_schedule_fallback(am_f->ring);
 
        return true;
 }
--

2.43.0

#5 lore raw

Re: [PATCH 2/2] drm/amdgpu: protect potential NULL pointer dereferencing

Quoted 6 lines · Alex Deucher
Quoted 2 lines · David (Ming Qiang) Wu

to_amdgpu_fence() could return NULL pointer which needs to be protected for dereferencing.

I don't think any of these cases could ever be NULL. The amdgpu_fence is a container for the dma_fence so it will alway be present. See struct amdgpu_fence.

hmmm... but - the function could return NULL based on base.ops - see below if it should never happen then we should remove the checking. I doubt we will ever return NULL, however someone knowledgeable PLEASE fix it properly. The patch is only to protect it just in case.

static inline struct amdgpu_fence *to_amdgpu_fence(struct dma_fence *f)

{
    struct amdgpu_fence *__f = container_of(f, struct amdgpu_fence, base);

    if (__f->base.ops == &amdgpu_fence_ops ||
        __f->base.ops == &amdgpu_job_fence_ops)
        return __f;

    return NULL;
}

Quoted 85 lines

Alex

Quoted 82 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 22 +++++++++++++++-------
  1 file changed, 15 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +15 -8
index 2d58aefbd68a7..1432b64d379ef 100644
@@ -160,7 +160,9 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f,
                 }
         }
 
-       to_amdgpu_fence(fence)->start_timestamp = ktime_get();
+       am_fence = to_amdgpu_fence(fence);
+       if (am_fence)
+               am_fence->start_timestamp = ktime_get();
 
         /* This function can't be called concurrently anyway, otherwise
          * emitting the fence would mess up the hardware ring buffer.
@@ -387,6 +389,7 @@ u64 amdgpu_fence_last_unsignaled_time_us(struct amdgpu_ring *ring)
         struct amdgpu_fence_driver *drv = &ring->fence_drv;
         struct dma_fence *fence;
         uint32_t last_seq, sync_seq;
+       struct amdgpu_fence *f;
 
         last_seq = atomic_read(&ring->fence_drv.last_seq);
         sync_seq = READ_ONCE(ring->fence_drv.sync_seq);
@@ -399,8 +402,8 @@ u64 amdgpu_fence_last_unsignaled_time_us(struct amdgpu_ring *ring)
         if (!fence)
                 return 0;
 
-       return ktime_us_delta(ktime_get(),
-               to_amdgpu_fence(fence)->start_timestamp);
+       f = to_amdgpu_fence(fence);
+       return f ? ktime_us_delta(ktime_get(), f->start_timestamp) : 0;
  }
 
  /**
@@ -417,13 +420,16 @@ void amdgpu_fence_update_start_timestamp(struct amdgpu_ring *ring, uint32_t seq,
  {
         struct amdgpu_fence_driver *drv = &ring->fence_drv;
         struct dma_fence *fence;
+       struct amdgpu_fence *f;
 
         seq &= drv->num_fences_mask;
         fence = drv->fences[seq];
         if (!fence)
                 return;
 
-       to_amdgpu_fence(fence)->start_timestamp = timestamp;
+       f = to_amdgpu_fence(fence);
+       if (f)
+               f->start_timestamp = timestamp;
  }
 
  /**
@@ -827,7 +833,8 @@ static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence)
 
  static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
  {
-       return (const char *)to_amdgpu_fence(f)->ring->name;
+       struct amdgpu_fence *am_f = to_amdgpu_fence(f);
+       return (const char *) (am_f ? am_f->ring->name : "");
  }
 
  static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f)
@@ -847,8 +854,9 @@ static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f)
   */
  static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
  {
-       if (!timer_pending(&to_amdgpu_fence(f)->ring->fence_drv.fallback_timer))
-               amdgpu_fence_schedule_fallback(to_amdgpu_fence(f)->ring);
+       struct amdgpu_fence *am_f = to_amdgpu_fence(f);
+       if (am_f && !timer_pending(&am_f->ring->fence_drv.fallback_timer))
+               amdgpu_fence_schedule_fallback(am_f->ring);
 
         return true;
  }
--

2.43.0

#6 lore raw

Re: [PATCH 2/2] drm/amdgpu: protect potential NULL pointer dereferencing

Quoted 8 lines · David Wu

On 2025-08-26 16:58, Alex Deucher wrote:

On Tue, Aug 26, 2025 at 3:48 PM David (Ming Qiang) Wu <[email protected]> wrote:

to_amdgpu_fence() could return NULL pointer which needs to be protected for dereferencing.

Quoted 8 lines

I don't think any of these cases could ever be NULL. The amdgpu_fence is a container for the dma_fence so it will alway be present. See struct amdgpu_fence.

hmmm... but - the function could return NULL based on base.ops - see below if it should never happen then we should remove the checking. I doubt we will ever return NULL, however someone knowledgeable PLEASE fix it properly. The patch is only to protect it just in case.

if you look at amdgpu_fence_emit() the fences will only be created with either amdgpu_job_fence_ops or amdgpu_fence_ops so there is no way it will be NULL. It's a false positive.

Alex

Quoted 97 lines

static inline struct amdgpu_fence *to_amdgpu_fence(struct dma_fence *f)

{
    struct amdgpu_fence *__f = container_of(f, struct amdgpu_fence, base);

    if (__f->base.ops == &amdgpu_fence_ops ||
        __f->base.ops == &amdgpu_job_fence_ops)
        return __f;

    return NULL;
}

Alex

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +15 -8
index 2d58aefbd68a7..1432b64d379ef 100644
@@ -160,7 +160,9 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f,
                }
        }
 
-       to_amdgpu_fence(fence)->start_timestamp = ktime_get();
+       am_fence = to_amdgpu_fence(fence);
+       if (am_fence)
+               am_fence->start_timestamp = ktime_get();
 
        /* This function can't be called concurrently anyway, otherwise
         * emitting the fence would mess up the hardware ring buffer.
@@ -387,6 +389,7 @@ u64 amdgpu_fence_last_unsignaled_time_us(struct amdgpu_ring *ring)
        struct amdgpu_fence_driver *drv = &ring->fence_drv;
        struct dma_fence *fence;
        uint32_t last_seq, sync_seq;
+       struct amdgpu_fence *f;
 
        last_seq = atomic_read(&ring->fence_drv.last_seq);
        sync_seq = READ_ONCE(ring->fence_drv.sync_seq);
@@ -399,8 +402,8 @@ u64 amdgpu_fence_last_unsignaled_time_us(struct amdgpu_ring *ring)
        if (!fence)
                return 0;
 
-       return ktime_us_delta(ktime_get(),
-               to_amdgpu_fence(fence)->start_timestamp);
+       f = to_amdgpu_fence(fence);
+       return f ? ktime_us_delta(ktime_get(), f->start_timestamp) : 0;
 }
 
 /**
@@ -417,13 +420,16 @@ void amdgpu_fence_update_start_timestamp(struct amdgpu_ring *ring, uint32_t seq,
 {
        struct amdgpu_fence_driver *drv = &ring->fence_drv;
        struct dma_fence *fence;
+       struct amdgpu_fence *f;
 
        seq &= drv->num_fences_mask;
        fence = drv->fences[seq];
        if (!fence)
                return;
 
-       to_amdgpu_fence(fence)->start_timestamp = timestamp;
+       f = to_amdgpu_fence(fence);
+       if (f)
+               f->start_timestamp = timestamp;
 }
 
 /**
@@ -827,7 +833,8 @@ static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence)
 
 static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
 {
-       return (const char *)to_amdgpu_fence(f)->ring->name;
+       struct amdgpu_fence *am_f = to_amdgpu_fence(f);
+       return (const char *) (am_f ? am_f->ring->name : "");
 }
 
 static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f)
@@ -847,8 +854,9 @@ static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f)
  */
 static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
 {
-       if (!timer_pending(&to_amdgpu_fence(f)->ring->fence_drv.fallback_timer))
-               amdgpu_fence_schedule_fallback(to_amdgpu_fence(f)->ring);
+       struct amdgpu_fence *am_f = to_amdgpu_fence(f);
+       if (am_f && !timer_pending(&am_f->ring->fence_drv.fallback_timer))
+               amdgpu_fence_schedule_fallback(am_f->ring);
 
        return true;
 }
--

2.43.0

#7 lore raw

Re: [PATCH 2/2] drm/amdgpu: protect potential NULL pointer dereferencing

Quoted 12 lines · Alex Deucher
Quoted 11 lines · David Wu

On 2025-08-26 16:58, Alex Deucher wrote:

On Tue, Aug 26, 2025 at 3:48 PM David (Ming Qiang) Wu <[email protected]> wrote:

to_amdgpu_fence() could return NULL pointer which needs to be protected for dereferencing.

I don't think any of these cases could ever be NULL. The amdgpu_fence is a container for the dma_fence so it will alway be present. See struct amdgpu_fence.

Quoted 7 lines
Quoted 4 lines

hmmm... but - the function could return NULL based on base.ops - see below if it should never happen then we should remove the checking. I doubt we will ever return NULL, however someone knowledgeable PLEASE fix it properly. The patch is only to protect it just in case.

if you look at amdgpu_fence_emit() the fences will only be created with either amdgpu_job_fence_ops or amdgpu_fence_ops so there is no way it will be NULL. It's a false positive.

My proposition is  there is no guarantee for future even though it is a false positive now as the original code is not confident either about the condition should never met. I think there is no harm to protect it. Or better I can come up with another patch to remove the checking - I feel keeping NULL without being handled is not acceptable (at least to silence Coverity).
Alex - please suggest - either removing the checking or do nothing. I am fine either way.
David

Quoted 98 lines

Alex

Quoted 96 lines

static inline struct amdgpu_fence *to_amdgpu_fence(struct dma_fence *f)

{
     struct amdgpu_fence *__f = container_of(f, struct amdgpu_fence, base);

     if (__f->base.ops == &amdgpu_fence_ops ||
         __f->base.ops == &amdgpu_job_fence_ops)
         return __f;

     return NULL;
}

Alex

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 22 +++++++++++++++-------
  1 file changed, 15 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +15 -8
index 2d58aefbd68a7..1432b64d379ef 100644
@@ -160,7 +160,9 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f,
                 }
         }
 
-       to_amdgpu_fence(fence)->start_timestamp = ktime_get();
+       am_fence = to_amdgpu_fence(fence);
+       if (am_fence)
+               am_fence->start_timestamp = ktime_get();
 
         /* This function can't be called concurrently anyway, otherwise
          * emitting the fence would mess up the hardware ring buffer.
@@ -387,6 +389,7 @@ u64 amdgpu_fence_last_unsignaled_time_us(struct amdgpu_ring *ring)
         struct amdgpu_fence_driver *drv = &ring->fence_drv;
         struct dma_fence *fence;
         uint32_t last_seq, sync_seq;
+       struct amdgpu_fence *f;
 
         last_seq = atomic_read(&ring->fence_drv.last_seq);
         sync_seq = READ_ONCE(ring->fence_drv.sync_seq);
@@ -399,8 +402,8 @@ u64 amdgpu_fence_last_unsignaled_time_us(struct amdgpu_ring *ring)
         if (!fence)
                 return 0;
 
-       return ktime_us_delta(ktime_get(),
-               to_amdgpu_fence(fence)->start_timestamp);
+       f = to_amdgpu_fence(fence);
+       return f ? ktime_us_delta(ktime_get(), f->start_timestamp) : 0;
  }
 
  /**
@@ -417,13 +420,16 @@ void amdgpu_fence_update_start_timestamp(struct amdgpu_ring *ring, uint32_t seq,
  {
         struct amdgpu_fence_driver *drv = &ring->fence_drv;
         struct dma_fence *fence;
+       struct amdgpu_fence *f;
 
         seq &= drv->num_fences_mask;
         fence = drv->fences[seq];
         if (!fence)
                 return;
 
-       to_amdgpu_fence(fence)->start_timestamp = timestamp;
+       f = to_amdgpu_fence(fence);
+       if (f)
+               f->start_timestamp = timestamp;
  }
 
  /**
@@ -827,7 +833,8 @@ static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence)
 
  static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
  {
-       return (const char *)to_amdgpu_fence(f)->ring->name;
+       struct amdgpu_fence *am_f = to_amdgpu_fence(f);
+       return (const char *) (am_f ? am_f->ring->name : "");
  }
 
  static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f)
@@ -847,8 +854,9 @@ static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f)
   */
  static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
  {
-       if (!timer_pending(&to_amdgpu_fence(f)->ring->fence_drv.fallback_timer))
-               amdgpu_fence_schedule_fallback(to_amdgpu_fence(f)->ring);
+       struct amdgpu_fence *am_f = to_amdgpu_fence(f);
+       if (am_f && !timer_pending(&am_f->ring->fence_drv.fallback_timer))
+               amdgpu_fence_schedule_fallback(am_f->ring);
 
         return true;
  }
--

2.43.0

#8 lore raw

Re: [PATCH 2/2] drm/amdgpu: protect potential NULL pointer dereferencing

Quoted 22 lines · David Wu
Quoted 12 lines · Alex Deucher
Quoted 11 lines · David Wu

On 2025-08-26 16:58, Alex Deucher wrote:

On Tue, Aug 26, 2025 at 3:48 PM David (Ming Qiang) Wu <[email protected]> wrote:

to_amdgpu_fence() could return NULL pointer which needs to be protected for dereferencing.

I don't think any of these cases could ever be NULL. The amdgpu_fence is a container for the dma_fence so it will alway be present. See struct amdgpu_fence.

Quoted 7 lines
Quoted 4 lines

hmmm... but - the function could return NULL based on base.ops - see below if it should never happen then we should remove the checking. I doubt we will ever return NULL, however someone knowledgeable PLEASE fix it properly. The patch is only to protect it just in case.

if you look at amdgpu_fence_emit() the fences will only be created with either amdgpu_job_fence_ops or amdgpu_fence_ops so there is no way it will be NULL. It's a false positive.

Quoted 9 lines

My proposition is there is no guarantee for future even though it is a false positive now as the original code is not
confident either about the condition should never met. I think there is no harm to protect it. Or better I can come up with
another patch to remove the checking - I feel keeping NULL without being handled is not acceptable (at least to silence
Coverity).
Alex - please suggest - either removing the checking or do nothing. I am fine either way.

Ideally we'd clean up the fence handling so that we don't need separate fence ops for the job and non-job case. Then we could remove the checks in to_amdgpu_fence(). At this point we could remove the checks in there anyway. I think it was mostly just added on the off chance someone tries to lift that helper out of amdgpu_fence.c and tries to use it for other cases.

Alex

Quoted 99 lines

David

Quoted 98 lines

Alex

Quoted 96 lines

static inline struct amdgpu_fence *to_amdgpu_fence(struct dma_fence *f)

{
     struct amdgpu_fence *__f = container_of(f, struct amdgpu_fence, base);

     if (__f->base.ops == &amdgpu_fence_ops ||
         __f->base.ops == &amdgpu_job_fence_ops)
         return __f;

     return NULL;
}

Alex

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 22 +++++++++++++++-------
  1 file changed, 15 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +15 -8
index 2d58aefbd68a7..1432b64d379ef 100644
@@ -160,7 +160,9 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f,
                 }
         }
 
-       to_amdgpu_fence(fence)->start_timestamp = ktime_get();
+       am_fence = to_amdgpu_fence(fence);
+       if (am_fence)
+               am_fence->start_timestamp = ktime_get();
 
         /* This function can't be called concurrently anyway, otherwise
          * emitting the fence would mess up the hardware ring buffer.
@@ -387,6 +389,7 @@ u64 amdgpu_fence_last_unsignaled_time_us(struct amdgpu_ring *ring)
         struct amdgpu_fence_driver *drv = &ring->fence_drv;
         struct dma_fence *fence;
         uint32_t last_seq, sync_seq;
+       struct amdgpu_fence *f;
 
         last_seq = atomic_read(&ring->fence_drv.last_seq);
         sync_seq = READ_ONCE(ring->fence_drv.sync_seq);
@@ -399,8 +402,8 @@ u64 amdgpu_fence_last_unsignaled_time_us(struct amdgpu_ring *ring)
         if (!fence)
                 return 0;
 
-       return ktime_us_delta(ktime_get(),
-               to_amdgpu_fence(fence)->start_timestamp);
+       f = to_amdgpu_fence(fence);
+       return f ? ktime_us_delta(ktime_get(), f->start_timestamp) : 0;
  }
 
  /**
@@ -417,13 +420,16 @@ void amdgpu_fence_update_start_timestamp(struct amdgpu_ring *ring, uint32_t seq,
  {
         struct amdgpu_fence_driver *drv = &ring->fence_drv;
         struct dma_fence *fence;
+       struct amdgpu_fence *f;
 
         seq &= drv->num_fences_mask;
         fence = drv->fences[seq];
         if (!fence)
                 return;
 
-       to_amdgpu_fence(fence)->start_timestamp = timestamp;
+       f = to_amdgpu_fence(fence);
+       if (f)
+               f->start_timestamp = timestamp;
  }
 
  /**
@@ -827,7 +833,8 @@ static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence)
 
  static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
  {
-       return (const char *)to_amdgpu_fence(f)->ring->name;
+       struct amdgpu_fence *am_f = to_amdgpu_fence(f);
+       return (const char *) (am_f ? am_f->ring->name : "");
  }
 
  static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f)
@@ -847,8 +854,9 @@ static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f)
   */
  static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
  {
-       if (!timer_pending(&to_amdgpu_fence(f)->ring->fence_drv.fallback_timer))
-               amdgpu_fence_schedule_fallback(to_amdgpu_fence(f)->ring);
+       struct amdgpu_fence *am_f = to_amdgpu_fence(f);
+       if (am_f && !timer_pending(&am_f->ring->fence_drv.fallback_timer))
+               amdgpu_fence_schedule_fallback(am_f->ring);
 
         return true;
  }
--

2.43.0