Skip to contentSkip to conversation

Core scheduling remaining patches

PATCH In review Joel Fernandes (Google) 4 participants 34 messages Jan 23, 2021 – Mar 2, 2021

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

v10 cover Core scheduling remaining patches

Core-Scheduling
===============
Enclosed is series v10 of core scheduling.
Many of the core patches in v9 were picked up in Peter's queue tree. This series contains the remaining patches (interface, docs).

v9 series is here: https://lwn.net/Articles/837595/

Introduction of feature
=======================
Core scheduling is a feature that allows only trusted tasks to run concurrently on cpus sharing compute resources (eg: hyperthreads on a core). The goal is to mitigate the core-level side-channel attacks without requiring to disable SMT (which has a significant impact on performance in some situations). Core scheduling (as of v7) mitigates user-space to user-space attacks and user to kernel attack when one of the siblings enters the kernel via interrupts or system call.

By default, the feature doesn't change any of the current scheduler behavior. The user decides which tasks can run simultaneously on the same core (for now by having them in the same tagged cgroup). When a tag is enabled in a cgroup and a task from that cgroup is running on a hardware thread, the scheduler ensures that only idle or trusted tasks run on the other sibling(s). Besides security concerns, this feature can also be beneficial for RT and performance applications where we want to control how tasks make use of SMT dynamically.

Both a CGroup and Per-task interface via prctl(2) are provided for configuring core sharing. More details are provided in documentation patch. Kselftests are provided to verify the correctness/rules of the interface.

Testing
=======
ChromeOS testing shows 300% improvement in keypress latency on a Google docs key press with Google hangout test (the maximum latency drops from 150ms to 50ms for keypresses).

Vineeth tested sysbench on v9 series and does not see any regressions. Hong and Aubrey tested v9 and see results similar to v8. There is a known issue with uperf that does regress. This appears to be because of ksoftirq heavily contending with other tasks on the core. The consensus is this can be improved in the future.

Changes in v10
==============
- migration code changes from Aubrey.
- interface changes from Josh and Chris.
- dropped kernel protection changes for now (have to rework them for mainline).

Changes in v9
=============
- Note that the vruntime snapshot change is written in 2 patches to show the progression of the idea and prevent merge conflicts:
sched/fair: Snapshot the min_vruntime of CPUs on force idle
sched: Improve snapshotting of min_vruntime for CGroups
Same with the RT priority inversion change:
sched: Fix priority inversion of cookied task with sibling
sched: Improve snapshotting of min_vruntime for CGroups
- Disable coresched on certain AMD HW.

Changes in v8
=============
- New interface/API implementation
- Joel
- Revised kernel protection patch
- Joel
- Revised Hotplug fixes
- Joel
- Minor bug fixes and address review comments
- Vineeth

Changes in v7
=============
- Kernel protection from untrusted usermode tasks
- Joel, Vineeth
- Fix for hotplug crashes and hangs
- Joel, Vineeth

Changes in v6
=============
- Documentation
- Joel
- Pause siblings on entering nmi/irq/softirq
- Joel, Vineeth
- Fix for RCU crash
- Joel
- Fix for a crash in pick_next_task
- Yu Chen, Vineeth
- Minor re-write of core-wide vruntime comparison
- Aaron Lu
- Cleanup: Address Review comments
- Cleanup: Remove hotplug support (for now)
- Build fixes: 32 bit, SMT=n, AUTOGROUP=n etc
- Joel, Vineeth

Changes in v5
=============
- Fixes for cgroup/process tagging during corner cases like cgroup destroy, task moving across cgroups etc
- Tim Chen
- Coresched aware task migrations
- Aubrey Li
- Other minor stability fixes.

Changes in v4
=============
- Implement a core wide min_vruntime for vruntime comparison of tasks across cpus in a core.
- Aaron Lu
- Fixes a typo bug in setting the forced_idle cpu.
- Aaron Lu

Changes in v3
=============
- Fixes the issue of sibling picking up an incompatible task
- Aaron Lu
- Vineeth Pillai
- Julien Desfossez
- Fixes the issue of starving threads due to forced idle
- Peter Zijlstra
- Fixes the refcounting issue when deleting a cgroup with tag
- Julien Desfossez
- Fixes a crash during cpu offline/online with coresched enabled
- Vineeth Pillai
- Fixes a comparison logic issue in sched_core_find
- Aaron Lu

Changes in v2
=============
- Fixes for couple of NULL pointer dereference crashes
- Subhra Mazumdar
- Tim Chen
- Improves priority comparison logic for process in different cpus
- Peter Zijlstra
- Aaron Lu
- Fixes a hard lockup in rq locking
- Vineeth Pillai
- Julien Desfossez
- Fixes a performance issue seen on IO heavy workloads
- Vineeth Pillai
- Julien Desfossez
- Fix for 32bit build
- Aubrey Li

Future work
===========
- Load balancing/Migration fixes for core scheduling. With v6, Load balancing is partially coresched aware, but has some issues w.r.t process/taskgroup weights:
https://lwn.net/ml/linux-kernel/20200225034438.GA617271@z...

Aubrey Li (1):
sched: migration changes for core scheduling

Joel Fernandes (Google) (3):
kselftest: Add tests for core-sched interface
Documentation: Add core scheduling documentation
sched: Debug bits...

Peter Zijlstra (1):
sched: CGroup tagging interface for core scheduling

.../admin-guide/hw-vuln/core-scheduling.rst   | 263 +++++++
Documentation/admin-guide/hw-vuln/index.rst   |   1 +
include/linux/sched.h                         |  10 +
include/uapi/linux/prctl.h                    |   6 +
kernel/fork.c                                 |   1 +
kernel/sched/Makefile                         |   1 +
kernel/sched/core.c                           | 171 ++++-
kernel/sched/coretag.c                        | 669 ++++++++++++++++
kernel/sched/debug.c                          |   4 +
kernel/sched/fair.c                           |  42 +-
kernel/sched/sched.h                          | 130 +++-
kernel/sys.c                                  |   7 +
tools/include/uapi/linux/prctl.h              |   6 +
tools/testing/selftests/sched/.gitignore      |   1 +
tools/testing/selftests/sched/Makefile        |  14 +
tools/testing/selftests/sched/config          |   1 +
.../testing/selftests/sched/test_coresched.c  | 716 ++++++++++++++++++
17 files changed, 2018 insertions(+), 25 deletions(-)

create mode 100644 Documentation/admin-guide/hw-vuln/core-scheduling.rst create mode 100644 kernel/sched/coretag.c
create mode 100644 tools/testing/selftests/sched/.gitignore create mode 100644 tools/testing/selftests/sched/Makefile create mode 100644 tools/testing/selftests/sched/config create mode 100644 tools/testing/selftests/sched/test_coresched.c

--
2.30.0.280.ga3ce27912f-goog

#1 lore raw

PATCH v10 1/5 sched: migration changes for core scheduling

From: Aubrey Li <[email protected]>

- Don't migrate if there is a cookie mismatch
Load balance tries to move task from busiest CPU to the destination CPU. When core scheduling is enabled, if the task's cookie does not match with the destination CPU's core cookie, this task will be skipped by this CPU. This mitigates the forced idle time on the destination CPU.

- Select cookie matched idle CPU
In the fast path of task wakeup, select the first cookie matched idle CPU instead of the first idle CPU.

- Find cookie matched idlest CPU
In the slow path of task wakeup, find the idlest CPU whose core cookie matches with task's cookie

- Don't migrate task if cookie not match
For the NUMA load balance, don't migrate task to the CPU whose core cookie does not match with task's cookie

---
 kernel/sched/fair.c  | 33 +++++++++++++++++---
 kernel/sched/sched.h | 72 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 101 insertions(+), 4 deletions(-)
kernel/sched/fair.c +29 -4
index d7f90765f7fd..fddd7c44bbf3 100644
@@ -1921,6 +1921,13 @@ static void task_numa_find_cpu(struct task_numa_env *env,
 		if (!cpumask_test_cpu(cpu, env->p->cpus_ptr))
 			continue;
 
+		/*
+		 * Skip this cpu if source task's cookie does not match
+		 * with CPU's core cookie.
+		 */
+		if (!sched_core_cookie_match(cpu_rq(cpu), env->p))
+			continue;
+
 		env->dst_cpu = cpu;
 		if (task_numa_compare(env, taskimp, groupimp, maymove))
 			break;
@@ -5867,11 +5874,15 @@ find_idlest_group_cpu(struct sched_group *group, struct task_struct *p, int this
 
 	/* Traverse only the allowed CPUs */
 	for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) {
+		struct rq *rq = cpu_rq(i);
+
+		if (!sched_core_cookie_match(rq, p))
+			continue;
+
 		if (sched_idle_cpu(i))
 			return i;
 
 		if (available_idle_cpu(i)) {
-			struct rq *rq = cpu_rq(i);
 			struct cpuidle_state *idle = idle_get_state(rq);
 			if (idle && idle->exit_latency < min_exit_latency) {
 				/*
@@ -6129,7 +6140,9 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int t
 	for_each_cpu_wrap(cpu, cpus, target) {
 		if (!--nr)
 			return -1;
-		if (available_idle_cpu(cpu) || sched_idle_cpu(cpu))
+
+		if ((available_idle_cpu(cpu) || sched_idle_cpu(cpu)) &&
+		    sched_cpu_cookie_match(cpu_rq(cpu), p))
 			break;
 	}
 
@@ -7530,8 +7543,9 @@ int can_migrate_task(struct task_struct *p, struct lb_env *env)
 	 * We do not migrate tasks that are:
 	 * 1) throttled_lb_pair, or
 	 * 2) cannot be migrated to this CPU due to cpus_ptr, or
-	 * 3) running (obviously), or
-	 * 4) are cache-hot on their current CPU.
+	 * 3) task's cookie does not match with this CPU's core cookie
+	 * 4) running (obviously), or
+	 * 5) are cache-hot on their current CPU.
 	 */
 	if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
 		return 0;
@@ -7566,6 +7580,13 @@ int can_migrate_task(struct task_struct *p, struct lb_env *env)
 		return 0;
 	}
 
+	/*
+	 * Don't migrate task if the task's cookie does not match
+	 * with the destination CPU's core cookie.
+	 */
+	if (!sched_core_cookie_match(cpu_rq(env->dst_cpu), p))
+		return 0;
+
 	/* Record that we found atleast one task that could run on dst_cpu */
 	env->flags &= ~LBF_ALL_PINNED;
 
@@ -8792,6 +8813,10 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
 					p->cpus_ptr))
 			continue;
 
+		/* Skip over this group if no cookie matched */
+		if (!sched_group_cookie_match(cpu_rq(this_cpu), p, group))
+			continue;
+
 		local_group = cpumask_test_cpu(this_cpu,
 					       sched_group_span(group));
 
kernel/sched/sched.h +72 -0

90 lines not on this page · raw message

Signature · 1 line
2.30.0.280.ga3ce27912f-goog
#2 lore raw

PATCH v10 2/5 sched: CGroup tagging interface for core scheduling

In reply to Joel Fernandes (Google)

From: Peter Zijlstra <[email protected]>

Marks all tasks in a cgroup as matching for core-scheduling.

A task will need to be moved into the core scheduler queue when the cgroup it belongs to is tagged to run with core scheduling. Similarly the task will need to be moved out of the core scheduler queue when the cgroup is untagged.

Also after we forked a task, its core scheduler queue's presence will need to be updated according to its new cgroup's status.

Use stop machine mechanism to update all tasks in a cgroup to prevent a new task from sneaking into the cgroup, and missed out from the update while we iterates through all the tasks in the cgroup. A more complicated scheme could probably avoid the stop machine. Such scheme will also need to resovle inconsistency between a task's cgroup core scheduling tag and residency in core scheduler queue.

We are opting for the simple stop machine mechanism for now that avoids such complications.

Core scheduler has extra overhead. Enable it only for core with more than one SMT hardware threads.

---
 include/linux/sched.h            |  10 +
 include/uapi/linux/prctl.h       |   6 +
 kernel/fork.c                    |   1 +
 kernel/sched/Makefile            |   1 +
 kernel/sched/core.c              | 136 ++++++-
 kernel/sched/coretag.c           | 669 +++++++++++++++++++++++++++++++
 kernel/sched/debug.c             |   4 +
 kernel/sched/sched.h             |  58 ++-
 kernel/sys.c                     |   7 +
 tools/include/uapi/linux/prctl.h |   6 +
 10 files changed, 878 insertions(+), 20 deletions(-)
 create mode 100644 kernel/sched/coretag.c
include/linux/sched.h +10 -0
index 7efce9c9d9cf..7ca6f2f72cda 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
 
@@ -2076,4 +2078,12 @@ int sched_trace_rq_nr_running(struct rq *rq);
 
 const struct cpumask *sched_trace_rd_span(struct root_domain *rd);
 
+#ifdef CONFIG_SCHED_CORE
+int sched_core_share_pid(unsigned long flags, pid_t pid);
+void sched_tsk_free(struct task_struct *tsk);
+#else
+#define sched_core_share_pid(flags, pid) do { } while (0)
+#define sched_tsk_free(tsk) do { } while (0)
+#endif
+
 #endif
include/uapi/linux/prctl.h +6 -0
index c334e6a02e5f..f8e4e9626121 100644
@@ -248,4 +248,10 @@ struct prctl_mm_map {
 #define PR_SET_IO_FLUSHER		57
 #define PR_GET_IO_FLUSHER		58
 
+/* Request the scheduler to share a core */
+#define PR_SCHED_CORE_SHARE		59
+# define PR_SCHED_CORE_CLEAR		0  /* clear core_sched cookie of pid */
+# define PR_SCHED_CORE_SHARE_FROM	1  /* get core_sched cookie from pid */
+# define PR_SCHED_CORE_SHARE_TO		2  /* push core_sched cookie to pid */
+
 #endif /* _LINUX_PRCTL_H */
kernel/fork.c +1 -0
index 7199d359690c..5468c93829c5 100644
@@ -736,6 +736,7 @@ void __put_task_struct(struct task_struct *tsk)
 	exit_creds(tsk);
 	delayacct_tsk_free(tsk);
 	put_signal_struct(tsk->signal);
+	sched_tsk_free(tsk);
 
 	if (!profile_handoff_task(tsk))
 		free_task(tsk);
kernel/sched/Makefile +1 -0
index 5fc9c9b70862..c526c20adf9d 100644
@@ -36,3 +36,4 @@ obj-$(CONFIG_CPU_FREQ_GOV_SCHEDUTIL) += cpufreq_schedutil.o
 obj-$(CONFIG_MEMBARRIER) += membarrier.o
 obj-$(CONFIG_CPU_ISOLATION) += isolation.o
 obj-$(CONFIG_PSI) += psi.o
+obj-$(CONFIG_SCHED_CORE) += coretag.o
kernel/sched/core.c +117 -19

238 lines not on this page · raw message

kernel/sched/coretag.c new file +669 -0

669 lines not on this page · raw message

kernel/sched/debug.c +4 -0
index 60a922d3f46f..8c452b8010ad 100644
@@ -1024,6 +1024,10 @@ void proc_sched_show_task(struct task_struct *p, struct pid_namespace *ns,
 		__PS("clock-delta", t1-t0);
 	}
 
+#ifdef CONFIG_SCHED_CORE
+	__PS("core_cookie", p->core_cookie);
+#endif
+
 	sched_show_numa(p, m);
 }
 
kernel/sched/sched.h +57 -1

87 lines not on this page · raw message

kernel/sys.c +7 -0
index a730c03ee607..da52a0da24ef 100644
@@ -2530,6 +2530,13 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 
 		error = (current->flags & PR_IO_FLUSHER) == PR_IO_FLUSHER;
 		break;
+#ifdef CONFIG_SCHED_CORE
+	case PR_SCHED_CORE_SHARE:
+		if (arg4 || arg5)
+			return -EINVAL;
+		error = sched_core_share_pid(arg2, arg3);
+		break;
+#endif
 	default:
 		error = -EINVAL;
 		break;
tools/include/uapi/linux/prctl.h +6 -0
index 7f0827705c9a..cd37bbf3cd90 100644
@@ -247,4 +247,10 @@ struct prctl_mm_map {
 #define PR_SET_IO_FLUSHER		57
 #define PR_GET_IO_FLUSHER		58
 
+/* Request the scheduler to share a core */
+#define PR_SCHED_CORE_SHARE		59
+# define PR_SCHED_CORE_CLEAR		0  /* clear core_sched cookie of pid */
+# define PR_SCHED_CORE_SHARE_FROM	1  /* get core_sched cookie from pid */
+# define PR_SCHED_CORE_SHARE_TO		2  /* push core_sched cookie to pid */
+
 #endif /* _LINUX_PRCTL_H */
Signature · 1 line
2.30.0.280.ga3ce27912f-goog
maintainer
#3 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

I'm slowly starting to go through this...

kernel/sched/core.c static inline bool __sched_core_less(struct task_struct *a, struct task_struct * · line 150

Quoted 12 lines · Joel Fernandes (Google)
kernel/sched/core.c +12 -0
+static bool sched_core_empty(struct rq *rq)
+{
+	return RB_EMPTY_ROOT(&rq->core_tree);
+}
+
+static struct task_struct *sched_core_first(struct rq *rq)
+{
+	struct task_struct *task;
+
+	task = container_of(rb_first(&rq->core_tree), struct task_struct, core_node);
+	return task;
+}

AFAICT you can do with:

static struct task_struct *sched_core_any(struct rq *rq)

{
	return rb_entry(rq->core_tree.rb_node, struct task_struct, code_node);
}

kernel/sched/core.c static inline bool __sched_core_less(struct task_struct *a, struct task_struct * · line 163

Quoted 12 lines
kernel/sched/core.c +12 -0
+static void sched_core_flush(int cpu)
+{
+	struct rq *rq = cpu_rq(cpu);
+	struct task_struct *task;
+
+	while (!sched_core_empty(rq)) {
+		task = sched_core_first(rq);
+		rb_erase(&task->core_node, &rq->core_tree);
+		RB_CLEAR_NODE(&task->core_node);
+	}
+	rq->core->core_task_seq++;
+}

However,

Quoted 8 lines
kernel/sched/core.c +8 -0
+	for_each_possible_cpu(cpu) {
+		struct rq *rq = cpu_rq(cpu);
+
+		WARN_ON_ONCE(enabled == rq->core_enabled);
+
+		if (!enabled || (enabled && cpumask_weight(cpu_smt_mask(cpu)) >= 2)) {
+			/*
+			 * All active and migrating tasks will have already

kernel/sched/core.c static int __sched_core_stopper(void *data) · line 281

Quoted 10 lines
kernel/sched/core.c +10 -0
+			 * been removed from core queue when we clear the
+			 * cgroup tags. However, dying tasks could still be
+			 * left in core queue. Flush them here.
+			 */
+			if (!enabled)
+				sched_core_flush(cpu);
+
+			rq->core_enabled = enabled;
+		}
+	}

I'm not sure I understand. Is the problem that we're still schedulable during do_exit() after cgroup_exit() ? It could be argued that when we leave the cgroup there, we should definitely leave the tag group too.

maintainer
#4 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

Quoted 53 lines · Peter Zijlstra

I'm slowly starting to go through this...

kernel/sched/core.c static inline bool __sched_core_less(struct task_struct *a, struct task_struct * · line 150

Quoted 12 lines · Joel Fernandes (Google)
kernel/sched/core.c +12 -0
+static bool sched_core_empty(struct rq *rq)
+{
+	return RB_EMPTY_ROOT(&rq->core_tree);
+}
+
+static struct task_struct *sched_core_first(struct rq *rq)
+{
+	struct task_struct *task;
+
+	task = container_of(rb_first(&rq->core_tree), struct task_struct, core_node);
+	return task;
+}

AFAICT you can do with:

static struct task_struct *sched_core_any(struct rq *rq)

{
	return rb_entry(rq->core_tree.rb_node, struct task_struct, code_node);
}

kernel/sched/core.c static inline bool __sched_core_less(struct task_struct *a, struct task_struct * · line 163

Quoted 12 lines
kernel/sched/core.c +12 -0
+static void sched_core_flush(int cpu)
+{
+	struct rq *rq = cpu_rq(cpu);
+	struct task_struct *task;
+
+	while (!sched_core_empty(rq)) {
+		task = sched_core_first(rq);
+		rb_erase(&task->core_node, &rq->core_tree);
+		RB_CLEAR_NODE(&task->core_node);
+	}
+	rq->core->core_task_seq++;
+}

However,

Quoted 14 lines
kernel/sched/core.c +14 -0
+	for_each_possible_cpu(cpu) {
+		struct rq *rq = cpu_rq(cpu);
+
+		WARN_ON_ONCE(enabled == rq->core_enabled);
+
+		if (!enabled || (enabled && cpumask_weight(cpu_smt_mask(cpu)) >= 2)) {
+			/*
+			 * All active and migrating tasks will have already
+			 * been removed from core queue when we clear the
+			 * cgroup tags. However, dying tasks could still be
+			 * left in core queue. Flush them here.
+			 */
+			if (!enabled)
+				sched_core_flush(cpu);
Quoted 8 lines

kernel/sched/core.c static int __sched_core_stopper(void *data) · line 281

Quoted 4 lines
kernel/sched/core.c +4 -0
+
+			rq->core_enabled = enabled;
+		}
+	}

I'm not sure I understand. Is the problem that we're still schedulable during do_exit() after cgroup_exit() ? It could be argued that when we leave the cgroup there, we should definitely leave the tag group too.

That is, did you forget to implement cpu_cgroup_exit()?

#5 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

Hi Peter,

Quoted 53 lines · Peter Zijlstra
Quoted 52 lines · Peter Zijlstra

I'm slowly starting to go through this...

kernel/sched/core.c static inline bool __sched_core_less(struct task_struct *a, struct task_struct * · line 150

Quoted 12 lines · Joel Fernandes (Google)
kernel/sched/core.c +12 -0
+static bool sched_core_empty(struct rq *rq)
+{
+	return RB_EMPTY_ROOT(&rq->core_tree);
+}
+
+static struct task_struct *sched_core_first(struct rq *rq)
+{
+	struct task_struct *task;
+
+	task = container_of(rb_first(&rq->core_tree), struct task_struct, core_node);
+	return task;
+}

AFAICT you can do with:

static struct task_struct *sched_core_any(struct rq *rq)

{
	return rb_entry(rq->core_tree.rb_node, struct task_struct, code_node);
}

kernel/sched/core.c static inline bool __sched_core_less(struct task_struct *a, struct task_struct * · line 163

Quoted 12 lines
kernel/sched/core.c +12 -0
+static void sched_core_flush(int cpu)
+{
+	struct rq *rq = cpu_rq(cpu);
+	struct task_struct *task;
+
+	while (!sched_core_empty(rq)) {
+		task = sched_core_first(rq);
+		rb_erase(&task->core_node, &rq->core_tree);
+		RB_CLEAR_NODE(&task->core_node);
+	}
+	rq->core->core_task_seq++;
+}

However,

Quoted 13 lines
kernel/sched/core.c +13 -0
+	for_each_possible_cpu(cpu) {
+		struct rq *rq = cpu_rq(cpu);
+
+		WARN_ON_ONCE(enabled == rq->core_enabled);
+
+		if (!enabled || (enabled && cpumask_weight(cpu_smt_mask(cpu)) >= 2)) {
+			/*
+			 * All active and migrating tasks will have already
+			 * been removed from core queue when we clear the
+			 * cgroup tags. However, dying tasks could still be
+			 * left in core queue. Flush them here.
+			 */
+			if (!enabled)
Quoted 8 lines
Quoted 8 lines

kernel/sched/core.c static int __sched_core_stopper(void *data) · line 281

Quoted 5 lines
kernel/sched/core.c +5 -0
+				sched_core_flush(cpu);
+
+			rq->core_enabled = enabled;
+		}
+	}

I'm not sure I understand. Is the problem that we're still schedulable during do_exit() after cgroup_exit() ?

Yes, exactly. Tim had written this code in the original patches and it carried (I was not involved at that time). IIRC, the issue is the exit will race with core scheduling being disabled. Even after core sched is disabled, it will still exist in the core rb tree and needs to be removed. Otherwise it causes crashes.

Quoted 4 lines

It could be argued that when we

Quoted 1 line

leave the cgroup there, we should definitely leave the tag group too.

That is, did you forget to implement cpu_cgroup_exit()?

Yes, I think it is better to implement it in cpu_cgroup_exit().

thanks,

- Joel

maintainer
#6 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

In reply to Joel Fernandes (Google)
Quoted 1 line · Joel Fernandes (Google)

From: Peter Zijlstra <[email protected]>

I'm thinking this is too much credit, I didn't write much of this.

Quoted 17 lines

Marks all tasks in a cgroup as matching for core-scheduling.

A task will need to be moved into the core scheduler queue when the cgroup it belongs to is tagged to run with core scheduling. Similarly the task will need to be moved out of the core scheduler queue when the cgroup is untagged.

Also after we forked a task, its core scheduler queue's presence will need to be updated according to its new cgroup's status.

Use stop machine mechanism to update all tasks in a cgroup to prevent a new task from sneaking into the cgroup, and missed out from the update while we iterates through all the tasks in the cgroup. A more complicated scheme could probably avoid the stop machine. Such scheme will also need to resovle inconsistency between a task's cgroup core scheduling tag and residency in core scheduler queue.

Quoted 5 lines

We are opting for the simple stop machine mechanism for now that avoids such complications.

Core scheduler has extra overhead. Enable it only for core with more than one SMT hardware threads.

Very little actual words on the desired and implemented semantics of the interface, while the patch contains much non-obvious complication.

#7 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

Quoted 23 lines · Peter Zijlstra
Quoted 1 line · Joel Fernandes (Google)

From: Peter Zijlstra <[email protected]>

I'm thinking this is too much credit, I didn't write much of this.

Quoted 17 lines

Marks all tasks in a cgroup as matching for core-scheduling.

A task will need to be moved into the core scheduler queue when the cgroup it belongs to is tagged to run with core scheduling. Similarly the task will need to be moved out of the core scheduler queue when the cgroup is untagged.

Also after we forked a task, its core scheduler queue's presence will need to be updated according to its new cgroup's status.

Use stop machine mechanism to update all tasks in a cgroup to prevent a new task from sneaking into the cgroup, and missed out from the update while we iterates through all the tasks in the cgroup. A more complicated scheme could probably avoid the stop machine. Such scheme will also need to resovle inconsistency between a task's cgroup core scheduling tag and residency in core scheduler queue.

Quoted 8 lines
Quoted 5 lines

We are opting for the simple stop machine mechanism for now that avoids such complications.

Core scheduler has extra overhead. Enable it only for core with more than one SMT hardware threads.

Very little actual words on the desired and implemented semantics of the interface, while the patch contains much non-obvious complication.

Ack to both, will fix in the next posting.

maintainer
#8 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

In reply to Joel Fernandes (Google)

include/uapi/linux/prctl.h struct prctl_mm_map { · line 255

Quoted 5 lines · Joel Fernandes (Google)
include/uapi/linux/prctl.h +5 -0
+/* Request the scheduler to share a core */
+#define PR_SCHED_CORE_SHARE		59
+# define PR_SCHED_CORE_CLEAR		0  /* clear core_sched cookie of pid */
+# define PR_SCHED_CORE_SHARE_FROM	1  /* get core_sched cookie from pid */
+# define PR_SCHED_CORE_SHARE_TO		2  /* push core_sched cookie to pid */

Why ?

Also, how do I set a unique cookie on myself with this interface?

#9 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

Quoted 8 lines · Peter Zijlstra

include/uapi/linux/prctl.h struct prctl_mm_map { · line 255

Quoted 5 lines · Joel Fernandes (Google)
include/uapi/linux/prctl.h +5 -0
+/* Request the scheduler to share a core */
+#define PR_SCHED_CORE_SHARE		59
+# define PR_SCHED_CORE_CLEAR		0  /* clear core_sched cookie of pid */
+# define PR_SCHED_CORE_SHARE_FROM	1  /* get core_sched cookie from pid */
+# define PR_SCHED_CORE_SHARE_TO		2  /* push core_sched cookie to pid */

Why ?

The simplest interface would be a single 'set' command that specifies and sets a cookie value. Using 0 as a special value could then clear it. However, an early requirement that people seemed to agree with, is that cookies should be opaque and system guaranteed unique except when explicitly shared. Thus, since two tasks cannot share a cookie by explicitly setting the same cookie value, the prctl() must provide for a means of cookie sharing between tasks. The v9 proposal had incorporated all of this into a single "from-only" command whose actions depended on the state of the two tasks. If neither have a cookie and one shares from the other, they both get the same new cookie. If the calling task had one and the other didn't, the calling task's cookie was cleared. And of course if the src task has a cookie, the caller just gets it. Does a lot, tad bit overloaded, and still insufficient.

A second complication was a decision that new processes (not threads) do not inherit their parents cookie. Thus forking is also not a means to share a cookie. Basically with a "from-only" interface, the new task would need to be modified to call prctl() itself. From-only also does not allow for setting a cookie on an unmodified already running task. This can be fixed by providing both a "to" and "from" sharing interface that allows helper programs to construct arbitrary configurations from unmodified programs.

Quoted 1 line

Also, how do I set a unique cookie on myself with this interface?

The v10 patch still uses the overloaded v9 mechanism (which as mentioned above is if two tasks w/o cookies share a cookie they get a new shared unique cookie). Yes, that is clearly an inconsistency and kludgy. The mechanism is documented in the docs, but clearly not obvious from the interface above. I think we got a bit overzealous in patch squashing and much of this verbiage should have been in the combined commit message.

So based on the above, how about we add a "create" to pair with "clear" and call it "create" vs "set" since we are creating a unique opaque cookie versus setting a particular value. And as mentioned, because one can't specify a cookie directly but only thru sharing relationships, we need both "to" and "from" to make it completely usable.

So we end up with something like this:

    PR_SCHED_CORE_CREATE                       -- give yourself a unique cookie
    PR_SCHED_CORE_CLEAR                        -- clear your core sched cookie
    PR_SCHED_CORE_SHARE_FROM <src_task>        -- get their cookie for you
    PR_SCHED_CORE_SHARE_TO   <dest_task>       -- push your cookie to them

An additional question is should the inheritability of a process' cookie be configurable? The current code gives the child process their own unique cookie if the parent had a cookie. That is useful in some cases, but many other configurations could be made much easier with inheritance.

If configurable cookie inheritance would be useful, it might look something like this:

PR_SCHED_CORE_CHILD_INHERIT <0/1> -- 1 - child inherits cookie from parent. 0 - If parent has a cookie, child process gets a unique cookie.

-chrish

maintainer
#10 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

Quoted 7 lines · Chris Hyser

A second complication was a decision that new processes (not threads) do not inherit their parents cookie. Thus forking is also not a means to share a cookie. Basically with a "from-only" interface, the new task would need to be modified to call prctl() itself. From-only also does not allow for setting a cookie on an unmodified already running task. This can be fixed by providing both a "to" and "from" sharing interface that allows helper programs to construct arbitrary configurations from unmodified programs.

Do we really want to inhibit on fork() or would exec() be a better place? What about those applications that use fork() based workers?

Quoted 6 lines
Quoted 1 line

Also, how do I set a unique cookie on myself with this interface?

The v10 patch still uses the overloaded v9 mechanism (which as mentioned above is if two tasks w/o cookies share a cookie they get a new shared unique cookie). Yes, that is clearly an inconsistency and kludgy. The mechanism is documented in the docs, but clearly not obvious from the

I've not seen a document so far (also, I'm not one to actually read documentation, much preferring comments and Changelogs).

Quoted 11 lines

So based on the above, how about we add a "create" to pair with "clear" and call it "create" vs "set" since we are creating a unique opaque cookie versus setting a particular value. And as mentioned, because one can't specify a cookie directly but only thru sharing relationships, we need both "to" and "from" to make it completely usable.

So we end up with something like this:

    PR_SCHED_CORE_CREATE                       -- give yourself a unique cookie
    PR_SCHED_CORE_CLEAR                        -- clear your core sched cookie
    PR_SCHED_CORE_SHARE_FROM <src_task>        -- get their cookie for you
    PR_SCHED_CORE_SHARE_TO   <dest_task>       -- push your cookie to them

I'm still wondering why we need _FROM/_TO. What exactly will we miss with just _SHARE <pid>?

	current		arg_task
	<none>		<none>		-EDAFT
	<none>		<cookie>	current gets cookie
	<cookie>	<none>		arg_task gets cookie
	<cookie>	<cookie>	-EDAFTER

(I have a suspicion, but I want to see it spelled out).

Also, do we wants this interface to be able to work on processes? Things like fcntl(F_SETOWN_EX) allow you to specify a PID type.

Quoted 4 lines

An additional question is should the inheritability of a process' cookie be configurable? The current code gives the child process their own unique cookie if the parent had a cookie. That is useful in some cases, but many other configurations could be made much easier with inheritance.

What was the argument for not following the traditional fork() semantics and inheriting everything?

#11 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

Quoted 10 lines · Peter Zijlstra
Quoted 7 lines · Chris Hyser

A second complication was a decision that new processes (not threads) do not inherit their parents cookie. Thus forking is also not a means to share a cookie. Basically with a "from-only" interface, the new task would need to be modified to call prctl() itself. From-only also does not allow for setting a cookie on an unmodified already running task. This can be fixed by providing both a "to" and "from" sharing interface that allows helper programs to construct arbitrary configurations from unmodified programs.

Quoted 2 lines

Do we really want to inhibit on fork() or would exec() be a better place? What about those applications that use fork() based workers?

I like exec-time as a fan of fork-based workers. I suppose the counter argument would be security, but the new process is in a state to be trusted to lower it's privileges, change permissions, core sched cookie etc before it makes itself differently vulnerable and because it is the same code, it "knows" if it did.

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

Also, how do I set a unique cookie on myself with this interface?

The v10 patch still uses the overloaded v9 mechanism (which as mentioned above is if two tasks w/o cookies share a cookie they get a new shared unique cookie). Yes, that is clearly an inconsistency and kludgy. The mechanism is documented in the docs, but clearly not obvious from the

I've not seen a document so far (also, I'm not one to actually read documentation, much preferring comments and Changelogs).

Understood. I think we should split this patch into separate prctl and cgroup patches. The rationale decided here would then go into the prctl patch commit message. We can also use this split to address any dependencies we've created on cgroups that you mentioned in a different email.

Quoted 15 lines
Quoted 11 lines

So based on the above, how about we add a "create" to pair with "clear" and call it "create" vs "set" since we are creating a unique opaque cookie versus setting a particular value. And as mentioned, because one can't specify a cookie directly but only thru sharing relationships, we need both "to" and "from" to make it completely usable.

So we end up with something like this:

     PR_SCHED_CORE_CREATE                       -- give yourself a unique cookie
     PR_SCHED_CORE_CLEAR                        -- clear your core sched cookie
     PR_SCHED_CORE_SHARE_FROM <src_task>        -- get their cookie for you
     PR_SCHED_CORE_SHARE_TO   <dest_task>       -- push your cookie to them

I'm still wondering why we need _FROM/_TO. What exactly will we miss with just _SHARE <pid>?

Quoted 7 lines
	current		arg_task
	<none>		<none>		-EDAFT
	<none>		<cookie>	current gets cookie
	<cookie>	<none>		arg_task gets cookie
	<cookie>	<cookie>	-EDAFTER

(I have a suspicion, but I want to see it spelled out).

The min requirements of the interface I see are:

1. create a my own cookie
2. clear my own cookie
3. create a cookie for a running unmodified task
4. clear a cookie for a running unmodified task
5. copy a cookie from one running unmodified task to another unmodified task

So from your example above:

Quoted 1 line

<none> <cookie> current gets cookie

could also mean clear the cookie of arg_task and satisfy requirement 4 above.

"Share" is a fuzzy term. I should have used COPY as that is more the semantics I was thinking ... specified directional transfer. So we could have a single command with two arguments where argument order determines direction. In the v10 interface proposal, as one argument, current, was always implied, direction had to be specified in the command.

So a single copy command could be something like:

PR_SCHED_CORE_COPY <src_task> <dst_task>

to replace the two. The very first util you write to do any thing useful w/ all of this is a "copy_cookie <spid> <dpid>". :-)

Quoted 2 lines

Also, do we wants this interface to be able to work on processes? Things like fcntl(F_SETOWN_EX) allow you to specify a PID type.

Yes and creating/clearing a cookie on a PGID and SID seem like useful shortcuts to look into.

Quoted 7 lines
Quoted 4 lines

An additional question is should the inheritability of a process' cookie be configurable? The current code gives the child process their own unique cookie if the parent had a cookie. That is useful in some cases, but many other configurations could be made much easier with inheritance.

What was the argument for not following the traditional fork() semantics and inheriting everything?

The code just showed up with no explanation :-), but I think I know what was intended and it touches on the same security policy type problem you mentioned in a comment on the CLEAR code. In a secure context, you can't just allow a random user to clear their cookie, i.e. make themselves trusted. At the same time, in a non-secure context, and several use cases have been put forward, I can't think of anything more annoying then being able to set a cookie on my task and then not having permission to clear it.

The fork scenario has a similar problem. A child inheriting the cookie means just that, but not inheriting is likely different depending on whether its secure vs non-secure (and obviously we can't use those terms. Who wants to advocate for non-security :-). For non-secure, don't inherit means the child gets no cookie; secure means the child gets their own unique cookie and not the parent's. In the absence of any way to set a policy, the current code chose the secure default which makes sense.

So I guess that raises the ugly question, do we need some kind of globally scoped, "secure/not-secure" core sched policy flag?

-chrish

maintainer
#12 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

In reply to Joel Fernandes (Google)
Quoted 7 lines · Joel Fernandes (Google)
kernel/sched/core.c +7 -0
+#ifdef CONFIG_SCHED_DEBUG
+	/* Read the group cookie. */
+	{
+		.name = "core_group_cookie",
+		.flags = CFTYPE_NOT_ON_ROOT,
+		.read_u64 = cpu_core_group_cookie_read_u64,
+	},

kernel/sched/core.c static struct cftype cpu_legacy_files[] = { · line 9529

Quoted 10 lines
kernel/sched/core.c +9 -0
+#endif
 
+#ifdef CONFIG_SCHED_DEBUG
+	/* Read the group cookie. */
+	{
+		.name = "core_group_cookie",
+		.flags = CFTYPE_NOT_ON_ROOT,
+		.read_u64 = cpu_core_group_cookie_read_u64,
+	},
+#endif

AFAICT this leaks kernel pointers. IIRC that was a bad thing.

#13 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

Quoted 12 lines · Peter Zijlstra
Quoted 8 lines · Joel Fernandes (Google)
kernel/sched/core.c +8 -0
+#ifdef CONFIG_SCHED_DEBUG
+     /* Read the group cookie. */
+     {
+             .name = "core_group_cookie",
+             .flags = CFTYPE_NOT_ON_ROOT,
+             .read_u64 = cpu_core_group_cookie_read_u64,
+     },
+#endif
Quoted 10 lines

kernel/sched/core.c static struct cftype cpu_legacy_files[] = { · line 9529

Quoted 8 lines
kernel/sched/core.c +8 -0
+#ifdef CONFIG_SCHED_DEBUG
+     /* Read the group cookie. */
+     {
+             .name = "core_group_cookie",
+             .flags = CFTYPE_NOT_ON_ROOT,
+             .read_u64 = cpu_core_group_cookie_read_u64,
+     },
+#endif

AFAICT this leaks kernel pointers. IIRC that was a bad thing.

For that matter, we're also exposing the cookie pointer in /proc/$pid/sched. Currently these are used by the selftests to validate that two tasks are/aren't sharing. If this poses a risk, we can rework to avoid exposing the actual pointers.

maintainer
#14 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

In reply to Joel Fernandes (Google)
Quoted 34 lines · Joel Fernandes (Google)
kernel/sched/coretag.c new file +31 -0
+static inline void __sched_core_erase_cookie(struct sched_core_cookie *cookie)
+{
+	lockdep_assert_held(&sched_core_cookies_lock);
+
+	/* Already removed */
+	if (RB_EMPTY_NODE(&cookie->node))
+		return;
+
+	rb_erase(&cookie->node, &sched_core_cookies);
+	RB_CLEAR_NODE(&cookie->node);
+}
+
+/* Called when a task no longer points to the cookie in question */
+static void sched_core_put_cookie(struct sched_core_cookie *cookie)
+{
+	unsigned long flags;
+
+	if (!cookie)
+		return;
+
+	if (refcount_dec_and_test(&cookie->refcnt)) {
+		raw_spin_lock_irqsave(&sched_core_cookies_lock, flags);
+		__sched_core_erase_cookie(cookie);
+		raw_spin_unlock_irqrestore(&sched_core_cookies_lock, flags);
+		kfree(cookie);
+	}
+}
 
+static void __sched_core_update_cookie(struct task_struct *p)
+{
 
+	raw_spin_lock(&sched_core_cookies_lock);
 
+		/*

kernel/sched/coretag.c line 190

Quoted 10 lines
kernel/sched/coretag.c new file +10 -0
+		 * Cookie exists, increment refcnt. If refcnt is currently 0,
+		 * we're racing with a put() (refcnt decremented but cookie not
+		 * yet removed from the tree). In this case, we can simply
+		 * perform the removal ourselves and retry.
+		 * sched_core_put_cookie() will still function correctly.
+		 */
+		if (unlikely(!refcount_inc_not_zero(&match->refcnt))) {
+			__sched_core_erase_cookie(match);
+			goto retry;
+		}

refcount_dec_and_lock() avoids that complication.

#15 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

Quoted 2 lines · Peter Zijlstra

refcount_dec_and_lock() avoids that complication.

There isn't currently an interface for raw_spin_locks. Certainly could add a new interface in a separate patch as part of this series, but doesn't seem that bad to do the locking inline.

maintainer
#16 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

In reply to Joel Fernandes (Google)
Quoted 34 lines · Joel Fernandes (Google)
kernel/sched/coretag.c new file +34 -0
+static void sched_core_update_cookie(struct task_struct *p, unsigned long cookie,
+				     enum sched_core_cookie_type cookie_type)
+{
+	struct rq_flags rf;
+	struct rq *rq;
+
+	if (!p)
+		return;
+
+	rq = task_rq_lock(p, &rf);
+
+	switch (cookie_type) {
+	case sched_core_task_cookie_type:
+		p->core_task_cookie = cookie;
+		break;
+	case sched_core_group_cookie_type:
+		p->core_group_cookie = cookie;
+		break;
+	default:
+		WARN_ON_ONCE(1);
+	}
+
+	/* Set p->core_cookie, which is the overall cookie */
+	__sched_core_update_cookie(p);
+
+	if (sched_core_enqueued(p)) {
+		sched_core_dequeue(rq, p);
+		if (!p->core_cookie) {
+			task_rq_unlock(rq, p, &rf);
+			return;
+		}
+	}
+
+	if (sched_core_enabled(rq) &&

kernel/sched/coretag.c line 251

Quoted 10 lines
kernel/sched/coretag.c new file +10 -0
+	    p->core_cookie && task_on_rq_queued(p))
+		sched_core_enqueue(task_rq(p), p);
+
+	/*
+	 * If task is currently running or waking, it may not be compatible
+	 * anymore after the cookie change, so enter the scheduler on its CPU
+	 * to schedule it away.
+	 */
+	if (task_running(rq, p) || p->state == TASK_WAKING)
+		resched_curr(rq);

I'm not immediately seeing the need for that WAKING test. Since we're holding it's rq->lock, the only place that task can be WAKING is on the wake_list. And if it's there, it needs to acquire rq->lock to get enqueued, and rq->lock again to get scheduled.

What am I missing?

Quoted 3 lines
kernel/sched/coretag.c new file +3 -0
+
+	task_rq_unlock(rq, p, &rf);
+}
#17 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

Quoted 51 lines · Peter Zijlstra
Quoted 34 lines · Joel Fernandes (Google)
kernel/sched/coretag.c new file +34 -0
+static void sched_core_update_cookie(struct task_struct *p, unsigned long cookie,
+				     enum sched_core_cookie_type cookie_type)
+{
+	struct rq_flags rf;
+	struct rq *rq;
+
+	if (!p)
+		return;
+
+	rq = task_rq_lock(p, &rf);
+
+	switch (cookie_type) {
+	case sched_core_task_cookie_type:
+		p->core_task_cookie = cookie;
+		break;
+	case sched_core_group_cookie_type:
+		p->core_group_cookie = cookie;
+		break;
+	default:
+		WARN_ON_ONCE(1);
+	}
+
+	/* Set p->core_cookie, which is the overall cookie */
+	__sched_core_update_cookie(p);
+
+	if (sched_core_enqueued(p)) {
+		sched_core_dequeue(rq, p);
+		if (!p->core_cookie) {
+			task_rq_unlock(rq, p, &rf);
+			return;
+		}
+	}
+
+	if (sched_core_enabled(rq) &&

kernel/sched/coretag.c line 251

Quoted 10 lines
kernel/sched/coretag.c new file +10 -0
+	    p->core_cookie && task_on_rq_queued(p))
+		sched_core_enqueue(task_rq(p), p);
+
+	/*
+	 * If task is currently running or waking, it may not be compatible
+	 * anymore after the cookie change, so enter the scheduler on its CPU
+	 * to schedule it away.
+	 */
+	if (task_running(rq, p) || p->state == TASK_WAKING)
+		resched_curr(rq);

I'm not immediately seeing the need for that WAKING test. Since we're holding it's rq->lock, the only place that task can be WAKING is on the wake_list. And if it's there, it needs to acquire rq->lock to get enqueued, and rq->lock again to get scheduled.

Quoted 1 line

What am I missing?

Hi Peter,

I did this way following a similar pattern in affine_move_task(). However, I think you are right. Unlike in the case affine_move_task(), we have schedule() to do the right thing for us in case of any races with wakeup. So the TASK_WAKING test is indeed not needed and we can drop tha test. Apologies for adding the extra test out of paranoia.

thanks,

- Joel

maintainer
#18 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

In reply to Joel Fernandes (Google)
Quoted 52 lines · Joel Fernandes (Google)
kernel/fork.c +38 -0
@@ -736,6 +736,7 @@ void __put_task_struct(struct task_struct *tsk)
 	exit_creds(tsk);
 	delayacct_tsk_free(tsk);
 	put_signal_struct(tsk->signal);
+	sched_tsk_free(tsk);
 
 	if (!profile_handoff_task(tsk))
 		free_task(tsk);
 
+struct sched_core_task_cookie {
+	refcount_t refcnt;
+	struct work_struct work; /* to free in WQ context. */;
+};
 
+static void sched_core_put_cookie_work(struct work_struct *ws);
+
+/* Caller has to call sched_core_get() if non-zero value is returned. */
+static unsigned long sched_core_alloc_task_cookie(void)
+{
+	struct sched_core_task_cookie *ck =
+		kmalloc(sizeof(struct sched_core_task_cookie), GFP_KERNEL);
+
+	if (!ck)
+		return 0;
+	refcount_set(&ck->refcnt, 1);
+	INIT_WORK(&ck->work, sched_core_put_cookie_work);
+
+	return (unsigned long)ck;
+}
 
+static void sched_core_put_task_cookie(unsigned long cookie)
+{
+	struct sched_core_task_cookie *ptr =
+		(struct sched_core_task_cookie *)cookie;
+
+	if (refcount_dec_and_test(&ptr->refcnt))
+		kfree(ptr);
+}
 
+static void sched_core_put_cookie_work(struct work_struct *ws)
+{
+	struct sched_core_task_cookie *ck =
+		container_of(ws, struct sched_core_task_cookie, work);
+
+	sched_core_put_task_cookie((unsigned long)ck);
+	sched_core_put();
+}
 
+void sched_tsk_free(struct task_struct *tsk)
+{

kernel/fork.c void __put_task_struct(struct task_struct *tsk)

Quoted 10 lines
kernel/fork.c +10 -0
+	struct sched_core_task_cookie *ck;
+
+	sched_core_put_cookie((struct sched_core_cookie *)tsk->core_cookie);
+
+	if (!tsk->core_task_cookie)
+		return;
+
+	ck = (struct sched_core_task_cookie *)tsk->core_task_cookie;
+	queue_work(system_wq, &ck->work);
+}

*groan*.. so the purpose of that work is to be able to disable core-scheduling after the last such task dies.

Maybe add sched_core_put_async() instead?

maintainer
#19 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

In reply to Joel Fernandes (Google)
Quoted 18 lines · Joel Fernandes (Google)
kernel/sched/coretag.c new file +17 -0
+/* All active sched_core_cookies */
+static struct rb_root sched_core_cookies = RB_ROOT;
+static DEFINE_RAW_SPINLOCK(sched_core_cookies_lock);
 
+/*
+ * Returns the following:
+ * a < b  => -1
+ * a == b => 0
+ * a > b  => 1
+ */
+static int sched_core_cookie_cmp(const struct sched_core_cookie *a,
+				 const struct sched_core_cookie *b)
+{
+#define COOKIE_CMP_RETURN(field) do {		\
+	if (a->field < b->field)		\
+		return -1;			\
+	else if (a->field > b->field)		\
+		return 1;			\

kernel/sched/coretag.c line 70

Quoted 10 lines
kernel/sched/coretag.c new file +10 -0
+} while (0)					\
+
+	COOKIE_CMP_RETURN(task_cookie);
+	COOKIE_CMP_RETURN(group_cookie);
+
+	/* all cookie fields match */
+	return 0;
+
+#undef COOKIE_CMP_RETURN
+}

AFAICT all this madness exists because cgroup + task interaction, yet none of that code is actually dependent on cgroups being on.

So this seems to implement semantics that will make two tasks that share a cookie, but are then placed in different cgroups not actually share.

Is that desired? Can we justify these semantics and the resulting code complexity.

#20 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

Quoted 35 lines · Peter Zijlstra
Quoted 18 lines · Joel Fernandes (Google)
kernel/sched/coretag.c new file +17 -0
+/* All active sched_core_cookies */
+static struct rb_root sched_core_cookies = RB_ROOT;
+static DEFINE_RAW_SPINLOCK(sched_core_cookies_lock);
 
+/*
+ * Returns the following:
+ * a < b  => -1
+ * a == b => 0
+ * a > b  => 1
+ */
+static int sched_core_cookie_cmp(const struct sched_core_cookie *a,
+                              const struct sched_core_cookie *b)
+{
+#define COOKIE_CMP_RETURN(field) do {                \
+     if (a->field < b->field)                \
+             return -1;                      \
+     else if (a->field > b->field)           \
+             return 1;                       \

kernel/sched/coretag.c line 70

Quoted 10 lines
kernel/sched/coretag.c new file +10 -0
+} while (0)                                  \
+
+     COOKIE_CMP_RETURN(task_cookie);
+     COOKIE_CMP_RETURN(group_cookie);
+
+     /* all cookie fields match */
+     return 0;
+
+#undef COOKIE_CMP_RETURN
+}

AFAICT all this madness exists because cgroup + task interaction, yet none of that code is actually dependent on cgroups being on.

Quoted 5 lines

So this seems to implement semantics that will make two tasks that share a cookie, but are then placed in different cgroups not actually share.

Is that desired? Can we justify these semantics and the resulting code complexity.

Yes that is the desired result. IMO it is less optimal from an interface perspective if we were to instead have group or task cookie override the other. Joel gave some additional justification here:
https://lkml.org/lkml/2020/12/6/389.

maintainer
#21 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

In reply to Joel Fernandes (Google)
Quoted 4 lines · Joel Fernandes (Google)
kernel/sched/coretag.c new file +4 -0
+int sched_core_share_tasks(struct task_struct *t1, struct task_struct *t2)
+{
+	static DEFINE_MUTEX(sched_core_tasks_mutex);
+	unsigned long cookie;

kernel/sched/coretag.c line 337

Quoted 10 lines
kernel/sched/coretag.c new file +10 -0
+	int ret = -ENOMEM;
+
+	mutex_lock(&sched_core_tasks_mutex);
+
+	if (!t2) {
+		if (t1->core_task_cookie) {
+			sched_core_put_task_cookie(t1->core_task_cookie);
+			sched_core_update_task_cookie(t1, 0);
+			sched_core_put();
+		}

So this seems to be the bit that implements _CLEAR. ISTR there were security implications / considerations here.

When the machine is vulnerable to L1TF/MDS and the like, clearing the cookie would gain privilege and should thus be subject to some checks, but I can'd find anything.

At the very least that deserves a comment I'm thinking.

#22 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

In reply to Joel Fernandes (Google)
Quoted 114 lines · Joel Fernandes (Google)
kernel/sched/coretag.c new file +114 -0
+static void __sched_core_update_cookie(struct task_struct *p)
+{
+	struct rb_node *parent, **node;
+	struct sched_core_cookie *node_core_cookie, *match;
+	static const struct sched_core_cookie zero_cookie;
+	struct sched_core_cookie requested_cookie;
+	bool is_zero_cookie;
+	struct sched_core_cookie *const curr_cookie =
+		(struct sched_core_cookie *)p->core_cookie;
+
+	/*
+	 * Ensures that we do not cause races/corruption by modifying/reading
+	 * task cookie fields. Also ensures task cannot get migrated.
+	 */
+	lockdep_assert_held(rq_lockp(task_rq(p)));
+
+	sched_core_cookie_init_from_task(&requested_cookie, p);
+
+	is_zero_cookie = !sched_core_cookie_cmp(&requested_cookie, &zero_cookie);
+
+	/*
+	 * Already have a cookie matching the requested settings? Nothing to
+	 * do.
+	 */
+	if ((curr_cookie && !sched_core_cookie_cmp(curr_cookie, &requested_cookie)) ||
+	    (!curr_cookie && is_zero_cookie))
+		return;
+
+	raw_spin_lock(&sched_core_cookies_lock);
+
+	if (is_zero_cookie) {
+		match = NULL;
+		goto finish;
+	}
+
+retry:
+	match = NULL;
+
+	node = &sched_core_cookies.rb_node;
+	parent = *node;
+	while (*node) {
+		int cmp;
+
+		node_core_cookie =
+			container_of(*node, struct sched_core_cookie, node);
+		parent = *node;
+
+		cmp = sched_core_cookie_cmp(&requested_cookie, node_core_cookie);
+		if (cmp < 0) {
+			node = &parent->rb_left;
+		} else if (cmp > 0) {
+			node = &parent->rb_right;
+		} else {
+			match = node_core_cookie;
+			break;
+		}
+	}
+
+	if (!match) {
+		/* No existing cookie; create and insert one */
+		match = kmalloc(sizeof(struct sched_core_cookie), GFP_ATOMIC);
+
+		/* Fall back to zero cookie */
+		if (WARN_ON_ONCE(!match))
+			goto finish;
+
+		*match = requested_cookie;
+		refcount_set(&match->refcnt, 1);
+
+		rb_link_node(&match->node, parent, node);
+		rb_insert_color(&match->node, &sched_core_cookies);
+	} else {
+		/*
+		 * Cookie exists, increment refcnt. If refcnt is currently 0,
+		 * we're racing with a put() (refcnt decremented but cookie not
+		 * yet removed from the tree). In this case, we can simply
+		 * perform the removal ourselves and retry.
+		 * sched_core_put_cookie() will still function correctly.
+		 */
+		if (unlikely(!refcount_inc_not_zero(&match->refcnt))) {
+			__sched_core_erase_cookie(match);
+			goto retry;
+		}
+	}
+
+finish:
+	p->core_cookie = (unsigned long)match;
+
+	raw_spin_unlock(&sched_core_cookies_lock);
+
+	sched_core_put_cookie(curr_cookie);
+}
+
+/*
+ * sched_core_update_cookie - Common helper to update a task's core cookie. This
+ * updates the selected cookie field and then updates the overall cookie.
+ * @p: The task whose cookie should be updated.
+ * @cookie: The new cookie.
+ * @cookie_type: The cookie field to which the cookie corresponds.
+ */
+static void sched_core_update_cookie(struct task_struct *p, unsigned long cookie,
+				     enum sched_core_cookie_type cookie_type)
+{
+	struct rq_flags rf;
+	struct rq *rq;
+
+	if (!p)
+		return;
+
+	rq = task_rq_lock(p, &rf);
+
+	switch (cookie_type) {
+	case sched_core_task_cookie_type:
+		p->core_task_cookie = cookie;

kernel/sched/coretag.c line 231

Quoted 10 lines
kernel/sched/coretag.c new file +10 -0
+		break;
+	case sched_core_group_cookie_type:
+		p->core_group_cookie = cookie;
+		break;
+	default:
+		WARN_ON_ONCE(1);
+	}
+
+	/* Set p->core_cookie, which is the overall cookie */
+	__sched_core_update_cookie(p);

While trying to test the new prctl() code I'm working on, I ran into a bug I chased back into this v10 code. Under a fair amount of stress, when the function __sched_core_update_cookie() is ultimately called from sched_core_fork(), the system deadlocks or otherwise non-visibly crashes. I've not had much success figuring out why/what. I'm running with LOCKDEP on and seeing no complaints. Duplicating it only requires setting a cookie on a task and forking a bunch of threads ... all of which then want to update their cookie.

-chrish

maintainer
#23 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

Quoted 9 lines · Chris Hyser

On 1/22/21 8:17 PM, Joel Fernandes (Google) wrote:
While trying to test the new prctl() code I'm working on, I ran into a bug I chased back into this v10 code. Under a fair amount of stress, when the function __sched_core_update_cookie() is ultimately called from sched_core_fork(), the system deadlocks or otherwise non-visibly crashes. I've not had much success figuring out why/what. I'm running with LOCKDEP on and seeing no complaints. Duplicating it only requires setting a cookie on a task and forking a bunch of threads ... all of which then want to update their cookie.

Can you share the code and reproducer?

#24 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

Quoted 11 lines · Peter Zijlstra
Quoted 9 lines · Chris Hyser

On 1/22/21 8:17 PM, Joel Fernandes (Google) wrote:
While trying to test the new prctl() code I'm working on, I ran into a bug I chased back into this v10 code. Under a fair amount of stress, when the function __sched_core_update_cookie() is ultimately called from sched_core_fork(), the system deadlocks or otherwise non-visibly crashes. I've not had much success figuring out why/what. I'm running with LOCKDEP on and seeing no complaints. Duplicating it only requires setting a cookie on a task and forking a bunch of threads ... all of which then want to update their cookie.

Quoted 1 line

Can you share the code and reproducer?

Attached is a tarball with c code (source) and scripts. Just run ./setup_bug which will compile the source and start a bash with a cs cookie. Then run ./show_bug which dumps the cookie and then fires off some processes and threads. Note the cs_clone command is not doing any core sched prctls for this test (not needed and currently coded for a diff prctl interface). It just creates processes and threads. I see this hang almost instantly.

Josh, I did verify that this occurs on Joel's coresched tree both with and w/o the kprot patch and that should exactly correspond to these patches.

-chrish

#25 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

Quoted 20 lines · Chris Hyser
Quoted 11 lines · Peter Zijlstra
Quoted 9 lines · Chris Hyser

On 1/22/21 8:17 PM, Joel Fernandes (Google) wrote:
While trying to test the new prctl() code I'm working on, I ran into a bug I chased back into this v10 code. Under a fair amount of stress, when the function __sched_core_update_cookie() is ultimately called from sched_core_fork(), the system deadlocks or otherwise non-visibly crashes. I've not had much success figuring out why/what. I'm running with LOCKDEP on and seeing no complaints. Duplicating it only requires setting a cookie on a task and forking a bunch of threads ... all of which then want to update their cookie.

Quoted 1 line

Can you share the code and reproducer?

Attached is a tarball with c code (source) and scripts. Just run ./setup_bug which will compile the source and start a bash with a cs cookie. Then run ./show_bug which dumps the cookie and then fires off some processes and threads. Note the cs_clone command is not doing any core sched prctls for this test (not needed and currently coded for a diff prctl interface). It just creates processes and threads. I see this hang almost instantly.

Quoted 5 lines

Josh, I did verify that this occurs on Joel's coresched tree both with and w/o the kprot patch and that should exactly correspond to these patches.

-chrish

I think I've gotten to the root of this. In the fork code, our cases for inheriting task_cookie are inverted for CLONE_THREAD vs !CLONE_THREAD. As a result, we are creating a new cookie per-thread, rather than inheriting from the parent. Now this is actually ok; I'm not observing a scalability problem with creating this many cookies. However, it means that overall throughput of your binary is cut in ~half, since none of the threads can share a core. Note that I never saw an indefinite deadlock, just ~2x runtime for your binary vs the control. I've verified that both a) manually hardcoding all threads to be able to share regardless of cookie, and b) using a machine with 6 cores instead of 2, both allow your binary to complete in the same amount of time as without the new API.

#26 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

Quoted 24 lines · Josh Don
Quoted 23 lines · Chris Hyser
Quoted 11 lines · Peter Zijlstra
Quoted 9 lines · Chris Hyser

On 1/22/21 8:17 PM, Joel Fernandes (Google) wrote:
While trying to test the new prctl() code I'm working on, I ran into a bug I chased back into this v10 code. Under a fair amount of stress, when the function __sched_core_update_cookie() is ultimately called from sched_core_fork(), the system deadlocks or otherwise non-visibly crashes. I've not had much success figuring out why/what. I'm running with LOCKDEP on and seeing no complaints. Duplicating it only requires setting a cookie on a task and forking a bunch of threads ... all of which then want to update their cookie.

Quoted 1 line

Can you share the code and reproducer?

Attached is a tarball with c code (source) and scripts. Just run ./setup_bug which will compile the source and start a bash with a cs cookie. Then run ./show_bug which dumps the cookie and then fires off some processes and threads. Note the cs_clone command is not doing any core sched prctls for this test (not needed and currently coded for a diff prctl interface). It just creates processes and threads. I see this hang almost instantly.

Josh, I did verify that this occurs on Joel's coresched tree both with and w/o the kprot patch and that should exactly correspond to these patches.

Quoted 8 lines
Quoted 2 lines

-chrish

I think I've gotten to the root of this. In the fork code, our cases for inheriting task_cookie are inverted for CLONE_THREAD vs !CLONE_THREAD. As a result, we are creating a new cookie per-thread, rather than inheriting from the parent. Now this is actually ok; I'm not observing a scalability problem with creating this many cookies.

This isn't the issue. The test code generates cases for both THREAD_CLONE and not and both paths call the cookie update code. The new code I was testing when I discovered this, fixed the problem you noted.

Quoted 6 lines

However, it means that overall throughput of your binary is cut in
~half, since none of the threads can share a core. Note that I never
saw an indefinite deadlock, just ~2x runtime for your binary vs th > control. I've verified that both a) manually hardcoding all threads to be able to share regardless of cookie, and b) using a machine with 6
cores instead of 2, both allow your binary to complete in the same
amount of time as without the new API.

This was on a 24 core box. When I run the test, I definitely hangs. I'll answer your other email as wwll.

-chrish

#27 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

Quoted 8 lines · Chris Hyser
Quoted 6 lines

However, it means that overall throughput of your binary is cut in
~half, since none of the threads can share a core. Note that I never
saw an indefinite deadlock, just ~2x runtime for your binary vs th > control. I've verified that both a) manually hardcoding all threads to be able to share regardless of cookie, and b) using a machine with 6
cores instead of 2, both allow your binary to complete in the same
amount of time as without the new API.

This was on a 24 core box. When I run the test, I definitely hangs. I'll answer your other email as wwll.

I just want to clarify. The test completes in secs normally. When I run this on the 24 core box from the console, other ssh connections immediately freeze. The console is frozen. You can't ping the box and it has stayed that way for up to 1/2 hour before I reset it. I'm trying to get some kind of stack trace to see what it is doing. To the extent that I've been able to trace it or print it, the "next code" always seems to be __sched_core_update_cookie(p);

-chrish

#28 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

Quoted 9 lines · chris hyser
Quoted 7 lines · Chris Hyser
Quoted 6 lines

However, it means that overall throughput of your binary is cut in
~half, since none of the threads can share a core. Note that I never
saw an indefinite deadlock, just ~2x runtime for your binary vs th > control. I've verified that both a) manually hardcoding all threads to be able to share regardless of cookie, and b) using a machine with 6
cores instead of 2, both allow your binary to complete in the same
amount of time as without the new API.

Quoted 4 lines
Quoted 1 line

This was on a 24 core box. When I run the test, I definitely hangs. I'll answer your other email as wwll.

I just want to clarify. The test completes in secs normally. When I run this on the 24 core box from the console, other ssh connections immediately freeze. The console is frozen. You can't ping the box and it has stayed that way for up to 1/2 hour before I reset it. I'm trying to get some kind of stack trace to see what it is doing. To the extent that I've been able to trace it or print it, the "next code" always seems to be __sched_core_update_cookie(p);

I cannot duplicate this on a 4 core box even with 1000's of processes and threads. The 24 core box does not even create the full 400 processes/threads in that test before it hangs and that test reliably fails almost instantly. The working theory is that the 24 core box is doing way more of the clone syscalls in parallel.

-chrish

#29 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

Quoted 10 lines · chris hyser
Quoted 9 lines · chris hyser
Quoted 7 lines · Chris Hyser
Quoted 6 lines

However, it means that overall throughput of your binary is cut in
~half, since none of the threads can share a core. Note that I never
saw an indefinite deadlock, just ~2x runtime for your binary vs th > control. I've verified that both a) manually hardcoding all threads to be able to share regardless of cookie, and b) using a machine with 6
cores instead of 2, both allow your binary to complete in the same
amount of time as without the new API.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

This was on a 24 core box. When I run the test, I definitely hangs. I'll answer your other email as wwll.

I just want to clarify. The test completes in secs normally. When I run this on the 24 core box from the console, other ssh connections immediately freeze. The console is frozen. You can't ping the box and it has stayed that way for up to 1/2 hour before I reset it. I'm trying to get some kind of stack trace to see what it is doing. To the extent that I've been able to trace it or print it, the "next code" always seems to be __sched_core_update_cookie(p);

I cannot duplicate this on a 4 core box even with 1000's of processes and threads. The 24 core box does not even create the full 400 processes/threads in that test before it hangs and that test reliably fails almost instantly. The working theory is that the 24 core box is doing way more of the clone syscalls in parallel.

Update:

The clone syscall stress test I have is causing a deadlock with this patchset when compiled with CONFIG_PROVE_RAW_LOCK_NESTING=y. I am able to get stacktraces with nmi_watchdog and am looking through those. Josh was not able to duplicate the deadlock, instead getting an actual warning about kmalloc w/GFP_ATOMIC while under a raw spinlock in the function __sched_core_update_cookie(). When I retry the test with a patch Josh sent, removing the kmalloc() and thus the trigger of the warning, no more deadlock. So some combination of CONFIGs, timing and function calls seems to have found something in this LOCK proving code.

-chrish

#30 lore raw

Re: [PATCH v10 2/5] sched: CGroup tagging interface for core scheduling

Quoted 12 lines · Chris Hyser

Update:

The clone syscall stress test I have is causing a deadlock with this patchset when compiled with CONFIG_PROVE_RAW_LOCK_NESTING=y. I am able to get stacktraces with nmi_watchdog and am looking through those. Josh was not able to duplicate the deadlock, instead getting an actual warning about kmalloc w/GFP_ATOMIC while under a raw spinlock in the function __sched_core_update_cookie(). When I retry the test with a patch Josh sent, removing the kmalloc() and thus the trigger of the warning, no more deadlock. So some combination of CONFIGs, timing and function calls seems to have found something in this LOCK proving code.

Quoted 1 line

-chrish

I have a patch to remove the dynamic allocation and overall simplify the logic here. Will squash that along with the rest of the modifications suggested by Peter in the next version.

#31 lore raw

PATCH v10 3/5 kselftest: Add tests for core-sched interface

In reply to Joel Fernandes (Google)

Add a kselftest test to ensure that the core-sched interface is working correctly.

---
 tools/testing/selftests/sched/.gitignore      |   1 +
 tools/testing/selftests/sched/Makefile        |  14 +
 tools/testing/selftests/sched/config          |   1 +
 .../testing/selftests/sched/test_coresched.c  | 716 ++++++++++++++++++
 4 files changed, 732 insertions(+)
 create mode 100644 tools/testing/selftests/sched/.gitignore
 create mode 100644 tools/testing/selftests/sched/Makefile
 create mode 100644 tools/testing/selftests/sched/config
 create mode 100644 tools/testing/selftests/sched/test_coresched.c
tools/testing/selftests/sched/.gitignore new file +1 -0
new file mode 100644
index 000000000000..4660929b0b9a
@@ -0,0 +1 @@
+test_coresched
tools/testing/selftests/sched/Makefile new file +14 -0
new file mode 100644
index 000000000000..e43b74fc5d7e
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0+
+
+ifneq ($(shell $(CC) --version 2>&1 | head -n 1 | grep clang),)
+CLANG_FLAGS += -no-integrated-as
+endif
+
+CFLAGS += -O2 -Wall -g -I./ -I../../../../usr/include/  -Wl,-rpath=./ \
+	  $(CLANG_FLAGS)
+LDLIBS += -lpthread
+
+TEST_GEN_FILES := test_coresched
+TEST_PROGS := test_coresched
+
+include ../lib.mk
tools/testing/selftests/sched/config new file +1 -0
new file mode 100644
index 000000000000..e8b09aa7c0c4
@@ -0,0 +1 @@
+CONFIG_SCHED_DEBUG=y
tools/testing/selftests/sched/test_coresched.c new file +716 -0

716 lines not on this page · raw message

Signature · 1 line
2.30.0.280.ga3ce27912f-goog
#32 lore raw

PATCH v10 4/5 Documentation: Add core scheduling documentation

In reply to Joel Fernandes (Google)

Document the usecases, design and interfaces for core scheduling.

---
 .../admin-guide/hw-vuln/core-scheduling.rst   | 263 ++++++++++++++++++
 Documentation/admin-guide/hw-vuln/index.rst   |   1 +
 2 files changed, 264 insertions(+)
 create mode 100644 Documentation/admin-guide/hw-vuln/core-scheduling.rst
Documentation/admin-guide/hw-vuln/core-scheduling.rst new file +263 -0
new file mode 100644
index 000000000000..a795747c706a
@@ -0,0 +1,263 @@
+Core Scheduling
+***************
+Core scheduling support allows userspace to define groups of tasks that can
+share a core. These groups can be specified either for security usecases (one
+group of tasks don't trust another), or for performance usecases (some
+workloads may benefit from running on the same core as they don't need the same
+hardware resources of the shared core).
+
+Security usecase
+----------------
+A cross-HT attack involves the attacker and victim running on different
+Hyper Threads of the same core. MDS and L1TF are examples of such attacks.
+Without core scheduling, the only full mitigation of cross-HT attacks is to
+disable Hyper Threading (HT). Core scheduling allows HT to be turned on safely
+by ensuring that trusted tasks can share a core. This increase in core sharing
+can improvement performance, however it is not guaranteed that performance will
+always improve, though that is seen to be the case with a number of real world
+workloads. In theory, core scheduling aims to perform at least as good as when
+Hyper Threading is disabled. In practice, this is mostly the case though not
+always: as synchronizing scheduling decisions across 2 or more CPUs in a core
+involves additional overhead - especially when the system is lightly loaded
+(``total_threads <= N/2``, where N is the total number of CPUs).
+
+Usage
+-----
+Core scheduling support is enabled via the ``CONFIG_SCHED_CORE`` config option.
+Using this feature, userspace defines groups of tasks that can be co-scheduled
+on the same core.
+The core scheduler uses this information to make sure that tasks that are not
+in the same group never run simultaneously on a core, while doing its best to
+satisfy the system's scheduling requirements.
+
+There are 2 ways to use core-scheduling:
+
+CGroup
+######
+Core scheduling adds additional files to the CPU controller CGroup:
+
+* ``cpu.core_tag``
+Writing ``1`` into this file results in all tasks in the group getting tagged.
+This results in all the CGroup's tasks allowed to run concurrently on a core's
+hyperthreads (also called siblings).
+
+The file being a value of ``0`` means the tag state of the CGroup is inherited
+from its parent hierarchy. If any ancestor of the CGroup is tagged, then the
+group is tagged.
+
+.. note:: Once a CGroup is tagged via cpu.core_tag, it is not possible to set this
+          for any descendant of the tagged group.
+
+.. note:: When a CGroup is not tagged, all the tasks within the group can share
+          a core with kernel threads and untagged system threads. For this reason,
+          if a group has ``cpu.core_tag`` of 0, it is considered to be trusted.
+
+prctl interface
+###############
+A ``prtcl(2)`` command ``PR_SCHED_CORE_SHARE`` provides an interface for the
+creation of and admission and removal of tasks from core scheduling groups.
+
+::
+
+    #include <sys/prctl.h>
+
+    int prctl(int option, unsigned long arg2, unsigned long arg3,
+            unsigned long arg4, unsigned long arg5);
+
+option:
+    ``PR_SCHED_CORE_SHARE``
+
+arg2:
+    - ``PR_SCHED_CORE_CLEAR            0  -- clear core_sched cookie of pid``
+    - ``PR_SCHED_CORE_SHARE_FROM       1  -- get core_sched cookie from pid``
+    - ``PR_SCHED_CORE_SHARE_TO         2  -- push core_sched cookie to pid``
+
+arg3:
+    ``tid`` of the task for which the operation applies
+
+arg4 and arg5:
+    MUST be equal to 0.
+
+Creation
+~~~~~~~~
+Creation is accomplished by sharing a ''cookie'' from a process not currently in
+a core scheduling group.
+
+::
+
+    if (prctl(PR_SCHED_CORE_SHARE, PR_SCHED_CORE_SHARE_FROM, src_tid, 0, 0) < 0)
+            handle_error("src_tid sched_core failed");
+
+Removal
+~~~~~~~
+Removing a task from a core scheduling group is done by:
+
+::
+
+    if (prctl(PR_SCHED_CORE_SHARE, PR_SCHED_CORE_SHARE_CLEAR, clr_tid, 0, 0) < 0)
+             handle_error("clr_tid sched_core failed");
+
+Cookie Transferal
+~~~~~~~~~~~~~~~~~
+Transferring a cookie between the current and other tasks is possible using
+PR_SCHED_CORE_SHARE_FROM and PR_SCHED_CORE_SHARE_TO to inherit a cookie from a
+specified task or a share a cookie with a task. In combination this allows a
+simple helper program to pull a cookie from a task in an existing core
+scheduling group and share it with already running tasks.
+
+::
+
+    if (prctl(PR_SCHED_CORE_SHARE, PR_SCHED_CORE_SHARE_FROM, from_tid, 0, 0) < 0)
+            handle_error("from_tid sched_core failed");
+
+    if (prctl(PR_SCHED_CORE_SHARE, PR_SCHED_CORE_SHARE_TO, to_tid, 0, 0) < 0)
+            handle_error("to_tid sched_core failed");
+
+
+.. note:: The core-sharing granted with ``prctl(2)`` will be subject to
+          core-sharing restrictions specified by the CGroup interface. For example,
+          if tasks T1 and T2 are a part of 2 different tagged CGroups, then they will
+          not share a core even if ``prctl(2)`` is called. This is analogous to how
+          affinities are set using the cpuset interface.
+
+It is important to note that, on a ``clone(2)`` syscall with ``CLONE_THREAD`` set,
+the child will be assigned the same ''cookie'' as its parent and thus in the
+same core scheduling group.  In the security usecase, a ``CLONE_THREAD`` child
+can access its parent's address space anyway (``CLONE_THREAD`` requires
+``CLONE_SIGHAND`` which requires ``CLONE_VM``), so there's no point in not
+allowing them to share a core. If a different behavior is desired, the child
+thread can call ``prctl(2)`` as needed.  This behavior is specific to the
+``prctl(2)`` interface. For the CGroup interface, the child of a fork always
+shares a core with its parent.  On the other hand, if a parent was previously
+tagged via ``prctl(2)`` and does a regular ``fork(2)`` syscall, the child will
+receive a unique tag.
+
+Design/Implementation
+---------------------
+Each task that is tagged is assigned a cookie internally in the kernel. As
+mentioned in `Usage`_, tasks with the same cookie value are assumed to trust
+each other and share a core.
+
+The basic idea is that, every schedule event tries to select tasks for all the
+siblings of a core such that all the selected tasks running on a core are
+trusted (same cookie) at any point in time. Kernel threads are assumed trusted.
+The idle task is considered special, as it trusts everything and everything
+trusts it.
+
+During a schedule() event on any sibling of a core, the highest priority task on
+the sibling's core is picked and assigned to the sibling calling schedule(), if
+the sibling has the task enqueued. For rest of the siblings in the core,
+highest priority task with the same cookie is selected if there is one runnable
+in their individual run queues. If a task with same cookie is not available,
+the idle task is selected.  Idle task is globally trusted.
+
+Once a task has been selected for all the siblings in the core, an IPI is sent to
+siblings for whom a new task was selected. Siblings on receiving the IPI will
+switch to the new task immediately. If an idle task is selected for a sibling,
+then the sibling is considered to be in a `forced idle` state. I.e., it may
+have tasks on its on runqueue to run, however it will still have to run idle.
+More on this in the next section.
+
+Forced-idling of tasks
+----------------------
+The scheduler tries its best to find tasks that trust each other such that all
+tasks selected to be scheduled are of the highest priority in a core.  However,
+it is possible that some runqueues had tasks that were incompatible with the
+highest priority ones in the core. Favoring security over fairness, one or more
+siblings could be forced to select a lower priority task if the highest
+priority task is not trusted with respect to the core wide highest priority
+task.  If a sibling does not have a trusted task to run, it will be forced idle
+by the scheduler (idle thread is scheduled to run).
+
+When the highest priority task is selected to run, a reschedule-IPI is sent to
+the sibling to force it into idle. This results in 4 cases which need to be
+considered depending on whether a VM or a regular usermode process was running
+on either HT::
+
+          HT1 (attack)            HT2 (victim)
+   A      idle -> user space      user space -> idle
+   B      idle -> user space      guest -> idle
+   C      idle -> guest           user space -> idle
+   D      idle -> guest           guest -> idle
+
+Note that for better performance, we do not wait for the destination CPU
+(victim) to enter idle mode. This is because the sending of the IPI would bring
+the destination CPU immediately into kernel mode from user space, or VMEXIT
+in the case of guests. At best, this would only leak some scheduler metadata
+which may not be worth protecting. It is also possible that the IPI is received
+too late on some architectures, but this has not been observed in the case of
+x86.
+
+Trust model
+-----------
+Core scheduling maintains trust relationships amongst groups of tasks by
+assigning the tag of them with the same cookie value.
+When a system with core scheduling boots, all tasks are considered to trust
+each other. This is because the core scheduler does not have information about
+trust relationships until userspace uses the above mentioned interfaces, to
+communicate them. In other words, all tasks have a default cookie value of 0.
+and are considered system-wide trusted. The stunning of siblings running
+cookie-0 tasks is also avoided.
+
+Once userspace uses the above mentioned interfaces to group sets of tasks, tasks
+within such groups are considered to trust each other, but do not trust those
+outside. Tasks outside the group also don't trust tasks within.
+
+Limitations in core-scheduling
+------------------------------
+Core scheduling tries to guarantee that only trusted tasks run concurrently on a
+core. But there could be small window of time during which untrusted tasks run
+concurrently or kernel could be running concurrently with a task not trusted by
+kernel.
+
+1. IPI processing delays
+########################
+Core scheduling selects only trusted tasks to run together. IPI is used to notify
+the siblings to switch to the new task. But there could be hardware delays in
+receiving of the IPI on some arch (on x86, this has not been observed). This may
+cause an attacker task to start running on a CPU before its siblings receive the
+IPI. Even though cache is flushed on entry to user mode, victim tasks on siblings
+may populate data in the cache and micro architectural buffers after the attacker
+starts to run and this is a possibility for data leak.
+
+Open cross-HT issues that core scheduling does not solve
+--------------------------------------------------------
+1. For MDS
+##########
+Core scheduling cannot protect against MDS attacks between an HT running in
+user mode and another running in kernel mode. Even though both HTs run tasks
+which trust each other, kernel memory is still considered untrusted. Such
+attacks are possible for any combination of sibling CPU modes (host or guest mode).
+
+2. For L1TF
+###########
+Core scheduling cannot protect against an L1TF guest attacker exploiting a
+guest or host victim. This is because the guest attacker can craft invalid
+PTEs which are not inverted due to a vulnerable guest kernel. The only
+solution is to disable EPT (Extended Page Tables).
+
+For both MDS and L1TF, if the guest vCPU is configured to not trust each
+other (by tagging separately), then the guest to guest attacks would go away.
+Or it could be a system admin policy which considers guest to guest attacks as
+a guest problem.
+
+Another approach to resolve these would be to make every untrusted task on the
+system to not trust every other untrusted task. While this could reduce
+parallelism of the untrusted tasks, it would still solve the above issues while
+allowing system processes (trusted tasks) to share a core.
+
+3. Protecting the kernel (IRQ, syscall, VMEXIT)
+###############################################
+This section is a work in progress. The main point here is entry into the
+kernel is not protected from attackers on a sibling.
+
+Use cases
+---------
+The main use case for Core scheduling is mitigating the cross-HT vulnerabilities
+with SMT enabled. There are other use cases where this feature could be used:
+
+- Isolating tasks that needs a whole core: Examples include realtime tasks, tasks
+  that uses SIMD instructions etc.
+- Gang scheduling: Requirements for a group of tasks that needs to be scheduled
+  together could also be realized using core scheduling. One example is vCPUs of
+  a VM.
Documentation/admin-guide/hw-vuln/index.rst +1 -0
index 21710f8609fe..361ccbbd9e54 100644
@@ -16,3 +16,4 @@ are configurable at compile, boot or run time.
    multihit.rst
    special-register-buffer-data-sampling.rst
    l1d_flush.rst
+   core-scheduling.rst
Signature · 1 line
2.30.0.280.ga3ce27912f-goog
#33 lore raw

PATCH v10 5/5 sched: Debug bits...

In reply to Joel Fernandes (Google)
---
 kernel/sched/core.c | 35 ++++++++++++++++++++++++++++++++++-
 kernel/sched/fair.c |  9 +++++++++
 2 files changed, 43 insertions(+), 1 deletion(-)
kernel/sched/core.c +34 -1
index a3844e2e7379..56ba2ca4f922 100644
@@ -106,6 +106,10 @@ static inline bool prio_less(struct task_struct *a, struct task_struct *b, bool
 
 	int pa = __task_prio(a), pb = __task_prio(b);
 
+	trace_printk("(%s/%d;%d,%Lu,%Lu) ?< (%s/%d;%d,%Lu,%Lu)\n",
+		     a->comm, a->pid, pa, a->se.vruntime, a->dl.deadline,
+		     b->comm, b->pid, pb, b->se.vruntime, b->dl.deadline);
+
 	if (-pa < -pb)
 		return true;
 
@@ -296,12 +300,16 @@ static void __sched_core_enable(void)
 
 	static_branch_enable(&__sched_core_enabled);
 	stop_machine(__sched_core_stopper, (void *)true, NULL);
+
+	printk("core sched enabled\n");
 }
 
 static void __sched_core_disable(void)
 {
 	stop_machine(__sched_core_stopper, (void *)false, NULL);
 	static_branch_disable(&__sched_core_enabled);
+
+	printk("core sched disabled\n");
 }
 
 void sched_core_get(void)
@@ -5237,6 +5245,13 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
 			set_next_task(rq, next);
 		}
 
+		trace_printk("pick pre selected (%u %u %u): %s/%d %lx\n",
+			     rq->core->core_task_seq,
+			     rq->core->core_pick_seq,
+			     rq->core_sched_seq,
+			     next->comm, next->pid,
+			     next->core_cookie);
+
 		rq->core_pick = NULL;
 		return next;
 	}
@@ -5331,6 +5346,9 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
 					rq->core->core_forceidle_seq++;
 			}
 
+			trace_printk("cpu(%d): selected: %s/%d %lx\n",
+				     i, p->comm, p->pid, p->core_cookie);
+
 			/*
 			 * If this new candidate is of higher priority than the
 			 * previous; and they're incompatible; we need to wipe
@@ -5347,6 +5365,8 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
 				rq->core->core_cookie = p->core_cookie;
 				max = p;
 
+				trace_printk("max: %s/%d %lx\n", max->comm, max->pid, max->core_cookie);
+
 				if (old_max) {
 					rq->core->core_forceidle = false;
 					for_each_cpu(j, smt_mask) {
@@ -5368,6 +5388,7 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
 
 	/* Something should have been selected for current CPU */
 	WARN_ON_ONCE(!next);
+	trace_printk("picked: %s/%d %lx\n", next->comm, next->pid, next->core_cookie);
 
 	/*
 	 * Reschedule siblings
@@ -5409,13 +5430,21 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
 		}
 
 		/* Did we break L1TF mitigation requirements? */
-		WARN_ON_ONCE(!cookie_match(next, rq_i->core_pick));
+		if (unlikely(!cookie_match(next, rq_i->core_pick))) {
+			trace_printk("[%d]: cookie mismatch. %s/%d/0x%lx/0x%lx\n",
+				     rq_i->cpu, rq_i->core_pick->comm,
+				     rq_i->core_pick->pid,
+				     rq_i->core_pick->core_cookie,
+				     rq_i->core->core_cookie);
+			WARN_ON_ONCE(1);
+		}
 
 		if (rq_i->curr == rq_i->core_pick) {
 			rq_i->core_pick = NULL;
 			continue;
 		}
 
+		trace_printk("IPI(%d)\n", i);
 		resched_curr(rq_i);
 	}
 
@@ -5455,6 +5484,10 @@ static bool try_steal_cookie(int this, int that)
 		if (p->core_occupation > dst->idle->core_occupation)
 			goto next;
 
+		trace_printk("core fill: %s/%d (%d->%d) %d %d %lx\n",
+			     p->comm, p->pid, that, this,
+			     p->core_occupation, dst->idle->core_occupation, cookie);
+
 		p->on_rq = TASK_ON_RQ_MIGRATING;
 		deactivate_task(src, p, 0);
 		set_task_cpu(p, this);
kernel/sched/fair.c +9 -0
index fddd7c44bbf3..ebeeebc4223a 100644
@@ -10769,6 +10769,15 @@ static void se_fi_update(struct sched_entity *se, unsigned int fi_seq, bool forc
 			cfs_rq->forceidle_seq = fi_seq;
 		}
 
+
+		if (root) {
+			old = cfs_rq->min_vruntime_fi;
+			new = cfs_rq->min_vruntime;
+			root = false;
+			trace_printk("cfs_rq(min_vruntime_fi) %lu->%lu\n",
+				     old, new);
+		}
+
 		cfs_rq->min_vruntime_fi = cfs_rq->min_vruntime;
 	}
 }
Signature · 1 line
2.30.0.280.ga3ce27912f-goog