Skip to contentSkip to conversation

drm/sched: fix potential page fault in drm_sched_job_init()

PATCH In review Danilo Krummrich 2 participants 8 messages Nov 8, 2023 – Nov 9, 2023

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

PATCH 1/1 drm/sched: fix potential page fault in drm_sched_job_init()

Commit 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues") introduces drm_err() in drm_sched_job_init(), in order to indicate that the given entity has no runq, however at this time job->sched is not yet set, likely to be NULL initialized, and hence shouldn't be used.

Replace the corresponding drm_err() call with pr_err() to avoid a potential page fault.

While at it, extend the documentation of drm_sched_job_init() to indicate that job->sched is not a valid pointer until drm_sched_job_arm() has been called.

Fixes 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues")
---
 drivers/gpu/drm/scheduler/sched_main.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +4 -1
index 27843e37d9b7..dd28389f0ddd 100644
@@ -680,6 +680,9 @@ EXPORT_SYMBOL(drm_sched_resubmit_jobs);
  * This function returns -ENOENT in this case (which probably should be -EIO as
  * a more meanigful return value).
  *
+ * Note that job->sched is not a valid pointer until drm_sched_job_arm() has
+ * been called.
+ *
  * Returns 0 for success, negative error code otherwise.
  */
 int drm_sched_job_init(struct drm_sched_job *job,
@@ -691,7 +694,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("%s: entity has no rq!\n", __func__);
 		return -ENOENT;
 	}
 

base-commit: c015fb6d01adb616fb54824feb55ce5ab18e8ca1

Signature · 1 line
2.41.0
#1 lore raw

Hi,

Could you please use my gmail address, the one one I'm responding from--I don't want to miss any DRM scheduler patches. BTW, the [email protected] email should bounce as undeliverable.

Quoted 24 lines · Danilo Krummrich

Commit 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues") introduces drm_err() in drm_sched_job_init(), in order to indicate that the given entity has no runq, however at this time job->sched is not yet set, likely to be NULL initialized, and hence shouldn't be used.

Replace the corresponding drm_err() call with pr_err() to avoid a potential page fault.

While at it, extend the documentation of drm_sched_job_init() to indicate that job->sched is not a valid pointer until drm_sched_job_arm() has been called.

Fixes 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues")
---
 drivers/gpu/drm/scheduler/sched_main.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 27843e37d9b7..dd28389f0ddd 100644

drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_resubmit_jobs); · line 685

Quoted 6 lines
drivers/gpu/drm/scheduler/sched_main.c +3 -0
@@ -680,6 +680,9 @@ EXPORT_SYMBOL(drm_sched_resubmit_jobs);
  * This function returns -ENOENT in this case (which probably should be -EIO as
  * a more meanigful return value).
  *
+ * Note that job->sched is not a valid pointer until drm_sched_job_arm() has
+ * been called.
+ *

Good catch!

Did you actually get this to page-fault and have a kernel log?

I'm asking because we see it correctly set in this kernel log coming from AMD,

[   11.886024] amdgpu 0000:0a:00.0: [drm] *ERROR* drm_sched_job_init: entity has no rq!

in this email,
https://lore.kernel.org/r/CADnq5_PS64jYS_Y3kGW27m-kuWP+FQFiaVcOaZiB=JLSgPnXBQ@mail.gmail.com

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

Quoted 9 lines
drivers/gpu/drm/scheduler/sched_main.c +1 -1
  * Returns 0 for success, negative error code otherwise.
  */
 int drm_sched_job_init(struct drm_sched_job *job,
@@ -691,7 +694,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("%s: entity has no rq!\n", __func__);

Is it feasible to do something like the following?

		dev_err(job->sched ? job->sched->dev : NULL, "%s: entity has no rq!\n", __func__);
Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
 		return -ENOENT;
 	}
 

base-commit: c015fb6d01adb616fb54824feb55ce5ab18e8ca1

Signature · 2 lines
Regards,
Luben
#2 lore raw
Quoted 61 lines · Luben Tuikov

Hi,

Could you please use my gmail address, the one one I'm responding from--I don't want to miss any DRM scheduler patches. BTW, the [email protected] email should bounce as undeliverable.

Quoted 24 lines · Danilo Krummrich

Commit 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues") introduces drm_err() in drm_sched_job_init(), in order to indicate that the given entity has no runq, however at this time job->sched is not yet set, likely to be NULL initialized, and hence shouldn't be used.

Replace the corresponding drm_err() call with pr_err() to avoid a potential page fault.

While at it, extend the documentation of drm_sched_job_init() to indicate that job->sched is not a valid pointer until drm_sched_job_arm() has been called.

Fixes 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues")
---
 drivers/gpu/drm/scheduler/sched_main.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 27843e37d9b7..dd28389f0ddd 100644

drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_resubmit_jobs); · line 685

Quoted 6 lines
drivers/gpu/drm/scheduler/sched_main.c +3 -0
@@ -680,6 +680,9 @@ EXPORT_SYMBOL(drm_sched_resubmit_jobs);
  * This function returns -ENOENT in this case (which probably should be -EIO as
  * a more meanigful return value).
  *
+ * Note that job->sched is not a valid pointer until drm_sched_job_arm() has
+ * been called.
+ *

Good catch!

Did you actually get this to page-fault and have a kernel log?

I'm asking because we see it correctly set in this kernel log coming from AMD,

[   11.886024] amdgpu 0000:0a:00.0: [drm] *ERROR* drm_sched_job_init: entity has no rq!

in this email,
https://lore.kernel.org/r/CADnq5_PS64jYS_Y3kGW27m-kuWP+FQFiaVcOaZiB=JLSgPnXBQ@mail.gmail.com

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

Quoted 9 lines
drivers/gpu/drm/scheduler/sched_main.c +1 -1
  * Returns 0 for success, negative error code otherwise.
  */
 int drm_sched_job_init(struct drm_sched_job *job,
@@ -691,7 +694,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("%s: entity has no rq!\n", __func__);

Is it feasible to do something like the following?

Quoted 1 line
		dev_err(job->sched ? job->sched->dev : NULL, "%s: entity has no rq!\n", __func__);

Sorry, that was meant to be like this to make the print look just like the original,

		dev_err(job->sched ? job->sched->dev : NULL, "[drm] *ERROR* %s: entity has no rq!\n", __func__);
Quoted 7 lines
Quoted 6 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
 		return -ENOENT;
 	}
 

base-commit: c015fb6d01adb616fb54824feb55ce5ab18e8ca1

Signature · 2 lines
Regards,
Luben
#3 lore raw
In reply to Luben Tuikov
Quoted 40 lines · Luben Tuikov

Hi,

Could you please use my gmail address, the one one I'm responding from--I don't want to miss any DRM scheduler patches. BTW, the [email protected] email should bounce as undeliverable.

Quoted 24 lines · Danilo Krummrich

Commit 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues") introduces drm_err() in drm_sched_job_init(), in order to indicate that the given entity has no runq, however at this time job->sched is not yet set, likely to be NULL initialized, and hence shouldn't be used.

Replace the corresponding drm_err() call with pr_err() to avoid a potential page fault.

While at it, extend the documentation of drm_sched_job_init() to indicate that job->sched is not a valid pointer until drm_sched_job_arm() has been called.

Fixes 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues")
---
  drivers/gpu/drm/scheduler/sched_main.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 27843e37d9b7..dd28389f0ddd 100644

drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_resubmit_jobs); · line 685

Quoted 6 lines
drivers/gpu/drm/scheduler/sched_main.c +3 -0
@@ -680,6 +680,9 @@ EXPORT_SYMBOL(drm_sched_resubmit_jobs);
   * This function returns -ENOENT in this case (which probably should be -EIO as
   * a more meanigful return value).
   *
+ * Note that job->sched is not a valid pointer until drm_sched_job_arm() has
+ * been called.
+ *

Good catch!

Quoted 1 line

Did you actually get this to page-fault and have a kernel log?

No, I just found it because I was about to make the same mistake.

Quoted 2 lines

I'm asking because we see it correctly set in this kernel log coming from AMD,

I think that's because amdgpu just sets job->sched to *some* scheduler instance after job allocation [1].

[1] https://elixir.bootlin.com/linux/latest/source/drivers/gpu/drm/amd/amdgpu/amdgpu_job.c#L108

Quoted 18 lines
[   11.886024] amdgpu 0000:0a:00.0: [drm] *ERROR* drm_sched_job_init: entity has no rq!

in this email,
https://lore.kernel.org/r/CADnq5_PS64jYS_Y3kGW27m-kuWP+FQFiaVcOaZiB=JLSgPnXBQ@mail.gmail.com

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

Quoted 9 lines
drivers/gpu/drm/scheduler/sched_main.c +1 -1
   * Returns 0 for success, negative error code otherwise.
   */
  int drm_sched_job_init(struct drm_sched_job *job,
@@ -691,7 +694,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("%s: entity has no rq!\n", __func__);

Is it feasible to do something like the following?

Quoted 1 line
		dev_err(job->sched ? job->sched->dev : NULL, "%s: entity has no rq!\n", __func__);

I don't think that's a good idea. Although I'd assume that every driver zero-initializes its job structures, I can't see a rule enforcing that. Hence, job->sched can be a random value until drm_sched_job_arm() is called.

However, I notice there are quite a view more fields of struct drm_sched_job that are never initialized, hence there are either a couple more potential bugs or missing documentation that drivers *must* ensure that a job is zero-initialized.

Not quite sure if we really want to rely on the latter for core infrastructure...

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

base-commit: c015fb6d01adb616fb54824feb55ce5ab18e8ca1

#4 lore raw
Quoted 71 lines · Danilo Krummrich
Quoted 40 lines · Luben Tuikov

Hi,

Could you please use my gmail address, the one one I'm responding from--I don't want to miss any DRM scheduler patches. BTW, the [email protected] email should bounce as undeliverable.

Quoted 24 lines · Danilo Krummrich

Commit 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues") introduces drm_err() in drm_sched_job_init(), in order to indicate that the given entity has no runq, however at this time job->sched is not yet set, likely to be NULL initialized, and hence shouldn't be used.

Replace the corresponding drm_err() call with pr_err() to avoid a potential page fault.

While at it, extend the documentation of drm_sched_job_init() to indicate that job->sched is not a valid pointer until drm_sched_job_arm() has been called.

Fixes 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues")
---
  drivers/gpu/drm/scheduler/sched_main.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 27843e37d9b7..dd28389f0ddd 100644

drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_resubmit_jobs); · line 685

Quoted 6 lines
drivers/gpu/drm/scheduler/sched_main.c +3 -0
@@ -680,6 +680,9 @@ EXPORT_SYMBOL(drm_sched_resubmit_jobs);
   * This function returns -ENOENT in this case (which probably should be -EIO as
   * a more meanigful return value).
   *
+ * Note that job->sched is not a valid pointer until drm_sched_job_arm() has
+ * been called.
+ *

Good catch!

Quoted 1 line

Did you actually get this to page-fault and have a kernel log?

No, I just found it because I was about to make the same mistake.

Quoted 2 lines

I'm asking because we see it correctly set in this kernel log coming from AMD,

I think that's because amdgpu just sets job->sched to *some* scheduler instance after job allocation [1].

[1] https://elixir.bootlin.com/linux/latest/source/drivers/gpu/drm/amd/amdgpu/amdgpu_job.c#L108

Quoted 18 lines
[   11.886024] amdgpu 0000:0a:00.0: [drm] *ERROR* drm_sched_job_init: entity has no rq!

in this email,
https://lore.kernel.org/r/CADnq5_PS64jYS_Y3kGW27m-kuWP+FQFiaVcOaZiB=JLSgPnXBQ@mail.gmail.com

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

Quoted 9 lines
drivers/gpu/drm/scheduler/sched_main.c +1 -1
   * Returns 0 for success, negative error code otherwise.
   */
  int drm_sched_job_init(struct drm_sched_job *job,
@@ -691,7 +694,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("%s: entity has no rq!\n", __func__);

Is it feasible to do something like the following?

Quoted 5 lines
Quoted 1 line
		dev_err(job->sched ? job->sched->dev : NULL, "%s: entity has no rq!\n", __func__);

I don't think that's a good idea. Although I'd assume that every driver zero-initializes its job structures, I can't see a rule enforcing that. Hence, job->sched can be a random value until drm_sched_job_arm() is called.

Okay. However, when using pr_err() we're losing "[drm] *ERROR* " prefix and we scan for that in the logs to quickly find the cause of the error.

Perhaps we can define pr_fmt() and also include "*ERROR*" so that we can get the desired result as the attached patch shows?

Signature · 2 lines
Regards,
Luben

From 1f3ed97947a406a555a3efea05cab67da94172e7 Mon Sep 17 00:00:00 2001
From: Danilo Krummrich <[email protected]>
Date: Wed, 8 Nov 2023 03:26:07 +0100
Subject: [PATCH] drm/sched: fix potential page fault in drm_sched_job_init()

Commit 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues") introduces drm_err() in drm_sched_job_init(), in order to indicate that the given entity has no runq, however at this time job->sched is not yet set, likely to be NULL initialized, and hence shouldn't be used.

Replace the corresponding drm_err() call with pr_err() to avoid a potential page fault.

While at it, extend the documentation of drm_sched_job_init() to indicate that job->sched is not a valid pointer until drm_sched_job_arm() has been called.

v2: Add pr_fmt to drm_printk.h. Add "*ERROR*" to this pr_err() message. (Luben)

Fixes 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues")
---
 drivers/gpu/drm/scheduler/sched_main.c | 5 ++++-
 include/drm/drm_print.h                | 9 +++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +4 -1
index cd0dc3f81d05f0..bd13d4c8c385a8 100644
@@ -680,6 +680,9 @@ EXPORT_SYMBOL(drm_sched_resubmit_jobs);
  * This function returns -ENOENT in this case (which probably should be -EIO as
  * a more meanigful return value).
  *
+ * Note that job->sched is not a valid pointer until drm_sched_job_arm() has
+ * been called.
+ *
  * Returns 0 for success, negative error code otherwise.
  */
 int drm_sched_job_init(struct drm_sched_job *job,
@@ -691,7 +694,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;
 	}
 
include/drm/drm_print.h +9 -0
index a93a387f8a1a15..0132d563c8cfb9 100644
@@ -26,6 +26,15 @@
 #ifndef DRM_PRINT_H_
 #define DRM_PRINT_H_
 
+/* Define this before including linux/printk.h, so that the format
+ * string in pr_*() macros is correctly set for DRM. If a file wants
+ * to define this to something else, it should do so before including
+ * this header file.
+ */
+#ifndef pr_fmt
+#define pr_fmt(fmt) "[drm] " fmt
+#endif
+
 #include <linux/compiler.h>
 #include <linux/printk.h>
 #include <linux/seq_file.h>

base-commit: 8d88e4cdce4f5c56de55174a4d32ea9c06f7fa66

Signature · 1 line
2.42.1
#5 lore raw
Quoted 78 lines · Luben Tuikov
Quoted 71 lines · Danilo Krummrich
Quoted 40 lines · Luben Tuikov

Hi,

Could you please use my gmail address, the one one I'm responding from--I don't want to miss any DRM scheduler patches. BTW, the [email protected] email should bounce as undeliverable.

Quoted 24 lines · Danilo Krummrich

Commit 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues") introduces drm_err() in drm_sched_job_init(), in order to indicate that the given entity has no runq, however at this time job->sched is not yet set, likely to be NULL initialized, and hence shouldn't be used.

Replace the corresponding drm_err() call with pr_err() to avoid a potential page fault.

While at it, extend the documentation of drm_sched_job_init() to indicate that job->sched is not a valid pointer until drm_sched_job_arm() has been called.

Fixes 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues")
---
   drivers/gpu/drm/scheduler/sched_main.c | 5 ++++-
   1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 27843e37d9b7..dd28389f0ddd 100644

drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_resubmit_jobs); · line 685

Quoted 6 lines
drivers/gpu/drm/scheduler/sched_main.c +3 -0
@@ -680,6 +680,9 @@ EXPORT_SYMBOL(drm_sched_resubmit_jobs);
    * This function returns -ENOENT in this case (which probably should be -EIO as
    * a more meanigful return value).
    *
+ * Note that job->sched is not a valid pointer until drm_sched_job_arm() has
+ * been called.
+ *

Good catch!

Quoted 1 line

Did you actually get this to page-fault and have a kernel log?

No, I just found it because I was about to make the same mistake.

Quoted 2 lines

I'm asking because we see it correctly set in this kernel log coming from AMD,

I think that's because amdgpu just sets job->sched to *some* scheduler instance after job allocation [1].

[1] https://elixir.bootlin.com/linux/latest/source/drivers/gpu/drm/amd/amdgpu/amdgpu_job.c#L108

Quoted 18 lines
[   11.886024] amdgpu 0000:0a:00.0: [drm] *ERROR* drm_sched_job_init: entity has no rq!

in this email,
https://lore.kernel.org/r/CADnq5_PS64jYS_Y3kGW27m-kuWP+FQFiaVcOaZiB=JLSgPnXBQ@mail.gmail.com

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

Quoted 9 lines
drivers/gpu/drm/scheduler/sched_main.c +1 -1
    * Returns 0 for success, negative error code otherwise.
    */
   int drm_sched_job_init(struct drm_sched_job *job,
@@ -691,7 +694,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("%s: entity has no rq!\n", __func__);

Is it feasible to do something like the following?

Quoted 5 lines
Quoted 1 line
		dev_err(job->sched ? job->sched->dev : NULL, "%s: entity has no rq!\n", __func__);

I don't think that's a good idea. Although I'd assume that every driver zero-initializes its job structures, I can't see a rule enforcing that. Hence, job->sched can be a random value until drm_sched_job_arm() is called.

Quoted 5 lines

Okay. However, when using pr_err() we're losing "[drm] *ERROR* " prefix and we scan for that in the logs to quickly find the cause of the error.

Perhaps we can define pr_fmt() and also include "*ERROR*" so that we can get the desired result as the attached patch shows?

Sure, I'd add the pr_fmt() in a separate patch though.

#6 lore raw
In reply to Danilo Krummrich
Quoted 73 lines · Danilo Krummrich
Quoted 40 lines · Luben Tuikov

Hi,

Could you please use my gmail address, the one one I'm responding from--I don't want to miss any DRM scheduler patches. BTW, the [email protected] email should bounce as undeliverable.

Quoted 24 lines · Danilo Krummrich

Commit 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues") introduces drm_err() in drm_sched_job_init(), in order to indicate that the given entity has no runq, however at this time job->sched is not yet set, likely to be NULL initialized, and hence shouldn't be used.

Replace the corresponding drm_err() call with pr_err() to avoid a potential page fault.

While at it, extend the documentation of drm_sched_job_init() to indicate that job->sched is not a valid pointer until drm_sched_job_arm() has been called.

Fixes 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues")

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

  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 27843e37d9b7..dd28389f0ddd 100644

drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_resubmit_jobs); · line 685

Quoted 6 lines
drivers/gpu/drm/scheduler/sched_main.c +3 -0
@@ -680,6 +680,9 @@ EXPORT_SYMBOL(drm_sched_resubmit_jobs);
    * This function returns -ENOENT in this case (which probably should be -EIO as
    * a more meanigful return value).
    *
+ * Note that job->sched is not a valid pointer until drm_sched_job_arm() has
+ * been called.
+ *

Good catch!

Quoted 1 line

Did you actually get this to page-fault and have a kernel log?

No, I just found it because I was about to make the same mistake.

Quoted 2 lines

I'm asking because we see it correctly set in this kernel log coming from AMD,

I think that's because amdgpu just sets job->sched to *some* scheduler instance after job allocation [1].

[1] https://elixir.bootlin.com/linux/latest/source/drivers/gpu/drm/amd/amdgpu/amdgpu_job.c#L108

Quoted 18 lines

[   11.886024] amdgpu 0000:0a:00.0: [drm] *ERROR* drm_sched_job_init: entity has no rq!

in this email,
https://lore.kernel.org/r/CADnq5_PS64jYS_Y3kGW27m-kuWP+FQFiaVcOaZiB=JLSgPnXBQ@mail.gmail.com

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

Quoted 9 lines
drivers/gpu/drm/scheduler/sched_main.c +1 -1
    * Returns 0 for success, negative error code otherwise.
    */
   int drm_sched_job_init(struct drm_sched_job *job,
@@ -691,7 +694,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("%s: entity has no rq!\n", __func__);

Is it feasible to do something like the following?

Quoted 1 line

        dev_err(job->sched ? job->sched->dev : NULL, "%s: entity has no rq!\n", __func__);

Quoted 7 lines

I don't think that's a good idea. Although I'd assume that every driver zero-initializes its job structures, I can't see a rule enforcing that. Hence, job->sched can be a random value until drm_sched_job_arm() is called.

However, I notice there are quite a view more fields of struct drm_sched_job that are never initialized, hence there are either a couple more potential bugs or missing documentation that drivers *must* ensure that a job is zero-initialized.

Any opinions on that? Otherwise I'd probably go ahead and send a fix for the other bugs too.

Quoted 9 lines

Not quite sure if we really want to rely on the latter for core infrastructure...

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

base-commit: c015fb6d01adb616fb54824feb55ce5ab18e8ca1

#7 lore raw
Quoted 78 lines · Danilo Krummrich
Quoted 77 lines · Danilo Krummrich
Quoted 40 lines · Luben Tuikov

Hi,

Could you please use my gmail address, the one one I'm responding from--I don't want to miss any DRM scheduler patches. BTW, the [email protected] email should bounce as undeliverable.

Quoted 24 lines · Danilo Krummrich

Commit 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues") introduces drm_err() in drm_sched_job_init(), in order to indicate that the given entity has no runq, however at this time job->sched is not yet set, likely to be NULL initialized, and hence shouldn't be used.

Replace the corresponding drm_err() call with pr_err() to avoid a potential page fault.

While at it, extend the documentation of drm_sched_job_init() to indicate that job->sched is not a valid pointer until drm_sched_job_arm() has been called.

Fixes 56e449603f0a ("drm/sched: Convert the GPU scheduler to variable number of run-queues")

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

  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 27843e37d9b7..dd28389f0ddd 100644

drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_resubmit_jobs); · line 685

Quoted 6 lines
drivers/gpu/drm/scheduler/sched_main.c +3 -0
@@ -680,6 +680,9 @@ EXPORT_SYMBOL(drm_sched_resubmit_jobs);
    * This function returns -ENOENT in this case (which probably should be -EIO as
    * a more meanigful return value).
    *
+ * Note that job->sched is not a valid pointer until drm_sched_job_arm() has
+ * been called.
+ *

Good catch!

Quoted 1 line

Did you actually get this to page-fault and have a kernel log?

No, I just found it because I was about to make the same mistake.

Quoted 2 lines

I'm asking because we see it correctly set in this kernel log coming from AMD,

I think that's because amdgpu just sets job->sched to *some* scheduler instance after job allocation [1].

[1] https://elixir.bootlin.com/linux/latest/source/drivers/gpu/drm/amd/amdgpu/amdgpu_job.c#L108

Quoted 18 lines

[   11.886024] amdgpu 0000:0a:00.0: [drm] *ERROR* drm_sched_job_init: entity has no rq!

in this email,
https://lore.kernel.org/r/CADnq5_PS64jYS_Y3kGW27m-kuWP+FQFiaVcOaZiB=JLSgPnXBQ@mail.gmail.com

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

Quoted 9 lines
drivers/gpu/drm/scheduler/sched_main.c +1 -1
    * Returns 0 for success, negative error code otherwise.
    */
   int drm_sched_job_init(struct drm_sched_job *job,
@@ -691,7 +694,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("%s: entity has no rq!\n", __func__);

Is it feasible to do something like the following?

Quoted 1 line

        dev_err(job->sched ? job->sched->dev : NULL, "%s: entity has no rq!\n", __func__);

I don't think that's a good idea. Although I'd assume that every driver zero-initializes its job structures, I can't see a rule enforcing that. Hence, job->sched can be a random value until drm_sched_job_arm() is called.

Quoted 5 lines
Quoted 3 lines

However, I notice there are quite a view more fields of struct drm_sched_job that are never initialized, hence there are either a couple more potential bugs or missing documentation that drivers *must* ensure that a job is zero-initialized.

Any opinions on that? Otherwise I'd probably go ahead and send a fix for the other bugs too.

Send the patches.

Will those patches also add pr_fmt() for DRM?

I'm asking because you said you'll add pr_fmt() in a "separate" patch, and I thought it was okay being self-contained in your patch as per the version I sent.

Signature · 2 lines
Regards,
Luben