Skip to contentSkip to conversation

drm/sched: Fix UB pointer dereference

PATCH In review Philipp Stanner 3 participants 5 messages Aug 27, 2024

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

PATCH 1/1 drm/sched: Fix UB pointer dereference

In drm_sched_job_init(), commit 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues") implemented a call to drm_err(), which uses the job's scheduler pointer as a parameter. job->sched, however, is not yet valid as it gets set by drm_sched_job_arm(), which is always called after drm_sched_job_init().

Since the scheduler code has no control over how the API-User has allocated or set 'job', the pointer's dereference is undefined behavior.

Fix the UB by replacing drm_err() with pr_err().

Cc <[email protected]> # 6.7+
Fixes 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues")
---
 drivers/gpu/drm/scheduler/sched_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +1 -1
index 7e90c9f95611..356c30fa24a8 100644
@@ -797,7 +797,7 @@ int drm_sched_job_init(struct drm_sched_job *job,
 		 * or worse--a blank screen--leave a trail in the
 		 * logs, so this can be debugged easier.
 		 */
-		drm_err(job->sched, "%s: entity has no rq!\n", __func__);
+		pr_err("*ERROR* %s: entity has no rq!\n", __func__);
 		return -ENOENT;
 	}
 
Signature · 1 line
2.46.0
#1 lore raw

Am 27.08.24 um 09:45 schrieb Philipp Stanner:

Quoted 9 lines

In drm_sched_job_init(), commit 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues") implemented a call to drm_err(), which uses the job's scheduler pointer as a parameter. job->sched, however, is not yet valid as it gets set by drm_sched_job_arm(), which is always called after drm_sched_job_init().

Since the scheduler code has no control over how the API-User has allocated or set 'job', the pointer's dereference is undefined behavior.

Quoted 7 lines

Fix the UB by replacing drm_err() with pr_err().

Cc <[email protected]> # 6.7+
Fixes 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues")

Good catch, Reviewed-by: Christian König <[email protected]>

Quoted 17 lines
---
  drivers/gpu/drm/scheduler/sched_main.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +1 -1
index 7e90c9f95611..356c30fa24a8 100644
@@ -797,7 +797,7 @@ int drm_sched_job_init(struct drm_sched_job *job,
  		 * or worse--a blank screen--leave a trail in the
  		 * logs, so this can be debugged easier.
  		 */
-		drm_err(job->sched, "%s: entity has no rq!\n", __func__);
+		pr_err("*ERROR* %s: entity has no rq!\n", __func__);
  		return -ENOENT;
  	}
 
maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Philipp Stanner
Quoted 25 lines · Philipp Stanner

In drm_sched_job_init(), commit 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues") implemented a call to drm_err(), which uses the job's scheduler pointer as a parameter. job->sched, however, is not yet valid as it gets set by drm_sched_job_arm(), which is always called after drm_sched_job_init().

Since the scheduler code has no control over how the API-User has allocated or set 'job', the pointer's dereference is undefined behavior.

Fix the UB by replacing drm_err() with pr_err().

Cc <[email protected]> # 6.7+
Fixes 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues")
---
  drivers/gpu/drm/scheduler/sched_main.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 7e90c9f95611..356c30fa24a8 100644

drivers/gpu/drm/scheduler/sched_main.c int drm_sched_job_init(struct drm_sched_job *job, · line 800

Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +1 -1
@@ -797,7 +797,7 @@ int drm_sched_job_init(struct drm_sched_job *job,
  		 * or worse--a blank screen--leave a trail in the
  		 * logs, so this can be debugged easier.
  		 */
-		drm_err(job->sched, "%s: entity has no rq!\n", __func__);
+		pr_err("*ERROR* %s: entity has no rq!\n", __func__);

I don't think the "*ERROR*" string is necessary, it's pr_err() already.

Otherwise,

Quoted 3 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
  		return -ENOENT;
  	}
 
#3 lore raw

Am 27.08.24 um 10:39 schrieb Danilo Krummrich:

Quoted 21 lines · Philipp Stanner
Quoted 20 lines

In drm_sched_job_init(), commit 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues") implemented a call to drm_err(), which uses the job's scheduler pointer as a parameter. job->sched, however, is not yet valid as it gets set by drm_sched_job_arm(), which is always called after drm_sched_job_init().

Since the scheduler code has no control over how the API-User has allocated or set 'job', the pointer's dereference is undefined behavior.

Fix the UB by replacing drm_err() with pr_err().

Cc <[email protected]>    # 6.7+
Fixes 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues")

---
  drivers/gpu/drm/scheduler/sched_main.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

drivers/gpu/drm/scheduler/sched_main.c int drm_sched_job_init(struct drm_sched_job *job, · line 800

Quoted 10 lines
drivers/gpu/drm/scheduler/sched_main.c +1 -1
index 7e90c9f95611..356c30fa24a8 100644
@@ -797,7 +797,7 @@ int drm_sched_job_init(struct drm_sched_job *job,
            * or worse--a blank screen--leave a trail in the
            * logs, so this can be debugged easier.
            */
-        drm_err(job->sched, "%s: entity has no rq!\n", __func__);
+        pr_err("*ERROR* %s: entity has no rq!\n", __func__);

I don't think the "*ERROR*" string is necessary, it's pr_err() already.

Good point. I will remove that and also add a comment why drm_err won't work here before pushing it to drm-misc-fixes.

Thanks,
Christian.

Quoted 7 lines

Otherwise,

Quoted 2 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
           return -ENOENT;
       }
#4 lore raw
Quoted 45 lines · Christian König

Am 27.08.24 um 10:39 schrieb Danilo Krummrich:

Quoted 27 lines · Philipp Stanner
Quoted 26 lines

In drm_sched_job_init(), commit 56e449603f0a ("drm/sched: Convert the
GPU scheduler to variable number of run-queues") implemented a call to
drm_err(), which uses the job's scheduler pointer as a parameter. job->sched, however, is not yet valid as it gets set by drm_sched_job_arm(), which is always called after drm_sched_job_init().

Since the scheduler code has no control over how the API-User has allocated or set 'job', the pointer's dereference is undefined behavior.

Fix the UB by replacing drm_err() with pr_err().

Cc <[email protected]>    # 6.7+
Fixes 56e449603f0a ("drm/sched: Convert the GPU scheduler to

variable number of run-queues")

Closes:
https://lore.kernel.org/lkml/[email protected]/

---
  drivers/gpu/drm/scheduler/sched_main.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines

drivers/gpu/drm/scheduler/sched_main.c int drm_sched_job_init(struct drm_sched_job · line 802

Quoted 13 lines
drivers/gpu/drm/scheduler/sched_main.c +1 -1
index 7e90c9f95611..356c30fa24a8 100644
@@ -797,7 +797,7 @@ int drm_sched_job_init(struct drm_sched_job
 *job,
            * or worse--a blank screen--leave a trail in the
            * logs, so this can be debugged easier.
            */
-        drm_err(job->sched, "%s: entity has no rq!\n",
 __func__);
+        pr_err("*ERROR* %s: entity has no rq!\n", __func__);

I don't think the "*ERROR*" string is necessary, it's pr_err() already.

Quoted 3 lines

Good point. I will remove that and also add a comment why drm_err won't
work here before pushing it to drm-misc-fixes.

Well, as we're at it I want to point out that the exact same mechanism occurs just a few lines below, from where I shamelessly copied it:

if (unlikely(!credits)) {
	pr_err("*ERROR* %s: credits cannot be 0!\n", __func__);

P.

Quoted 12 lines

Thanks,
Christian.

Quoted 7 lines

Otherwise,

Quoted 2 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
           return -ENOENT;

      }