Skip to contentSkip to conversation

sched: Split the cookie and setup per-task cookie on fork

PATCH chris hyser 2 participants 3 messages Nov 9, 2020 – Nov 17, 2020 1 message not in the archive Scheduler

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 61 lines · Joel Fernandes (Google)
Quoted 60 lines

In order to prevent interference and clearly support both per-task and CGroup APIs, split the cookie into 2 and allow it to be set from either per-task, or CGroup API. The final cookie is the combined value of both and is computed when the stop-machine executes during a change of cookie.

Also, for the per-task cookie, it will get weird if we use pointers of any emphemeral objects. For this reason, introduce a refcounted object who's sole purpose is to assign unique cookie value by way of the object's pointer.

While at it, refactor the CGroup code a bit. Future patches will introduce more APIs and support.

---
  include/linux/sched.h |   2 +
  kernel/sched/core.c   | 241 ++++++++++++++++++++++++++++++++++++++++--
  kernel/sched/debug.c  |   4 +
  3 files changed, 236 insertions(+), 11 deletions(-)
include/linux/sched.h +2 -0
index fe6f225bfbf9..c6034c00846a 100644
@@ -688,6 +688,8 @@ struct task_struct {
  #ifdef CONFIG_SCHED_CORE
      struct rb_node            core_node;
      unsigned long            core_cookie;
+    unsigned long            core_task_cookie;
+    unsigned long            core_group_cookie;
      unsigned int            core_occupation;
  #endif
  diff --git a/kernel/sched/core.c b/kernel/sched/core.c

index bab4ea2f5cd8..30a9e4cb5ce1 100644

kernel/sched/core.c +11 -0
@@ -346,11 +346,14 @@ void sched_core_put(void)
      mutex_unlock(&sched_core_mutex);
  }
  +static int sched_core_share_tasks(struct task_struct *t1, struct task_struct *t2);
+
  #else /* !CONFIG_SCHED_CORE */
    static inline void sched_core_enqueue(struct rq *rq, struct task_struct *p) { }
  static inline void sched_core_dequeue(struct rq *rq, struct task_struct *p) { }
  static bool sched_core_enqueued(struct task_struct *task) { return false; }
+static int sched_core_share_tasks(struct task_struct *t1, struct task_struct *t2) { }
    #endif /* CONFIG_SCHED_CORE */
  @@ -3583,6 +3586,20 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p)
  #endif
  #ifdef CONFIG_SCHED_CORE
      RB_CLEAR_NODE(&p->core_node);
+
+    /*
+     * Tag child via per-task cookie only if parent is tagged via per-task
+     * cookie. This is independent of, but can be additive to the CGroup tagging.
+     */
+    if (current->core_task_cookie) {
+
+        /* If it is not CLONE_THREAD fork, assign a unique per-task tag. */
+        if (!(clone_flags & CLONE_THREAD)) {
Quoted 8 lines

kernel/sched/core.c

Quoted 8 lines
kernel/sched/core.c +5 -0
+            return sched_core_share_tasks(p, p);
+               }
+        /* Otherwise share the parent's per-task tag. */
+        return sched_core_share_tasks(p, current);
+    }
  #endif
      return 0;
  } 

sched_core_share_tasks() looks at the value of the new tasks core_task_cookie which is non-zero on a process or thread clone and causes underflow for both the enable flag itself and for cookie ref counts.

So just zero it in __sched_fork().

-chris

PATCH] sched: zero out the core scheduling cookie on clone

From: chris hyser <[email protected]>

As the cookie is reference counted, even if inherited, zero this and allow explicit sharing.

---
 kernel/sched/core.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/sched/core.c +3 -0
index fd3cc03..2af0ea6 100644
@@ -3378,6 +3378,9 @@ static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
 	p->capture_control = NULL;
 #endif
 	init_numa_balancing(clone_flags, p);
+#ifdef CONFIG_SCHED_CORE
+	p->core_task_cookie = 0;
+#endif
 #ifdef CONFIG_SMP
 	p->wake_entry.u_flags = CSD_TYPE_TTWU;
 #endif
Signature · 1 line
1.8.3.1
#2 lore raw

On Mon, Nov 09, 2020 at 06:23:13PM -0500, chris hyser wrote:
[..]

Quoted 53 lines
Quoted 41 lines
Quoted 41 lines
include/linux/sched.h +2 -0
index fe6f225bfbf9..c6034c00846a 100644
@@ -688,6 +688,8 @@ struct task_struct {
  #ifdef CONFIG_SCHED_CORE
      struct rb_node            core_node;
      unsigned long            core_cookie;
+    unsigned long            core_task_cookie;
+    unsigned long            core_group_cookie;
      unsigned int            core_occupation;
  #endif
  diff --git a/kernel/sched/core.c b/kernel/sched/core.c

index bab4ea2f5cd8..30a9e4cb5ce1 100644

kernel/sched/core.c +11 -0
@@ -346,11 +346,14 @@ void sched_core_put(void)
      mutex_unlock(&sched_core_mutex);
  }
  +static int sched_core_share_tasks(struct task_struct *t1, struct task_struct *t2);
+
  #else /* !CONFIG_SCHED_CORE */
    static inline void sched_core_enqueue(struct rq *rq, struct task_struct *p) { }
  static inline void sched_core_dequeue(struct rq *rq, struct task_struct *p) { }
  static bool sched_core_enqueued(struct task_struct *task) { return false; }
+static int sched_core_share_tasks(struct task_struct *t1, struct task_struct *t2) { }
    #endif /* CONFIG_SCHED_CORE */
  @@ -3583,6 +3586,20 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p)
  #endif
  #ifdef CONFIG_SCHED_CORE
      RB_CLEAR_NODE(&p->core_node);
+
+    /*
+     * Tag child via per-task cookie only if parent is tagged via per-task
+     * cookie. This is independent of, but can be additive to the CGroup tagging.
+     */
+    if (current->core_task_cookie) {
+
+        /* If it is not CLONE_THREAD fork, assign a unique per-task tag. */
+        if (!(clone_flags & CLONE_THREAD)) {
Quoted 8 lines

kernel/sched/core.c

Quoted 8 lines
kernel/sched/core.c +5 -0
+            return sched_core_share_tasks(p, p);
+               }
+        /* Otherwise share the parent's per-task tag. */
+        return sched_core_share_tasks(p, current);
+    }
  #endif
      return 0;
  }

sched_core_share_tasks() looks at the value of the new tasks core_task_cookie which is non-zero on a process or thread clone and causes underflow for both the enable flag itself and for cookie ref counts.

Quoted 1 line

So just zero it in __sched_fork().

Nice catch, applying to my v9 staging tree. Thanks!

thanks,

- Joel

Quoted 33 lines

-chris

PATCH] sched: zero out the core scheduling cookie on clone

From: chris hyser <[email protected]>

As the cookie is reference counted, even if inherited, zero this and allow explicit sharing.

---
 kernel/sched/core.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/sched/core.c +3 -0
index fd3cc03..2af0ea6 100644
@@ -3378,6 +3378,9 @@ static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
 	p->capture_control = NULL;
 #endif
 	init_numa_balancing(clone_flags, p);
+#ifdef CONFIG_SCHED_CORE
+	p->core_task_cookie = 0;
+#endif
 #ifdef CONFIG_SMP
 	p->wake_entry.u_flags = CSD_TYPE_TTWU;
 #endif
Signature · 1 line
1.8.3.1
#3 lore raw
In reply to chris hyser

Hi Chris,

Quoted 73 lines · chris hyser
Quoted 61 lines · Joel Fernandes (Google)
Quoted 60 lines

In order to prevent interference and clearly support both per-task and CGroup APIs, split the cookie into 2 and allow it to be set from either per-task, or CGroup API. The final cookie is the combined value of both and is computed when the stop-machine executes during a change of cookie.

Also, for the per-task cookie, it will get weird if we use pointers of any emphemeral objects. For this reason, introduce a refcounted object who's sole purpose is to assign unique cookie value by way of the object's pointer.

While at it, refactor the CGroup code a bit. Future patches will introduce more APIs and support.

---
  include/linux/sched.h |   2 +
  kernel/sched/core.c   | 241 ++++++++++++++++++++++++++++++++++++++++--
  kernel/sched/debug.c  |   4 +
  3 files changed, 236 insertions(+), 11 deletions(-)
include/linux/sched.h +2 -0
index fe6f225bfbf9..c6034c00846a 100644
@@ -688,6 +688,8 @@ struct task_struct {
  #ifdef CONFIG_SCHED_CORE
      struct rb_node            core_node;
      unsigned long            core_cookie;
+    unsigned long            core_task_cookie;
+    unsigned long            core_group_cookie;
      unsigned int            core_occupation;
  #endif
  diff --git a/kernel/sched/core.c b/kernel/sched/core.c

index bab4ea2f5cd8..30a9e4cb5ce1 100644

kernel/sched/core.c +11 -0
@@ -346,11 +346,14 @@ void sched_core_put(void)
      mutex_unlock(&sched_core_mutex);
  }
  +static int sched_core_share_tasks(struct task_struct *t1, struct task_struct *t2);
+
  #else /* !CONFIG_SCHED_CORE */
    static inline void sched_core_enqueue(struct rq *rq, struct task_struct *p) { }
  static inline void sched_core_dequeue(struct rq *rq, struct task_struct *p) { }
  static bool sched_core_enqueued(struct task_struct *task) { return false; }
+static int sched_core_share_tasks(struct task_struct *t1, struct task_struct *t2) { }
    #endif /* CONFIG_SCHED_CORE */
  @@ -3583,6 +3586,20 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p)
  #endif
  #ifdef CONFIG_SCHED_CORE
      RB_CLEAR_NODE(&p->core_node);
+
+    /*
+     * Tag child via per-task cookie only if parent is tagged via per-task
+     * cookie. This is independent of, but can be additive to the CGroup tagging.
+     */
+    if (current->core_task_cookie) {
+
+        /* If it is not CLONE_THREAD fork, assign a unique per-task tag. */
+        if (!(clone_flags & CLONE_THREAD)) {
Quoted 8 lines

kernel/sched/core.c

Quoted 8 lines
kernel/sched/core.c +5 -0
+            return sched_core_share_tasks(p, p);
+               }
+        /* Otherwise share the parent's per-task tag. */
+        return sched_core_share_tasks(p, current);
+    }
  #endif
      return 0;
  }

sched_core_share_tasks() looks at the value of the new tasks core_task_cookie which is non-zero on a process or thread clone and causes underflow for both the enable flag itself and for cookie ref counts.

Quoted 1 line

So just zero it in __sched_fork().

Since I am going to split this into a new patch, could you please send me a detailed commit message explaining the issue? I feel the below one liner is unclear / insufficient as a commit message.

thanks,

- Joel

Quoted 32 lines

-chris

PATCH] sched: zero out the core scheduling cookie on clone

From: chris hyser <[email protected]>

As the cookie is reference counted, even if inherited, zero this and allow explicit sharing.

---
 kernel/sched/core.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/sched/core.c +3 -0
index fd3cc03..2af0ea6 100644
@@ -3378,6 +3378,9 @@ static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
 	p->capture_control = NULL;
 #endif
 	init_numa_balancing(clone_flags, p);
+#ifdef CONFIG_SCHED_CORE
+	p->core_task_cookie = 0;
+#endif
 #ifdef CONFIG_SMP
 	p->wake_entry.u_flags = CSD_TYPE_TTWU;
 #endif
Signature · 1 line
1.8.3.1