Skip to contentSkip to conversation

sched/eevdf: Avoid NULL in pick_eevdf

PATCH In review Xuewen Yan 3 participants 5 messages Nov 20, 2023 – Dec 12, 2023 Scheduler

lore
Share & tools
lore

Xuewen Yan

author
#0 lore raw

PATCH 1/1 sched/eevdf: Avoid NULL in pick_eevdf

Now in pick_eevdf function, add the pick_first_entity to prevent picking null when using eevdf, however, the leftmost may be null. As a result, it would cause oops because the se is NULL.

Fix this by compare the curr and left, if the left is null, set the se be curr.

Fixes 147f3efaa241 ("sched/fair: Implement an EEVDF-like scheduling policy")
---
 kernel/sched/fair.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
kernel/sched/fair.c +17 -1
index d7a3c63a2171..10916f6778ac 100644
@@ -951,12 +951,28 @@ static struct sched_entity *__pick_eevdf(struct cfs_rq *cfs_rq)
 	return NULL;
 }
 
+/* Just simply choose the se with the smallest vruntime */
+static struct sched_entity *__pick_cfs(struct cfs_rq *cfs_rq)
+{
+	struct sched_entity *curr = cfs_rq->curr;
+	struct sched_entity *left = __pick_first_entity(cfs_rq);
+
+	/*
+	 * If curr is set we have to see if its left of the leftmost entity
+	 * still in the tree, provided there was anything in the tree at all.
+	 */
+	if (!left || (curr && entity_before(curr, left)))
+		left = curr;
+
+	return left;
+}
+
 static struct sched_entity *pick_eevdf(struct cfs_rq *cfs_rq)
 {
 	struct sched_entity *se = __pick_eevdf(cfs_rq);
 
 	if (!se) {
-		struct sched_entity *left = __pick_first_entity(cfs_rq);
+		struct sched_entity *left = __pick_cfs(cfs_rq);
 		if (left) {
 			pr_err("EEVDF scheduling fail, picking leftmost\n");
 			return left;
Signature · 1 line
2.25.1
#1 lore raw

Hi Xuewen, the pick part has been re-worked, would you please re-test with the newest branch?

Thanks,
Abel

https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/?h=sched%2Fcore

On 11/20/23 3:38 PM, Xuewen Yan Wrote:

Quoted 47 lines

Now in pick_eevdf function, add the pick_first_entity to prevent picking null when using eevdf, however, the leftmost may be null. As a result, it would cause oops because the se is NULL.

Fix this by compare the curr and left, if the left is null, set the se be curr.

Fixes 147f3efaa241 ("sched/fair: Implement an EEVDF-like scheduling policy")
---
  kernel/sched/fair.c | 18 +++++++++++++++++-
  1 file changed, 17 insertions(+), 1 deletion(-)
kernel/sched/fair.c +17 -1
index d7a3c63a2171..10916f6778ac 100644
@@ -951,12 +951,28 @@ static struct sched_entity *__pick_eevdf(struct cfs_rq *cfs_rq)
  	return NULL;
  }
 
+/* Just simply choose the se with the smallest vruntime */
+static struct sched_entity *__pick_cfs(struct cfs_rq *cfs_rq)
+{
+	struct sched_entity *curr = cfs_rq->curr;
+	struct sched_entity *left = __pick_first_entity(cfs_rq);
+
+	/*
+	 * If curr is set we have to see if its left of the leftmost entity
+	 * still in the tree, provided there was anything in the tree at all.
+	 */
+	if (!left || (curr && entity_before(curr, left)))
+		left = curr;
+
+	return left;
+}
+
  static struct sched_entity *pick_eevdf(struct cfs_rq *cfs_rq)
  {
  	struct sched_entity *se = __pick_eevdf(cfs_rq);
 
  	if (!se) {
-		struct sched_entity *left = __pick_first_entity(cfs_rq);
+		struct sched_entity *left = __pick_cfs(cfs_rq);
  		if (left) {
  			pr_err("EEVDF scheduling fail, picking leftmost\n");
  			return left;
#2 lore raw
Quoted 3 lines · Abel Wu

Hi Xuewen, the pick part has been re-worked, would you please re-test with the newest branch?

Okay, Thanks!

Quoted 54 lines

Thanks,
Abel

https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/?h=sched%2Fcore

On 11/20/23 3:38 PM, Xuewen Yan Wrote:

Quoted 47 lines

Now in pick_eevdf function, add the pick_first_entity to prevent picking null when using eevdf, however, the leftmost may be null. As a result, it would cause oops because the se is NULL.

Fix this by compare the curr and left, if the left is null, set the se be curr.

Fixes 147f3efaa241 ("sched/fair: Implement an EEVDF-like scheduling policy")
---
  kernel/sched/fair.c | 18 +++++++++++++++++-
  1 file changed, 17 insertions(+), 1 deletion(-)
kernel/sched/fair.c +17 -1
index d7a3c63a2171..10916f6778ac 100644
@@ -951,12 +951,28 @@ static struct sched_entity *__pick_eevdf(struct cfs_rq *cfs_rq)
      return NULL;
  }
 
+/* Just simply choose the se with the smallest vruntime */
+static struct sched_entity *__pick_cfs(struct cfs_rq *cfs_rq)
+{
+     struct sched_entity *curr = cfs_rq->curr;
+     struct sched_entity *left = __pick_first_entity(cfs_rq);
+
+     /*
+      * If curr is set we have to see if its left of the leftmost entity
+      * still in the tree, provided there was anything in the tree at all.
+      */
+     if (!left || (curr && entity_before(curr, left)))
+             left = curr;
+
+     return left;
+}
+
  static struct sched_entity *pick_eevdf(struct cfs_rq *cfs_rq)
  {
      struct sched_entity *se = __pick_eevdf(cfs_rq);
 
      if (!se) {
-             struct sched_entity *left = __pick_first_entity(cfs_rq);
+             struct sched_entity *left = __pick_cfs(cfs_rq);
              if (left) {
                      pr_err("EEVDF scheduling fail, picking leftmost\n");
                      return left;
#3 lore raw
In reply to Abel Wu

Hi Abel

Quoted 8 lines · Abel Wu

Hi Xuewen, the pick part has been re-worked, would you please re-test with the newest branch?

Thanks,
Abel

https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/?h=sched%2Fcore

These patches would be merged into 6.6? If not, the pr_err also has a deadlock. And should be changed to printk_deferred.

954 static struct sched_entity *pick_eevdf(struct cfs_rq *cfs_rq)
955 {
956 struct sched_entity *se = __pick_eevdf(cfs_rq);
957
958 if (!se) {
959 struct sched_entity *left = __pick_first_entity(cfs_rq);
960 if (left) {
961 pr_err("EEVDF scheduling fail, picking
leftmost\n"); <<<<
962 return left;
963 }
964 }
965
966 return se;
967 }

Quoted 49 lines

On 11/20/23 3:38 PM, Xuewen Yan Wrote:

Quoted 47 lines

Now in pick_eevdf function, add the pick_first_entity to prevent picking null when using eevdf, however, the leftmost may be null. As a result, it would cause oops because the se is NULL.

Fix this by compare the curr and left, if the left is null, set the se be curr.

Fixes 147f3efaa241 ("sched/fair: Implement an EEVDF-like scheduling policy")
---
  kernel/sched/fair.c | 18 +++++++++++++++++-
  1 file changed, 17 insertions(+), 1 deletion(-)
kernel/sched/fair.c +17 -1
index d7a3c63a2171..10916f6778ac 100644
@@ -951,12 +951,28 @@ static struct sched_entity *__pick_eevdf(struct cfs_rq *cfs_rq)
      return NULL;
  }
 
+/* Just simply choose the se with the smallest vruntime */
+static struct sched_entity *__pick_cfs(struct cfs_rq *cfs_rq)
+{
+     struct sched_entity *curr = cfs_rq->curr;
+     struct sched_entity *left = __pick_first_entity(cfs_rq);
+
+     /*
+      * If curr is set we have to see if its left of the leftmost entity
+      * still in the tree, provided there was anything in the tree at all.
+      */
+     if (!left || (curr && entity_before(curr, left)))
+             left = curr;
+
+     return left;
+}
+
  static struct sched_entity *pick_eevdf(struct cfs_rq *cfs_rq)
  {
      struct sched_entity *se = __pick_eevdf(cfs_rq);
 
      if (!se) {
-             struct sched_entity *left = __pick_first_entity(cfs_rq);
+             struct sched_entity *left = __pick_cfs(cfs_rq);
              if (left) {
                      pr_err("EEVDF scheduling fail, picking leftmost\n");
                      return left;
#4 lore raw

On 12/12/23 10:51 AM, Xuewen Yan Wrote:

Quoted 7 lines

Hi Abel

Quoted 4 lines · Abel Wu

Hi Xuewen, the pick part has been re-worked, would you please re-test with the newest branch?

Quoted 7 lines
Quoted 4 lines

Thanks,
Abel

https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/?h=sched%2Fcore

These patches would be merged into 6.6? If not, the pr_err also has a deadlock. And should be changed to printk_deferred.

Yes, they will be merged in Linus's tree.

Quoted 68 lines

954 static struct sched_entity *pick_eevdf(struct cfs_rq *cfs_rq)

   955 {
   956         struct sched_entity *se = __pick_eevdf(cfs_rq);
   957
   958         if (!se) {
   959                 struct sched_entity *left = __pick_first_entity(cfs_rq);
   960                 if (left) {
   961                         pr_err("EEVDF scheduling fail, picking
leftmost\n"); <<<<
   962                         return left;
   963                 }
   964         }
   965
   966         return se;
   967 }
Quoted 49 lines

On 11/20/23 3:38 PM, Xuewen Yan Wrote:

Quoted 47 lines

Now in pick_eevdf function, add the pick_first_entity to prevent picking null when using eevdf, however, the leftmost may be null. As a result, it would cause oops because the se is NULL.

Fix this by compare the curr and left, if the left is null, set the se be curr.

Fixes 147f3efaa241 ("sched/fair: Implement an EEVDF-like scheduling policy")
---
   kernel/sched/fair.c | 18 +++++++++++++++++-
   1 file changed, 17 insertions(+), 1 deletion(-)
kernel/sched/fair.c +17 -1
index d7a3c63a2171..10916f6778ac 100644
@@ -951,12 +951,28 @@ static struct sched_entity *__pick_eevdf(struct cfs_rq *cfs_rq)
       return NULL;
   }
 
+/* Just simply choose the se with the smallest vruntime */
+static struct sched_entity *__pick_cfs(struct cfs_rq *cfs_rq)
+{
+     struct sched_entity *curr = cfs_rq->curr;
+     struct sched_entity *left = __pick_first_entity(cfs_rq);
+
+     /*
+      * If curr is set we have to see if its left of the leftmost entity
+      * still in the tree, provided there was anything in the tree at all.
+      */
+     if (!left || (curr && entity_before(curr, left)))
+             left = curr;
+
+     return left;
+}
+
   static struct sched_entity *pick_eevdf(struct cfs_rq *cfs_rq)
   {
       struct sched_entity *se = __pick_eevdf(cfs_rq);
 
       if (!se) {
-             struct sched_entity *left = __pick_first_entity(cfs_rq);
+             struct sched_entity *left = __pick_cfs(cfs_rq);
               if (left) {
                       pr_err("EEVDF scheduling fail, picking leftmost\n");
                       return left;