Core sched remaining patches rebased
Subsystems (7)
cover Core sched remaining patches rebased
Core-Scheduling (resend based on peter's queue.git sched/core-sched branch).
===============
Enclosed is interface related core scheduling patches and one for migration. The main core scheduling patches were already pulled in by Peter with these bits left.
Main changes are the simplification of the core cookie scheme, new prctl code, and other misc fixes based on Peter's feedback.
These remaining patches was worked on mostly by Josh Don and Chris Hyser.
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).
Julien: TPCC tests showed improvements with core-scheduling as below. With kernel protection enabled, it does not show any regression. Possibly ASI will improve the performance for those who choose kernel protection (can be controlled through ht_protect kernel command line option).
average stdev diff
baseline (SMT on) 1197.272 44.78312824
core sched ( kernel protect) 412.9895 45.42734343 -65.51%
core sched (no kernel protect) 686.6515 71.77756931 -42.65%
nosmt 408.667 39.39042872 -65.87%
(Note these results are from v8).
Vineeth tested sysbench 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.
Other changes:
- Fixed breaking of coresched= option patch on !SCHED_CORE builds.
- Trivial commit message changes.
Changes in v10
==============
- migration code changes from Aubrey.
- dropped patches merged.
- interface changes from Josh and Chris.
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...
Josh Don (2):
sched: core scheduling tagging infrastructure
sched: cgroup cookie API for core scheduling
chris hyser (2):
sched: prctl() cookie manipulation for core scheduling
kselftest: Add test for core sched prctl interface
.../admin-guide/hw-vuln/core-scheduling.rst | 460 ++++++++++ Documentation/admin-guide/hw-vuln/index.rst | 1 + fs/exec.c | 4 +- include/linux/sched.h | 35 +- include/linux/sched/task.h | 4 +- include/uapi/linux/prctl.h | 7 + kernel/fork.c | 1 + kernel/sched/Makefile | 1 + kernel/sched/core.c | 212 ++++- kernel/sched/coretag.c | 587 +++++++++++++ kernel/sched/debug.c | 4 + kernel/sched/fair.c | 41 +- kernel/sched/sched.h | 151 +++- kernel/sys.c | 7 + tools/include/uapi/linux/prctl.h | 7 + tools/testing/selftests/sched/.gitignore | 2 + tools/testing/selftests/sched/Makefile | 14 + tools/testing/selftests/sched/config | 1 + tools/testing/selftests/sched/cs_prctl_test.c | 370 ++++++++ .../testing/selftests/sched/test_coresched.c | 812 ++++++++++++++++++ 20 files changed, 2659 insertions(+), 62 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/cs_prctl_test.c create mode 100644 tools/testing/selftests/sched/test_coresched.c
--
2.31.0.291.g576ba9dcdaf-goog
PATCH RESEND 1/8 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 may 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
--- kernel/sched/fair.c | 29 ++++++++++++++---- kernel/sched/sched.h | 73 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 6 deletions(-)
kernel/sched/fair.c +23 -6
@@ -5877,11 +5877,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) {/*
@@ -5967,9 +5971,10 @@ static inline int find_idlest_cpu(struct sched_domain *sd, struct task_struct *preturn new_cpu; } -static inline int __select_idle_cpu (int cpu) +static inline int __select_idle_cpu (int cpu,struct task_struct *p) { -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))return cpu;return -1 ;
@@ -6041,7 +6046,7 @@ static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpuint cpu;if (!static_branch_likely (&sched_smt_present)) -return __select_idle_cpu (core); +return __select_idle_cpu (core, p);for_each_cpu (cpu,cpu_smt_mask (core)) {if (!available_idle_cpu (cpu)) {
@@ -6079,7 +6084,7 @@ static inline bool test_idle_cores(int cpu, bool def)static inline int select_idle_core (struct task_struct *p,int core,struct cpumask *cpus,int *idle_cpu) { -return __select_idle_cpu (core); +return __select_idle_cpu (core, p); }#endif /* CONFIG_SCHED_SMT */
@@ -6132,7 +6137,7 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int t }else {if (!--nr)return -1 ; - idle_cpu =__select_idle_cpu (cpu); + idle_cpu =__select_idle_cpu (cpu, p);if ((unsigned int )idle_cpu < nr_cpumask_bits)break ; }
@@ -7473,6 +7478,14 @@ static int task_hot(struct task_struct *p, struct lb_env *env)if (sysctl_sched_migration_cost ==-1 )return 1 ; + +/* +* 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 1 ; +if (sysctl_sched_migration_cost ==0 )return 0 ;
@@ -8834,6 +8847,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 +73 -0
93 lines not on this page · raw message
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
PATCH RESEND 2/8 sched: core scheduling tagging infrastructure
In reply to Joel Fernandes (Google)From: Josh Don <[email protected]>
A single unsigned long is insufficient as a cookie value for core scheduling. We will minimally have cookie values for a per-task and a per-group interface, which must be combined into an overall cookie.
This patch adds the infrastructure necessary for setting task and group cookie. Namely, it reworks the core_cookie into a struct, and provides interfaces for setting task and group cookie, as well as other operations (i.e. compare()). Subsequent patches will use these hooks to provide an API for setting these cookies.
One important property of this interface is that neither the per-task nor the per-cgroup setting overrides the other. For example, if two tasks are in different cgroups, and one or both of the cgroups is tagged using the per-cgroup interface, then these tasks cannot share, even if they use the per-task interface to attempt to share with one another.
Core scheduler has extra overhead. Enable it only for machines with more than one SMT hardware thread.
--- include/linux/sched.h | 24 +++- kernel/fork.c | 1 + kernel/sched/Makefile | 1 + kernel/sched/core.c | 100 ++++++++++------- kernel/sched/coretag.c | 245 +++++++++++++++++++++++++++++++++++++++++ kernel/sched/debug.c | 4 + kernel/sched/sched.h | 57 ++++++++-- 7 files changed, 384 insertions(+), 48 deletions(-) create mode 100644 kernel/sched/coretag.c
include/linux/sched.h +23 -1
@@ -645,6 +645,22 @@ struct kmap_ctrl {#endif }; +#ifdef CONFIG_SCHED_CORE +struct sched_core_cookie { +unsigned long task_cookie; +#ifdef CONFIG_CGROUP_SCHED +unsigned long group_cookie; +#endif + +/* +* A u64 representation of the cookie used only for display to +* userspace. We avoid exposing the actual cookie contents, which +* are kernel pointers. +*/ +u64 userspace_id; +}; +#endif +struct task_struct {#ifdef CONFIG_THREAD_INFO_IN_TASK /*
@@ -703,7 +719,7 @@ struct task_struct {#ifdef CONFIG_SCHED_CORE struct rb_node core_node; -unsigned long core_cookie; +struct sched_core_cookie core_cookie;unsigned int core_occupation;#endif
@@ -2166,4 +2182,10 @@ int sched_trace_rq_nr_running(struct rq *rq);const struct cpumask *sched_trace_rd_span (struct root_domain *rd); +#ifdef CONFIG_SCHED_CORE +void sched_tsk_free (struct task_struct *tsk); +#else +#define sched_tsk_free (tsk) do { } while (0) +#endif +#endif
kernel/fork.c +1 -0
@@ -737,6 +737,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
@@ -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 +62 -38
260 lines not on this page · raw message
kernel/sched/coretag.c new file +245 -0
245 lines not on this page · raw message
kernel/sched/debug.c +4 -0
@@ -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.userspace_id); +#endif +sched_show_numa (p, m); }
kernel/sched/sched.h +48 -9
101 lines not on this page · raw message
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
Peter Zijlstra
maintainerRe: [PATCH resend 2/8] sched: core scheduling tagging infrastructure
Quoted 6 lines · Joel Fernandes (Google)
From: Josh Don <[email protected]>
A single unsigned long is insufficient as a cookie value for core scheduling. We will minimally have cookie values for a per-task and a per-group interface, which must be combined into an overall cookie.
Quoted 6 lines
This patch adds the infrastructure necessary for setting task and group cookie. Namely, it reworks the
core_cookieinto a struct, and provides interfaces for setting task and group cookie, as well as other operations (i.e.compare()). Subsequent patches will use these hooks to provide an API for setting these cookies.
*urgh*... so I specifically wanted the task interface first to avoid / get-rid of all this madness. And then you keep it :-(
I've spend the past few hours rewriting patches #2 and #3, and adapting #4. The thing was working before I added SHARE_FROM back and introduced GET, but now I'm seeing a few FAILs from the selftest.
I'm too tired to make sense of anything much, or even focus my eyes consistently, so I'll have to prod at it some more next week, but I've pushed out the lot to my queue.git:
https://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git/log/?h=sched/core-sched
Also, we really need a better name than coretag.c.
Re: [PATCH resend 2/8] sched: core scheduling tagging infrastructure
Hi Peter,
Quoted 15 lines · Peter Zijlstra
Quoted 6 lines · Joel Fernandes (Google)
From: Josh Don <[email protected]>
A single unsigned long is insufficient as a cookie value for core scheduling. We will minimally have cookie values for a per-task and a per-group interface, which must be combined into an overall cookie.
Quoted 6 lines
This patch adds the infrastructure necessary for setting task and group cookie. Namely, it reworks the
core_cookieinto a struct, and provides interfaces for setting task and group cookie, as well as other operations (i.e.compare()). Subsequent patches will use these hooks to provide an API for setting these cookies.
Quoted 2 lines
*urgh*... so I specifically wanted the task interface first to avoid / get-rid of all this madness. And then you keep it :-(
Sorry, I misunderstood the ask here :/ I had separated out the cgroup interface parts of the patch, leaving (mostly) the parts which introduced a compound cookie structure. I see now that you just wanted the plain task interface to start, with no notion of group cookie.
Quoted 9 lines
I've spend the past few hours rewriting patches #2 and #3, and adapting #4. The thing was working before I added
SHARE_FROMback and introduced GET, but now I'm seeing a few FAILs from the selftest.I'm too tired to make sense of anything much, or even focus my eyes consistently, so I'll have to prod at it some more next week, but I've pushed out the lot to my queue.git:
https://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git/log/?h=sched/core-sched
Thanks, I'll take a look next week.
Quoted 1 line
Also, we really need a better name than
coretag.c.
Yea, we don't really otherwise use the phrase "tagging". core_sched.c is probably too confusing given we have sched/core.c.
Peter Zijlstra
maintainerRe: [PATCH resend 2/8] sched: core scheduling tagging infrastructure
Quoted 6 lines · Josh Don
Quoted 4 lines · Peter Zijlstra
I've spend the past few hours rewriting patches #2 and #3, and adapting #4. The thing was working before I added
SHARE_FROMback and introduced GET, but now I'm seeing a few FAILs from the selftest.
Quoted 7 lines
Quoted 5 lines
I'm too tired to make sense of anything much, or even focus my eyes consistently, so I'll have to prod at it some more next week, but I've pushed out the lot to my queue.git:
https://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git/log/?h=sched/core-sched
Thanks, I'll take a look next week.
OK, fixed the fails. My tired head made it unconditionally return the cookie-id of 'current' instead of task. Pushed out an update.
Quoted 4 lines
Quoted 1 line
Also, we really need a better name than
coretag.c.Yea, we don't really otherwise use the phrase "tagging".
core_sched.cis probably too confusing given we havesched/core.c.
Right, so I tried core_sched and my fingers already hate it as much as kernel/scftorture.c (which I'd assumed my fingers would get used to eventually, but noooo).
Looking at kernel/sched/ C is very overrepresented, so we really don't want another I think. B, E, G, H, J, K, N, seem to still be available in the first half of the alphabet. Maybe, bonghits.c, gabbleduck.c ?
Re: [PATCH resend 2/8] sched: core scheduling tagging infrastructure
Quoted 3 lines · Peter Zijlstra
OK, fixed the fails. My tired head made it unconditionally return the cookie-id of 'current' instead of task. Pushed out an update.
I see you have the per-task and prctl stuff pulled into your tree. I can rebase the compound cookie and cgroup api patches on top if you'd like; not sure if you've already re-ordered it locally. Any other comments on the former?
Quoted 5 lines
Quoted 4 lines
Quoted 1 line
Also, we really need a better name than
coretag.c.Yea, we don't really otherwise use the phrase "tagging".
core_sched.cis probably too confusing given we havesched/core.c.
Quoted 7 lines
Right, so I tried
core_schedand my fingers already hate it as much askernel/scftorture.c(which I'd assumed my fingers would get used to eventually, but noooo).Looking at
kernel/sched/C is very overrepresented, so we really don't want another I think. B, E, G, H, J, K, N, seem to still be available in the first half of the alphabet. Maybe,bonghits.c,gabbleduck.c?
hardware_vuln.c? Tricky to avoid a C with cpu, core, and cookie :)
Peter Zijlstra
maintainerRe: [PATCH resend 2/8] sched: core scheduling tagging infrastructure
Quoted 9 lines · Josh Don
Quoted 3 lines · Peter Zijlstra
OK, fixed the fails. My tired head made it unconditionally return the cookie-id of 'current' instead of task. Pushed out an update.
I see you have the per-task and prctl stuff pulled into your tree. I can rebase the compound cookie and cgroup api patches on top if you'd like; not sure if you've already re-ordered it locally. Any other comments on the former?
Hold off on that for a little while; i've been grubbing through the cgroup code as well, just haven't had anything that actually works yet. I'll hopefully have something soon (I really want to quickly forget all the cgroup details again).
Peter Zijlstra
maintainerRe: [PATCH resend 2/8] sched: core scheduling tagging infrastructure
Quoted 11 lines · Peter Zijlstra
Quoted 9 lines · Josh Don
Quoted 3 lines · Peter Zijlstra
OK, fixed the fails. My tired head made it unconditionally return the cookie-id of 'current' instead of task. Pushed out an update.
I see you have the per-task and prctl stuff pulled into your tree. I can rebase the compound cookie and cgroup api patches on top if you'd like; not sure if you've already re-ordered it locally. Any other comments on the former?
Quoted 4 lines
Hold off on that for a little while; i've been grubbing through the cgroup code as well, just haven't had anything that actually works yet. I'll hopefully have something soon (I really want to quickly forget all the cgroup details again).
With a significantly trimmed Cc list:
PATCH RESEND 3/8 sched: prctl() cookie manipulation for core scheduling
In reply to Joel Fernandes (Google)From: chris hyser <[email protected]>
This patch provides support for setting, clearing and copying core scheduling 'task cookies' between threads (PID), processes (TGID), and process groups (PGID).
The value of core scheduling isn't that tasks don't share a core, 'nosmt' can do that. The value lies in exploiting all the sharing opportunities that exist to recover possible lost performance and that requires a degree of flexibility in the API. From a security perspective (and there are others), the thread, process and process group distinction is an existent hierarchal categorization of tasks that reflects many of the security concerns about 'data sharing'. For example, protecting against cache-snooping by a thread that can just read the memory directly isn't all that useful. With this in mind, subcommands to CLEAR/CREATE/SHARE (TO/FROM) provide a mechanism to create, clear and share cookies. CLEAR/CREATE/SHARE_TO specify a target pid with enum pidtype used to specify the scope of the targeted tasks. For example, PIDTYPE_TGID will share the cookie with the process and all of it's threads as typically desired in a security scenario.
API:
prctl(PR_SCHED_CORE_SHARE, PR_SCHED_CORE_CREATE, tgtpid, pidtype, 0) prctl(PR_SCHED_CORE_SHARE, PR_SCHED_CORE_CLEAR, tgtpid, pidtype, 0) prctl(PR_SCHED_CORE_SHARE, PR_SCHED_CORE_SHARE_FROM, srcpid, 0, 0) prctl(PR_SCHED_CORE_SHARE, PR_SCHED_CORE_SHARE_TO, tgtpid, pidtype, 0)
where 'tgtpid/srcpid == 0' implies the current process and pidtype is kernel enum pid_type {PIDTYPE_PID, PIDTYPE_TGID, PIDTYPE_PGID, ...}. PIDTYPE_SID, sharing a cookie with an entire session, was considered less useful given the choice to create a new cookie on task exec().
For return values, EINVAL, ENOMEM are what they say. ESRCH means the tgtpid/srcpid was not found. EPERM indicates lack of PTRACE permission access to tgtpid/srcpid. EACCES indicates that a task in the target pidtype group was not updated due to permission.
In terms of interaction with the cgroup interface, task cookies are set independently of cgroup core scheduling cookies and thus would allow use for tasks within a container using cgroup cookies.
Current hard-coded policies are:
- a user can clear the cookie of any process they can set a cookie for. Lack of a cookie *might* be a security issue if cookies are being used for that.
- on fork of a parent with a cookie, both process and thread child tasks get a copy.
- on exec a task with a cookie is given a new cookie
--- fs/exec.c | 4 +- include/linux/sched.h | 11 ++ include/linux/sched/task.h | 4 +- include/uapi/linux/prctl.h | 7 ++ kernel/sched/core.c | 11 +- kernel/sched/coretag.c | 196 ++++++++++++++++++++++++++++++- kernel/sched/sched.h | 2 + kernel/sys.c | 7 ++ tools/include/uapi/linux/prctl.h | 7 ++ 9 files changed, 241 insertions(+), 8 deletions(-)
fs/exec.c +3 -1
@@ -1807,7 +1807,9 @@ static int bprm_execve(struct linux_binprm *bprm,if (IS_ERR (file)) goto out_unmark; -sched_exec (); + retval =sched_exec (); +if (retval) + goto out; bprm->file = file;/*
include/linux/sched.h +11 -0
@@ -2184,8 +2184,19 @@ const struct cpumask *sched_trace_rd_span(struct root_domain *rd);#ifdef CONFIG_SCHED_CORE void sched_tsk_free (struct task_struct *tsk); +int sched_core_share_pid (unsigned long flags,pid_t pid,enum pid_type type); +int sched_core_exec (void );#else #define sched_tsk_free (tsk) do { } while (0) +static inline int sched_core_share_pid (unsigned long flags,pid_t pid,enum pid_type type) +{ +return 0 ; +} + +static inline int sched_core_exec (void ) +{ +return 0 ; +}#endif #endif
include/linux/sched/task.h +2 -2
@@ -94,9 +94,9 @@ extern void free_task(struct task_struct *tsk);/* sched_exec is called by processes performing an exec */ #ifdef CONFIG_SMP -extern void sched_exec (void ); +int sched_exec (void );#else -#define sched_exec () {} +static inline int sched_exec (void ) {return 0 ; }#endif static inline struct task_struct *get_task_struct (struct task_struct *t)
include/uapi/linux/prctl.h +7 -0
@@ -255,4 +255,11 @@ struct prctl_mm_map {# define SYSCALL_DISPATCH_FILTER_ALLOW 0# define SYSCALL_DISPATCH_FILTER_BLOCK 1 +/* Request the scheduler to share a core */ +#define PR_SCHED_CORE_SHARE 60 +# define PR_SCHED_CORE_CLEAR 0/* clear core_sched cookie of pid */ +# define PR_SCHED_CORE_CREATE 1/* create unique core_sched cookie */ +# define PR_SCHED_CORE_SHARE_FROM 2/* get core_sched cookie from pid */ +# define PR_SCHED_CORE_SHARE_TO 3/* push core_sched cookie to pid */ +#endif /* _LINUX_PRCTL_H */
kernel/sched/core.c +9 -2
@@ -4752,11 +4752,17 @@ unsigned long nr_iowait(void)* sched_exec - execve() is a valuable balancing opportunity, because at * this point the task has the smallest effective memory and cache footprint. */ -void sched_exec (void ) +int sched_exec (void ) {struct task_struct *p = current;unsigned long flags;int dest_cpu; +int ret; + +/* this may change what tasks current can share a core with */ + ret =sched_core_exec (); +if (ret) +return ret;raw_spin_lock_irqsave (&p->pi_lock, flags); dest_cpu = p->sched_class->select_task_rq (p,task_cpu (p),WF_EXEC );
@@ -4768,10 +4774,11 @@ void sched_exec(void)raw_spin_unlock_irqrestore (&p->pi_lock, flags);stop_one_cpu (task_cpu (p), migration_cpu_stop, &arg); - return; + return 0; } unlock:raw_spin_unlock_irqrestore (&p->pi_lock, flags); +return 0 ; }#endif
kernel/sched/coretag.c +193 -3
215 lines not on this page · raw message
kernel/sched/sched.h +2 -0
@@ -1184,6 +1184,8 @@ void sched_core_dequeue(struct rq *rq, struct task_struct *p);void sched_core_get (void );void sched_core_put (void ); +int sched_core_share_pid (unsigned long flags,pid_t pid,enum pid_type type); +bool cfs_prio_less (struct task_struct *a,struct task_struct *b,bool fi); int sched_core_cookie_cmp(const struct sched_core_cookie *a,
kernel/sys.c +7 -0
@@ -2534,6 +2534,13 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, error =set_syscall_user_dispatch (arg2, arg3, arg4, (char __user *) arg5);break ; +#ifdef CONFIG_SCHED_CORE +case PR_SCHED_CORE_SHARE : +if (arg5) +return -EINVAL ; + error =sched_core_share_pid (arg2, arg3, arg4); +break ; +#endif default : error = -EINVAL ;break ;
tools/include/uapi/linux/prctl.h +7 -0
@@ -255,4 +255,11 @@ struct prctl_mm_map {# define SYSCALL_DISPATCH_FILTER_ALLOW 0# define SYSCALL_DISPATCH_FILTER_BLOCK 1 +/* Request the scheduler to share a core */ +#define PR_SCHED_CORE_SHARE 60 +# define PR_SCHED_CORE_CLEAR 0/* clear core_sched cookie of pid */ +# define PR_SCHED_CORE_CREATE 1/* get core_sched cookie from pid */ +# define PR_SCHED_CORE_SHARE_FROM 2/* get core_sched cookie from pid */ +# define PR_SCHED_CORE_SHARE_TO 3/* push core_sched cookie to pid */ +#endif /* _LINUX_PRCTL_H */
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
PATCH RESEND 4/8 kselftest: Add test for core sched prctl interface
In reply to Joel Fernandes (Google)From: chris hyser <[email protected]>
Provides a selftest and examples of using the interface.
--- tools/testing/selftests/sched/.gitignore | 1 + tools/testing/selftests/sched/Makefile | 14 + tools/testing/selftests/sched/config | 1 + tools/testing/selftests/sched/cs_prctl_test.c | 370 ++++++++++++++++++ 4 files changed, 386 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/cs_prctl_test.c
tools/testing/selftests/sched/.gitignore new file +1 -0
@@ -0,0 +1 @@ +cs_prctl_test
tools/testing/selftests/sched/Makefile new file +14 -0
@@ -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 := cs_prctl_test +TEST_PROGS := cs_prctl_test + +include ../lib.mk
tools/testing/selftests/sched/config new file +1 -0
@@ -0,0 +1 @@ +CONFIG_SCHED_DEBUG=y
tools/testing/selftests/sched/cs_prctl_test.c new file +370 -0
370 lines not on this page · raw message
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
PATCH RESEND 5/8 sched: cgroup cookie API for core scheduling
In reply to Joel Fernandes (Google)From: Josh Don <[email protected]>
This adds the API to set/get the cookie for a given cgroup. This interface lives at cgroup/cpu.core_tag.
The cgroup interface can be used to toggle a unique cookie value for all descendent tasks, preventing these tasks from sharing with any others. See Documentation/admin-guide/hw-vuln/core-scheduling.rst for a full rundown of both this and the per-task API.
--- kernel/sched/core.c | 61 ++++++++++++++-- kernel/sched/coretag.c | 156 ++++++++++++++++++++++++++++++++++++++++- kernel/sched/sched.h | 25 +++++++ 3 files changed, 235 insertions(+), 7 deletions(-)
kernel/sched/core.c +56 -5
@@ -9328,6 +9328,8 @@ struct task_group *sched_create_group(struct task_group *parent)alloc_uclamp_sched_group (tg, parent); +alloc_sched_core_sched_group (tg); +return tg; err:
@@ -9391,6 +9393,11 @@ static void sched_change_group(struct task_struct *tsk, int type) tg =container_of (task_css_check (tsk, cpu_cgrp_id, true), struct task_group, css); tg =autogroup_task_group (tsk, tg); + +#ifdef CONFIG_SCHED_CORE +sched_core_change_group (tsk, tg); +#endif + tsk->sched_task_group = tg;#ifdef CONFIG_FAIR_GROUP_SCHED
@@ -9443,11 +9450,6 @@ void sched_move_task(struct task_struct *tsk)task_rq_unlock (rq, tsk, &rf); } -static inline struct task_group *css_tg (struct cgroup_subsys_state *css) -{ - return css ? container_of(css, struct task_group, css) : NULL; -} -static struct cgroup_subsys_state *cpu_cgroup_css_alloc (struct cgroup_subsys_state *parent_css) {
@@ -9483,6 +9485,18 @@ static int cpu_cgroup_css_online(struct cgroup_subsys_state *css)return 0 ; } +static void cpu_cgroup_css_offline (struct cgroup_subsys_state *css) +{ +#ifdef CONFIG_SCHED_CORE +struct task_group *tg =css_tg (css); + +if (tg->core_tagged) { +sched_core_put (); + tg->core_tagged =0 ; + } +#endif +} +static void cpu_cgroup_css_released (struct cgroup_subsys_state *css) {struct task_group *tg =css_tg (css);
@@ -9517,6 +9531,25 @@ static void cpu_cgroup_fork(struct task_struct *task)task_rq_unlock (rq, task, &rf); } +static void cpu_cgroup_exit (struct task_struct *task) +{ +#ifdef CONFIG_SCHED_CORE +/* +* This is possible if task exit races with core sched being +* disabled due to the task's cgroup no longer being tagged, since +* cpu_core_tag_write_u64() will miss dying tasks. +*/ +if (unlikely (sched_core_enqueued (task))) { +struct rq *rq; +struct rq_flags rf; + + rq =task_rq_lock (task, &rf); +sched_core_dequeue (rq, task); +task_rq_unlock (rq, task, &rf); + } +#endif +} +static int cpu_cgroup_can_attach (struct cgroup_taskset *tset) {struct task_struct *task;
@@ -10084,6 +10117,14 @@ static struct cftype cpu_legacy_files[] = { .write_u64 = cpu_rt_period_write_uint, },#endif +#ifdef CONFIG_SCHED_CORE + { + .name = "core_tag", + .flags = CFTYPE_NOT_ON_ROOT, + .read_u64 = cpu_core_tag_read_u64, + .write_u64 = cpu_core_tag_write_u64, + }, +#endif #ifdef CONFIG_UCLAMP_TASK_GROUP { .name = "uclamp.min",
@@ -10257,6 +10298,14 @@ static struct cftype cpu_files[] = { .write_s64 = cpu_weight_nice_write_s64, },#endif +#ifdef CONFIG_SCHED_CORE + { + .name = "core_tag", + .flags = CFTYPE_NOT_ON_ROOT, + .read_u64 = cpu_core_tag_read_u64, + .write_u64 = cpu_core_tag_write_u64, + }, +#endif #ifdef CONFIG_CFS_BANDWIDTH { .name = "max",
@@ -10285,10 +10334,12 @@ static struct cftype cpu_files[] = {struct cgroup_subsys cpu_cgrp_subsys = { .css_alloc = cpu_cgroup_css_alloc, .css_online = cpu_cgroup_css_online, + .css_offline = cpu_cgroup_css_offline, .css_released = cpu_cgroup_css_released, .css_free = cpu_cgroup_css_free, .css_extra_stat_show = cpu_extra_stat_show, .fork = cpu_cgroup_fork, + .exit = cpu_cgroup_exit, .can_attach = cpu_cgroup_can_attach, .attach = cpu_cgroup_attach, .legacy_cftypes = cpu_legacy_files,
kernel/sched/coretag.c +154 -2
176 lines not on this page · raw message
kernel/sched/sched.h +25 -0
@@ -385,6 +385,11 @@ struct cfs_bandwidth {struct task_group {struct cgroup_subsys_state css; +#ifdef CONFIG_SCHED_CORE +int core_tagged; +u32 sched_core_id; +#endif +#ifdef CONFIG_FAIR_GROUP_SCHED /* schedulable entities of this group on each CPU */ struct sched_entity **se;
@@ -433,6 +438,11 @@ struct task_group { }; +static inline struct task_group *css_tg (struct cgroup_subsys_state *css) +{ + return css ? container_of(css, struct task_group, css) : NULL; +} +#ifdef CONFIG_FAIR_GROUP_SCHED #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
@@ -1170,6 +1180,7 @@ static inline raw_spinlock_t *__rq_lockp(struct rq *rq)return &rq->__lock; } +void sched_core_change_group (struct task_struct *p,struct task_group *new_tg);int sched_core_fork (struct task_struct *p,unsigned long clone_flags);static inline bool sched_core_enqueued (struct task_struct *task)
@@ -1186,6 +1197,16 @@ void sched_core_put(void);int sched_core_share_pid (unsigned long flags,pid_t pid,enum pid_type type); +#ifdef CONFIG_CGROUP_SCHED +u64 cpu_core_tag_read_u64 (struct cgroup_subsys_state *css, +struct cftype *cft); + +int cpu_core_tag_write_u64 (struct cgroup_subsys_state *css,struct cftype *cft, +u64 val); + +void alloc_sched_core_sched_group (struct task_group *tg); +#endif +bool cfs_prio_less (struct task_struct *a,struct task_struct *b,bool fi); int sched_core_cookie_cmp(const struct sched_core_cookie *a,
@@ -1305,6 +1326,10 @@ static inline bool sched_core_enqueued(struct task_struct *task)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) { } +#ifdef CONFIG_CGROUP_SCHED +void alloc_sched_core_sched_group (struct task_group *tg) {} +#endif +#endif /* CONFIG_SCHED_CORE */ static inline void lockdep_assert_rq_held (struct rq *rq)
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
Peter Zijlstra
maintainerRe: [PATCH resend 5/8] sched: cgroup cookie API for core scheduling
Quoted 9 lines · Joel Fernandes (Google)
From: Josh Don <[email protected]>
This adds the API to set/get the cookie for a given cgroup. This interface lives at cgroup/cpu.core_tag.
The cgroup interface can be used to toggle a unique cookie value for all descendent tasks, preventing these tasks from sharing with any others. See
Documentation/admin-guide/hw-vuln/core-scheduling.rstfor a full rundown of both this and the per-task API.
I refuse to read RST. Life's too short for that.
Quoted 21 lines
kernel/sched/coretag.c +21
-0+u64 cpu_core_tag_read_u64 (struct cgroup_subsys_state *css, +struct cftype *cft) +{ +return !!css_tg (css)->core_tagged; +} + +int cpu_core_tag_write_u64 (struct cgroup_subsys_state *css,struct cftype *cft, +u64 val) +{ +static DEFINE_MUTEX (sched_core_group_mutex); +struct task_group *tg =css_tg (css); +struct cgroup_subsys_state *css_tmp; +struct task_struct *p; +unsigned long group_cookie; +int ret =0 ; + +if (val >1 ) +return -ERANGE ; + +if (!static_branch_likely (&sched_smt_present)) +return -EINVAL ;
kernel/sched/coretag.c int sched_core_share_pid(unsigned long flags, pid_t pid, enum pid_type type) · line 476
Quoted 10 lines
kernel/sched/coretag.c +10
-0+ +mutex_lock (&sched_core_group_mutex); + +if (!tg->core_tagged && val) { +/* Tag is being set. Check ancestors and descendants. */ +if (cpu_core_get_group_cookie (tg) || +cpu_core_check_descendants (tg, true/* tag */ )) { + ret = -EBUSY ; + goto out_unlock; + }
So the desired semantics is to only allow a single tag on any upwards path? Isn't that in conflict with the cgroup requirements?
TJ?
kernel/sched/coretag.c int sched_core_share_pid(unsigned long flags, pid_t pid, enum pid_type type) · line 479
Quoted 3 lines
kernel/sched/coretag.c +3
-0+ }else if (tg->core_tagged && !val) { +/* Tag is being reset. Check descendants. */ +if (cpu_core_check_descendants (tg, true/* tag */ )) {
I'm struggling to understand this. If, per the above, you cannot set when either a parent is already set or a child is set, then how can a child be set to refuse clearing?
Quoted 6 lines
kernel/sched/coretag.c +6
-0+ ret = -EBUSY ; + goto out_unlock; + } + }else { + goto out_unlock; + }
Peter Zijlstra
maintainerRe: [PATCH resend 5/8] sched: cgroup cookie API for core scheduling
*sigh*, +tj
Quoted 66 lines · Peter Zijlstra
Quoted 9 lines · Joel Fernandes (Google)
From: Josh Don <[email protected]>
This adds the API to set/get the cookie for a given cgroup. This interface lives at cgroup/cpu.core_tag.
The cgroup interface can be used to toggle a unique cookie value for all descendent tasks, preventing these tasks from sharing with any others. See
Documentation/admin-guide/hw-vuln/core-scheduling.rstfor a full rundown of both this and the per-task API.I refuse to read RST. Life's too short for that.
Quoted 21 lines
kernel/sched/coretag.c +21
-0+u64 cpu_core_tag_read_u64 (struct cgroup_subsys_state *css, +struct cftype *cft) +{ +return !!css_tg (css)->core_tagged; +} + +int cpu_core_tag_write_u64 (struct cgroup_subsys_state *css,struct cftype *cft, +u64 val) +{ +static DEFINE_MUTEX (sched_core_group_mutex); +struct task_group *tg =css_tg (css); +struct cgroup_subsys_state *css_tmp; +struct task_struct *p; +unsigned long group_cookie; +int ret =0 ; + +if (val >1 ) +return -ERANGE ; + +if (!static_branch_likely (&sched_smt_present)) +return -EINVAL ;kernel/sched/coretag.c int sched_core_share_pid(unsigned long flags, pid_t pid, enum pid_type type) · line 476
Quoted 10 lines
kernel/sched/coretag.c +10
-0+ +mutex_lock (&sched_core_group_mutex); + +if (!tg->core_tagged && val) { +/* Tag is being set. Check ancestors and descendants. */ +if (cpu_core_get_group_cookie (tg) || +cpu_core_check_descendants (tg, true/* tag */ )) { + ret = -EBUSY ; + goto out_unlock; + }So the desired semantics is to only allow a single tag on any upwards path? Isn't that in conflict with the cgroup requirements?
TJ?
kernel/sched/coretag.c int sched_core_share_pid(unsigned long flags, pid_t pid, enum pid_type type) · line 479
Quoted 3 lines
kernel/sched/coretag.c +3
-0+ }else if (tg->core_tagged && !val) { +/* Tag is being reset. Check descendants. */ +if (cpu_core_check_descendants (tg, true/* tag */ )) {I'm struggling to understand this. If, per the above, you cannot set when either a parent is already set or a child is set, then how can a child be set to refuse clearing?
Quoted 6 lines
kernel/sched/coretag.c +6
-0+ ret = -EBUSY ; + goto out_unlock; + } + }else { + goto out_unlock; + }
Re: [PATCH resend 5/8] sched: cgroup cookie API for core scheduling
Quoted 10 lines · Peter Zijlstra
Quoted 10 lines · Joel Fernandes (Google)
Quoted 8 lines
kernel/sched/coretag.c +8
-0+ +if (!tg->core_tagged && val) { +/* Tag is being set. Check ancestors and descendants. */ +if (cpu_core_get_group_cookie (tg) || +cpu_core_check_descendants (tg, true/* tag */ )) { + ret = -EBUSY ; + goto out_unlock; + }
Quoted 4 lines
Quoted 4 lines
So the desired semantics is to only allow a single tag on any upwards path? Isn't that in conflict with the cgroup requirements?
TJ?
I carried this requirement over from the previous iteration, but I don't see a reason why we can't just dump this and have each task use the group cookie of its closest tagged ancestor. Joel, is there any context here I'm missing?
FWIW I also just realized that cpu_core_check_descendants() is busted as it recurses only on one child.
Quoted 7 lines
Quoted 7 lines
kernel/sched/coretag.c int sched_core_share_pid(unsigned long flags, pid_t pid, enum pid_type type) · line 479
Quoted 3 lines
kernel/sched/coretag.c +3
-0+ }else if (tg->core_tagged && !val) { +/* Tag is being reset. Check descendants. */ +if (cpu_core_check_descendants (tg, true/* tag */ )) {I'm struggling to understand this. If, per the above, you cannot set when either a parent is already set or a child is set, then how can a child be set to refuse clearing?
Yes this is superfluous with the above semantics.
PATCH RESEND 6/8 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 | 4 +- .../testing/selftests/sched/test_coresched.c | 812 ++++++++++++++++++ 3 files changed, 815 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/sched/test_coresched.c
tools/testing/selftests/sched/.gitignore +1 -0
@@ -1 +1,2 @@ cs_prctl_test +test_coresched
tools/testing/selftests/sched/Makefile +2 -2
@@ -8,7 +8,7 @@ CFLAGS += -O2 -Wall -g -I./ -I../../../../usr/include/ -Wl,-rpath=./ \ $(CLANG_FLAGS) LDLIBS += -lpthread -TEST_GEN_FILES := cs_prctl_test -TEST_PROGS := cs_prctl_test +TEST_GEN_FILES := test_coresched cs_prctl_test +TEST_PROGS := test_coresched cs_prctl_test include ../lib.mk
tools/testing/selftests/sched/test_coresched.c new file +812 -0
812 lines not on this page · raw message
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
PATCH RESEND 7/8 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 | 460 ++++++++++++++++++ Documentation/admin-guide/hw-vuln/index.rst | 1 + 2 files changed, 461 insertions(+) create mode 100644 Documentation/admin-guide/hw-vuln/core-scheduling.rst
Documentation/admin-guide/hw-vuln/core-scheduling.rst new file +460 -0
@@ -0,0 +1,460 @@ +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 trust each other. +The core scheduler uses this information to make sure that tasks that do not +trust each other will 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(2) 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. +Permission to change the ``cookie`` and hence the core scheduling group it +represents is based on ``ptrace access``. + +:: + + #include <sys/prctl.h> + + int prctl(int option, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5); + + int prctl(PR_SCHED_CORE_SHARE, sub_command, pid, pid_type, 0); + +option: + ``PR_SCHED_CORE_SHARE`` + +arg2: + sub-command: + + - ``PR_SCHED_CORE_CLEAR 0 -- clear core_sched cookie of pid`` + - ``PR_SCHED_CORE_CREATE 1 -- create a new cookie for pid`` + - ``PR_SCHED_CORE_SHARE_FROM 2 -- copy core_sched cookie from pid`` + - ``PR_SCHED_CORE_SHARE_TO 3 -- copy core_sched cookie to pid`` + +arg3: + ``pid`` of the task for which the operation applies where ``pid == 0`` + implies current process. + +arg4: + ``pid_type`` for PR_SCHED_CORE_CLEAR/CREATE/SHARE_TO is an enum + {PIDTYPE_PID=0, PIDTYPE_TGID, PIDTYPE_PGID} and determines how the target + ``pid`` should be interpreted. ``PIDTYPE_PID`` indicates that the target + ``pid`` should be treated as an individual task, ``PIDTYPE_TGID`` a process + or thread group, and ``PIDTYPE_PGID`` or a process group ``PIDTYPE_PGID``. + +arg5: + MUST be equal to 0. + +Return Value: +:: + + EINVAL - bad parameter + ENOMEM - unable to allocate memory for a cookie. + ESRCH - unable to find specified pid. + EPERM - caller lacks permission to change the cookie of target. + EACCES - caller lacks permission to change the cookies of all + targets of pidtype. + +Creation +~~~~~~~~ +Creation is accomplished by creating a ''cookie'' for the target pid which may +be an individual task if tgt_pid_type is ``PIDTYPE_PID``, a process and it's +threads if ``PIDTYPE_TGID`` or all tasks of the specified process group if +``PIDTYPE_PGID``. + +:: + + if (prctl(PR_SCHED_CORE_SHARE, PR_SCHED_CORE_CREATE, tgt_pid, tgt_pid_type, 0) < 0) + handle_error("sched_core_create failed"); + +Examples: +:: + + Given a process group, process and thread hierarchy: + + ## Create a thread/process/process group hierarchy + tid=40003, / tgid=40003 / pgid=40003: 0 + tid=40004, / tgid=40004 / pgid=40003: 0 + tid=40005, / tgid=40004 / pgid=40003: 0 + tid=40007, / tgid=40004 / pgid=40003: 0 + tid=40008, / tgid=40004 / pgid=40003: 0 + tid=40006, / tgid=40006 / pgid=40003: 0 + tid=40009, / tgid=40006 / pgid=40003: 0 + tid=40010, / tgid=40006 / pgid=40003: 0 + tid=40011, / tgid=40006 / pgid=40003: 0 + + ## Set a cookie on entire process group + 0 = prctl(59, 1, 0, 2, 0) + tid=40003, / tgid=40003 / pgid=40003: 1f7121100000000 + tid=40004, / tgid=40004 / pgid=40003: 1f7121100000000 + tid=40005, / tgid=40004 / pgid=40003: 1f7121100000000 + tid=40007, / tgid=40004 / pgid=40003: 1f7121100000000 + tid=40008, / tgid=40004 / pgid=40003: 1f7121100000000 + tid=40006, / tgid=40006 / pgid=40003: 1f7121100000000 + tid=40009, / tgid=40006 / pgid=40003: 1f7121100000000 + tid=40010, / tgid=40006 / pgid=40003: 1f7121100000000 + tid=40011, / tgid=40006 / pgid=40003: 1f7121100000000 + + ## Set a new cookie on entire process/TGID [40006] + 0 = prctl(59, 1, 40006, 1, 0) + tid=40003, / tgid=40003 / pgid=40003: 1f7121100000000 + tid=40004, / tgid=40004 / pgid=40003: 1f7121100000000 + tid=40005, / tgid=40004 / pgid=40003: 1f7121100000000 + tid=40007, / tgid=40004 / pgid=40003: 1f7121100000000 + tid=40008, / tgid=40004 / pgid=40003: 1f7121100000000 + tid=40006, / tgid=40006 / pgid=40003: 1f7121200000000 + tid=40009, / tgid=40006 / pgid=40003: 1f7121200000000 + tid=40010, / tgid=40006 / pgid=40003: 1f7121200000000 + tid=40011, / tgid=40006 / pgid=40003: 1f7121200000000 + +Removal +~~~~~~~ +Removing a task(s) from a core scheduling group is done by specifying a target +pid and it's type. Again, pidtype determines the interpretation of the target pid. + +:: + + if (prctl(PR_SCHED_CORE_SHARE, PR_SCHED_CORE_SHARE_CLEAR, tgt_pid, tgt_pid_type, 0) < 0) + handle_error("clr_tid sched_core failed"); + +Examples (continued from above): +:: + + ## Clear a cookie on a single task [40006] + 0 = prctl(59, 0, 40006, 0, 0) + tid=40003, / tgid=40003 / pgid=40003: 1f7121200000000 + tid=40004, / tgid=40004 / pgid=40003: 1f7121100000000 + tid=40005, / tgid=40004 / pgid=40003: 1f7121100000000 + tid=40007, / tgid=40004 / pgid=40003: 1f7121100000000 + tid=40008, / tgid=40004 / pgid=40003: 1f7121100000000 + tid=40006, / tgid=40006 / pgid=40003: 0 + tid=40009, / tgid=40006 / pgid=40003: 1f7121200000000 + tid=40010, / tgid=40006 / pgid=40003: 1f7121200000000 + tid=40011, / tgid=40006 / pgid=40003: 1f7121200000000 + + ## Clear cookies on the entire process group + 0 = prctl(59, 0, 0, 2, 0) + tid=40003, / tgid=40003 / pgid=40003: 0 + tid=40004, / tgid=40004 / pgid=40003: 0 + tid=40005, / tgid=40004 / pgid=40003: 0 + tid=40007, / tgid=40004 / pgid=40003: 0 + tid=40008, / tgid=40004 / pgid=40003: 0 + tid=40006, / tgid=40006 / pgid=40003: 0 + tid=40009, / tgid=40006 / pgid=40003: 0 + tid=40010, / tgid=40006 / pgid=40003: 0 + tid=40011, / tgid=40006 / pgid=40003: 0 + +Cookie Transferal +~~~~~~~~~~~~~~~~~ + +Transferring a cookie between the current tasks and other tasks is possible using +``PR_SCHED_CORE_SHARE_FROM`` and ``PR_SCHED_CORE_SHARE_TO`` depending on +direction. A helper utility can copy cookies between tasks by first copying a +cookie from the source task then sharing it using ``PR_SCHED_CORE_SHARE_TO`` to +the target ``pid`` with, again, pidtype determine the interpretation of the target pid. + +:: + + if (prctl(PR_SCHED_CORE_SHARE, PR_SCHED_CORE_SHARE_FROM, from_pid, 0, 0) < 0) + handle_error("from_tid sched_core failed"); + + if (prctl(PR_SCHED_CORE_SHARE, PR_SCHED_CORE_SHARE_TO, to_pid, tgt_pid_type, 0) < 0) + handle_error("to_tid sched_core failed"); + +Examples (continued from above): +:: + + pidtype can even be used with SHARE_TO to "spread" the cookie among the + appropriate group by sharing with self. + + ## given setup + tid=40003, / tgid=40003 / pgid=40003: 1f7121200000000 + tid=40004, / tgid=40004 / pgid=40003: 1f7121100000000 + tid=40005, / tgid=40004 / pgid=40003: 1f7121100000000 + tid=40007, / tgid=40004 / pgid=40003: 1f7121100000000 + tid=40008, / tgid=40004 / pgid=40003: 1f7121100000000 + tid=40006, / tgid=40006 / pgid=40003: 0 + tid=40009, / tgid=40006 / pgid=40003: 1f7121200000000 + tid=40010, / tgid=40006 / pgid=40003: 1f7121200000000 + tid=40011, / tgid=40006 / pgid=40003: 1f7121200000000 + + ## Copy cookie from current [40003] to current as pidtype PGID + 0 = prctl(59, 3, 0, 2, 0) + tid=40003, / tgid=40003 / pgid=40003: 1f7121200000000 + tid=40004, / tgid=40004 / pgid=40003: 1f7121200000000 + tid=40005, / tgid=40004 / pgid=40003: 1f7121200000000 + tid=40007, / tgid=40004 / pgid=40003: 1f7121200000000 + tid=40008, / tgid=40004 / pgid=40003: 1f7121200000000 + tid=40006, / tgid=40006 / pgid=40003: 1f7121200000000 + tid=40009, / tgid=40006 / pgid=40003: 1f7121200000000 + tid=40010, / tgid=40006 / pgid=40003: 1f7121200000000 + tid=40011, / tgid=40006 / pgid=40003: 1f7121200000000 + +Other prctl Notes +~~~~~~~~~~~~~~~~~ + +It is important to note that, on a ``clone(2)`` or ``fork(2)``, the child will +be assigned a copy of the same ``cookie`` as its parent and thus in the same +core scheduling group. Basically the default is that fork-only or threaded +applications can share the cores of a processor. A new cookie can be created +post clone/fork if such sharing is undesirable. An ``execve(2)`` will however, +automatically assign a new cookie to a task as no reasonable assumptions can be +made on the security of the new code. + +.. 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 used to set a shared cookie. + +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. + +Kernel protection from untrusted tasks +-------------------------------------- +Entry into the kernel (syscall, IRQ or VMEXIT) needs protection. The scheduler +on its own cannot protect the kernel executing concurrently with an untrusted +task in a core. This is because the scheduler is unaware of interrupts/syscalls +at scheduling time. To mitigate this, an IPI is sent to siblings on kernel +entry. This IPI forces the sibling to enter kernel mode and wait before +returning to user until all siblings of the core have left kernel mode. This +process is also known as stunning. For good performance, an IPI is sent only +to a sibling only if it is running a tagged task. If a sibling is running a +kernel thread or is idle, no IPI is sent. + +The kernel protection feature is off by default. To protect the kernel, pass a +comma separated list of what to protect to the ht_protect= kernel command line +option. Possible values are irq, syscall and kvm. + +Note that an arch has to define the ``TIF_UNSAFE_RET`` thread info flag to be +able to use kernel protection. Also, if protecting the kernel from a VM is +desired, an arch should call kvm_exit_to_guest_mode() during ``VMENTER`` and +kvm_exit_to_guest_mode() during ``VMEXIT``. Currently, x86 support both these. + +Other alternative ideas discussed for kernel protection are listed below just +for completeness. They all have limitations: + +1. Changing interrupt affinities to a trusted core which does not execute untrusted tasks +######################################################################################### +By changing the interrupt affinities to a designated safe-CPU which runs +only trusted tasks, IRQ data can be protected. One issue is this involves +giving up a full CPU core of the system to run safe tasks. Another is that, +per-cpu interrupts such as the local timer interrupt cannot have their +affinity changed. Also, sensitive timer callbacks such as the random entropy timer +can run in softirq on return from these interrupts and expose sensitive +data. In the future, that could be mitigated by forcing softirqs into threaded +mode by utilizing a mechanism similar to ``CONFIG_PREEMPT_RT``. + +Yet another issue with this is, for multiqueue devices with managed +interrupts, the IRQ affinities cannot be changed; however it could be +possible to force a reduced number of queues which would in turn allow to +shield one or two CPUs from such interrupts and queue handling for the price +of indirection. + +2. Running IRQs as threaded-IRQs +################################ +This would result in forcing IRQs into the scheduler which would then provide +the process-context mitigation. However, not all interrupts can be threaded. +Also this does nothing about syscall entries. + +3. Kernel Address Space Isolation +################################# +System calls could run in a much restricted address space which is +guaranteed not to leak any sensitive data. There are practical limitation in +implementing this - the main concern being how to decide on an address space +that is guaranteed to not have any sensitive data. + +4. Limited cookie-based protection +################################## +On a system call, change the cookie to the system trusted cookie and initiate a +schedule event. This would be better than pausing all the siblings during the +entire duration for the system call, but still would be a huge hit to the +performance. + +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. + +coresched command line option +----------------------------- +The coresched kernel command line option can be used to: + - Keep coresched on even if system is not vulnerable x (``coresched=on``). + - Keep coresched off even if system is vulnerable (``coresched=off``). + - Keep coresched on only if system is vulnerable x (``coresched=secure``). + +The default is ``coresched=secure``. However a user who has a usecase that +needs core-scheduling, such as improving performance of VMs by tagging vCPU +threads, could pass ``coresched=on`` to force it on. + +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. + +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. + +Future work +----------- +Skipping per-HT mitigations if task is trusted +############################################## +If core scheduling is enabled, by default all tasks trust each other as +mentioned above. In such scenario, it may be desirable to skip the same-HT +mitigations on return to the trusted user-mode to improve performance.
Documentation/admin-guide/hw-vuln/index.rst +1 -0
@@ -15,3 +15,4 @@ are configurable at compile, boot or run time. tsx_async_abort multihit.rst special-register-buffer-data-sampling.rst + core-scheduling.rst
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
PATCH RESEND 8/8 sched: Debug bits...
In reply to Joel Fernandes (Google)--- kernel/sched/core.c | 40 +++++++++++++++++++++++++++++++++++++++- kernel/sched/fair.c | 12 ++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-)
kernel/sched/core.c +39 -1
@@ -106,6 +106,10 @@ static inline bool prio_less(struct task_struct *a, struct task_struct *b, boolint pa =__task_prio (a), pb =__task_prio (b); +trace_printk ("(%s/%d;%d,%llu,%llu) ?< (%s/%d;%d,%llu,%llu)\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;
@@ -292,12 +296,16 @@ static void __sched_core_enable(void)static_branch_enable (&__sched_core_enabled);__sched_core_flip (true); + +printk ("core sched enabled\n" ); }static void __sched_core_disable (void ) {__sched_core_flip (false);static_branch_disable (&__sched_core_enabled); + +printk ("core sched disabled\n" ); }void sched_core_get (void )
@@ -5361,6 +5369,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 %llu\n" , + rq->core->core_task_seq, + rq->core->core_pick_seq, + rq->core_sched_seq, + next->comm, next->pid, + next->core_cookie.userspace_id); + rq->core_pick =NULL ;return next; }
@@ -5455,6 +5470,10 @@ 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 %llu\n" , + i, p->comm, p->pid, + p->core_cookie.userspace_id); +/* * If this new candidate is of higher priority than the * previous; and they're incompatible; we need to wipe
@@ -5471,6 +5490,10 @@ 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 %llu\n" , + max->comm, max->pid, + max->core_cookie.userspace_id); +if (old_max) { rq->core->core_forceidle = false;for_each_cpu (j, smt_mask) {
@@ -5492,6 +5515,8 @@ 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 %llu\n" , next->comm, next->pid, + next->core_cookie.userspace_id);/* * Reschedule siblings
@@ -5533,13 +5558,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%llu/0x%llu\n" , + rq_i->cpu, rq_i->core_pick->comm, + rq_i->core_pick->pid, + rq_i->core_pick->core_cookie.userspace_id, + rq_i->core->core_cookie.userspace_id); +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); }
@@ -5579,6 +5612,11 @@ 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 %llu\n" , + p->comm, p->pid, that, this, + p->core_occupation, dst->idle->core_occupation, + cookie->userspace_id); + p->on_rq =TASK_ON_RQ_MIGRATING ;deactivate_task (src, p,0 );set_task_cpu (p, this);
kernel/sched/fair.c +12 -0
@@ -10783,6 +10783,9 @@ static inline void task_tick_core(struct rq *rq, struct task_struct *curr)*/ static void se_fi_update (struct sched_entity *se,unsigned int fi_seq,bool forceidle) { +bool root = true; +long old, new; +for_each_sched_entity (se) {struct cfs_rq *cfs_rq =cfs_rq_of (se);
@@ -10792,6 +10795,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.31.0.291.g576ba9dcdaf-goog