UMCG - how should we proceed? Should we?
Hi Peter!
TL;DR: which approach, if any, should a UMCG implementation in the mainline kernel use?
Details:
We are rolling out internally a UMCG implementation copied below (with some boilerplate omitted), so I would like to restart our discussion on the topic.
The implementation below is different from what we had earlier
(https://lore.kernel.org/lkml/[email protected]/)
in that it keeps UMCG state in the kernel rather than TLS.
While having UMCG state in TLS is _much_ better, as it makes state synchronization between the userspace and the kernel much simpler, the whole page pinning machinery in the link above looked very scary, honestly.
So if we are going to ever have something like UMCG in the mainline kernel, we need to figure out the approach to use: the TLS-based one, something similar to what we have now internally (details below), or something else. Or none at all...
While I would very much prefer to have it done your way (state in TLS), the page pinning business was too much for me. If you can figure out a way to do it cleanly and reliably, great!
The main differences between what you had in the TLS patchset and what is below:
- per worker/server state not in TLS but in task_struct
- we keep a list of idle workers and a list of idle servers in mm
- worker wake events are delivered not to servers which ran the workers earlier, but to idle servers from the idle server list
- worker preemption happens not via a syscall (umcg_kick) but by hooking into sched_tick
None of the differences above are deal breakers; again, if the TLS/page pinning approach is viable, we will gladly use it.
Thanks,
Peter
I removed some boilerplate cruft from below; also removed some formatting, hoping that bots won't try to apply the code to any tree.
This patch (before mutilation) applies to 5.15.
[...]
arch/x86/entry/syscalls/syscall_64.tbl | 1 + fs/exec.c | 1 + include/linux/mm_types.h | 5 + include/linux/sched.h | 62 ++ include/linux/syscalls.h | 6 +- include/linux/tracehook.h | 2 + include/uapi/asm-generic/unistd.h | 3 + include/uapi/linux/umcg.h | 55 ++ init/Kconfig | 22 + kernel/exit.c | 2 + kernel/fork.c | 5 + kernel/sched/Makefile | 1 + kernel/sched/core.c | 19 +- kernel/sched/sched.h | 2 + kernel/sched/umcg.c | 1106 ++++++++++++++++++++++++ kernel/sys_ni.c | 1 + [...] 18 files changed, 1292 insertions(+), 7 deletions(-) create mode 100644 include/uapi/linux/umcg.h create mode 100644 kernel/sched/umcg.c
[...]
fs/exec.c +1 -0
@@ -1840,6 +1840,7 @@ static int bprm_execve(struct linux_binprm *bprm, current->fs->in_exec =0 ; current->in_execve =0 ;rseq_execve (current); +umcg_execve (current);acct_update_integrals (current);task_numa_free (current, false);return retval;
include/linux/mm_types.h +5 -0
@@ -560,6 +560,11 @@ struct mm_struct {/* numa_scan_seq prevents two threads setting pte_numa */ int numa_scan_seq; +#endif +#ifdef CONFIG_UMCG +spinlock_t umcg_lock; +struct list_head umcg_idle_server_list; +struct task_struct *umcg_idle_workers;#endif /* * An operation with batched TLB flushing is going on. Anything
include/linux/sched.h +61 -0
79 lines not on this page · raw message
[...]
include/linux/tracehook.h +2 -0
@@ -179,6 +179,8 @@ static inline void set_notify_resume(struct task_struct *task)*/ static inline void tracehook_notify_resume (struct pt_regs *regs) { +umcg_notify_resume (); +clear_thread_flag (TIF_NOTIFY_RESUME );/* * This barrier pairs with task_work_add()->set_notify_resume() after
[...]
include/uapi/linux/umcg.h new file +54 -0
54 lines not on this page · raw message
init/Kconfig +22 -0
@@ -1688,6 +1688,28 @@ config MEMBARRIER If unsure, say Y. +config UMCG + bool "Enable User Managed Concurrency Groups API" + depends on 64BIT + depends on GOOGLE_SYSCALLS + default n + help + Enable User Managed Concurrency Groups API, which form the basis + for an in-process M:N userspace scheduling framework. + At the moment this is an experimental/RFC feature that is not + guaranteed to be backward-compatible. + +config UMCG_PREEMPT_JIFFIES + int "Interval at which running UMCG workers are preempted." + depends on UMCG + default 10 + help + Normally UMCG workers cooperatively "yield" their servers. + However, UMCG worker preemption is needed in cases when + the worker is waiting on a spinlock that a descheduled worker + holds. In this case preempting a running worker gives the + userspace scheduler a chance to run the lock-holding worker. + config KALLSYMS bool "Load all symbols for debugging/ksymoops" if EXPERT default y
kernel/exit.c +2 -0
@@ -743,6 +743,8 @@ void __noreturn do_exit(long code)if (unlikely (!tsk->pid))panic ("Attempted to kill the idle task!" ); +umcg_handle_exit (); +/* * If do_exit is called because this processes oopsed, it's possible * that get_fs() was left as KERNEL_DS, so reset it to USER_DS before
kernel/fork.c +5 -0
@@ -1061,6 +1061,11 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,init_tlb_flush_pending (mm);#if defined(CONFIG_TRANSPARENT_HUGEPAGE ) && !USE_SPLIT_PMD_PTLOCKS mm->pmd_huge_pte =NULL ; +#endif +#ifdef CONFIG_UMCG +INIT_LIST_HEAD (&mm->umcg_idle_server_list); + mm->umcg_idle_workers =NULL ; +spin_lock_init (&mm->umcg_lock);#endif mm_init_uprobes_state (mm);hugetlb_count_init (mm);
[...]
kernel/sched/core.c +15 -4
@@ -3944,8 +3944,7 @@ bool ttwu_state_match(struct task_struct *p, unsigned int state, int *success)* Return: %true if @p->state changes (an actual wakeup was done), * %false otherwise. */ -static int -try_to_wake_up (struct task_struct *p,unsigned int state,int wake_flags) +int try_to_wake_up (struct task_struct *p,unsigned int state,int wake_flags) {unsigned long flags;int cpu, success =0 ;
@@ -4227,6 +4226,12 @@ static void __sched_fork(unsigned long clone_flags, struct task_struct *p) p->wake_entry.u_flags =CSD_TYPE_TTWU ; p->migration_pending =NULL ;#endif +#ifdef CONFIG_UMCG + p->umcg_worker_id =0 ; + p->umcg_server =NULL ; +INIT_LIST_HEAD (&p->umcg_idle_server_list); +atomic_long_set (&p->umcg_status,0 ); +#endif }DEFINE_STATIC_KEY_FALSE (sched_numa_balancing);
@@ -5224,6 +5229,7 @@ void scheduler_tick(void)resched_latency_warn (cpu, resched_latency);perf_event_task_tick (); +umcg_tick (curr);#ifdef CONFIG_SMP rq->idle_balance =idle_cpu (cpu);
@@ -6318,11 +6324,13 @@ void __noreturn do_task_dead(void)static inline void sched_submit_work (struct task_struct *tsk) {unsigned int task_flags; +bool umcg_worker;if (task_is_running (tsk))return ; task_flags = tsk->flags; + umcg_worker =umcg_wq_work (tsk);/* * If a worker went to sleep, notify and ask workqueue whether * it wants to wake up a task to maintain concurrency.
@@ -6331,12 +6339,15 @@ static inline void sched_submit_work(struct task_struct *tsk)* in the possible wakeup of a kworker and because wq_worker_sleeping() * requires it. */ -if (task_flags & (PF_WQ_WORKER |PF_IO_WORKER )) { +if ((task_flags & (PF_WQ_WORKER |PF_IO_WORKER )) || umcg_worker) {preempt_disable ();if (task_flags &PF_WQ_WORKER )wq_worker_sleeping (tsk); -else +else if (task_flags &PF_IO_WORKER )io_wq_worker_sleeping (tsk); + +if (umcg_worker) +umcg_wq_worker_sleeping (tsk);preempt_enable_no_resched (); }
kernel/sched/sched.h +2 -0
@@ -3060,6 +3060,8 @@ static inline bool is_per_cpu_kthread(struct task_struct *p)extern void swake_up_all_locked (struct swait_queue_head *q);extern void __prepare_to_swait (struct swait_queue_head *q,struct swait_queue *wait); +extern int try_to_wake_up (struct task_struct *tsk,unsigned int state,int wake_flags); +#ifdef CONFIG_PREEMPT_DYNAMIC extern int preempt_dynamic_mode;extern int sched_dynamic_mode (const char *str);
kernel/sched/umcg.c new file +1106 -0
1,106 lines not on this page · raw message
[...]
Peter Zijlstra
maintainerQuoted 17 lines · Peter Oskolkov
Hi Peter!
TL;DR: which approach, if any, should a UMCG implementation in the mainline kernel use?
Details:
We are rolling out internally a UMCG implementation copied below (with some boilerplate omitted), so I would like to restart our discussion on the topic.
The implementation below is different from what we had earlier
(https://lore.kernel.org/lkml/[email protected]/)
in that it keeps UMCG state in the kernel rather than TLS.While having UMCG state in TLS is _much_ better, as it makes state synchronization between the userspace and the kernel much simpler, the whole page pinning machinery in the link above looked very scary, honestly.
Quoted 6 lines
So if we are going to ever have something like UMCG in the mainline kernel, we need to figure out the approach to use: the TLS-based one, something similar to what we have now internally (details below), or something else. Or none at all...
While I would very much prefer to have it done your way (state in TLS), the page pinning business was too much for me. If you can figure out a way to do it cleanly and reliably, great!
A few quick notes without having looked at the patch...
Quoted 1 line
The main differences between what you had in the TLS patchset and what is below:
(note that in the end the per-task UMCG info thing didn't *need* to be TLS, although it is a logical place to put it)
Quoted 2 lines
- per worker/server state not in TLS but in
task_struct
- we keep a list of idle workers and a list of idle servers in mm
How much of a scalability fail is that? Mathieu and me are currently poking at a rseq/cid regression due to large multi thread contention on mm data.
But yeah, I think this was one of the open issues we still had; with the other implementation -- I seem to have a half finished patch for an idle_server list.
Quoted 2 lines
- worker wake events are delivered not to servers which ran the workers earlier, but to idle servers from the idle server list
Provided there is one I take it; very easy to run out of idle things. Also, what if you want to explicitly manage placement, can you still direct the wakeup?
Quoted 2 lines
- worker preemption happens not via a syscall (
umcg_kick) but by hooking intosched_tick
sched_tick would render it somewhat unsuitable for RT workloads/schedulers where you might need more immediate preemption.
Quoted 2 lines
None of the differences above are deal breakers; again, if the TLS/page pinning approach is viable, we will gladly use it.
Urgh, so yeah.. I meant to go look at the whole UMCG thing again with an eye specifically at inter-process support.
I'm hoping inter-process UMCG can be used to implement custom libpthread that would allow running most of userspace under a custom UMCG scheduler and obviate the need for this horrible piece of shit eBPF sched thing.
But I keep getting side-tracked with other stuff :/ I'll try and bump this stuff up the todo list.
Quoted 26 lines · Peter Zijlstra
Quoted 17 lines · Peter Oskolkov
Hi Peter!
TL;DR: which approach, if any, should a UMCG implementation in the mainline kernel use?
Details:
We are rolling out internally a UMCG implementation copied below (with some boilerplate omitted), so I would like to restart our discussion on the topic.
The implementation below is different from what we had earlier
(https://lore.kernel.org/lkml/[email protected]/)
in that it keeps UMCG state in the kernel rather than TLS.While having UMCG state in TLS is _much_ better, as it makes state synchronization between the userspace and the kernel much simpler, the whole page pinning machinery in the link above looked very scary, honestly.
Quoted 6 lines
So if we are going to ever have something like UMCG in the mainline kernel, we need to figure out the approach to use: the TLS-based one, something similar to what we have now internally (details below), or something else. Or none at all...
While I would very much prefer to have it done your way (state in TLS), the page pinning business was too much for me. If you can figure out a way to do it cleanly and reliably, great!
Quoted 6 lines
A few quick notes without having looked at the patch...
Quoted 1 line
The main differences between what you had in the TLS patchset and what is below:
(note that in the end the per-task UMCG info thing didn't *need* to be TLS, although it is a logical place to put it)
Yes, of course. By "TLS" here I mean in userspace per task. Just easier to type "TLS" than "a per-task userspace area similar to rseq"...
Quoted 7 lines
Quoted 3 lines
- per worker/server state not in TLS but in
task_struct
- we keep a list of idle workers and a list of idle servers in mmHow much of a scalability fail is that? Mathieu and me are currently poking at a rseq/cid regression due to large multi thread contention on mm data.
Our main use case is having a small number of servers and a single cross-server queue/scheduler in the userspace, not per-server queues/schedulers, so doing a couple of instructions (adding tasks to an SLL) under a spinlock does not seem to be an issue; if it becomes an issue, we can always switch to lock-free SLLs.
Quoted 11 lines
But yeah, I think this was one of the open issues we still had; with the other implementation -- I seem to have a half finished patch for an
idle_serverlist.Quoted 2 lines
- worker wake events are delivered not to servers which ran the workers earlier, but to idle servers from the idle server list
Provided there is one I take it; very easy to run out of idle things. Also, what if you want to explicitly manage placement, can you still direct the wakeup?
As I mentioned above, we don't have per-server queues/schedulers, so we didn't need to direct wakeups to specific servers. Again, our model is if we have M servers and N workers, and M workers are running (i.e. no idle servers), waking a server when a blocked worker wakes means either preempting a running worker or grabbing an additional CPU; none of these options fit our model well.
Doing it in a more flexible and scalable way to accommodate per-server queues/scheduling and RT scheduling would be great, of course, but I suspect the implementation will be more complex; and we definitely would like to stick to the principle "userspace cannot have more running tasks/threads than there are servers" (background stuff excluded, of course; but scheduling code is very much "foreground").
Quoted 6 lines
Quoted 3 lines
- worker preemption happens not via a syscall (
umcg_kick) but by hooking intosched_tick
sched_tickwould render it somewhat unsuitable for RT workloads/schedulers where you might need more immediate preemption.
Yes; on the other hand, having preemption only via a syscall (umcg_kick) means the userspace should be tracking all running workers, juggling timers, etc.; and the same unresolved question of needing extra CPUs do to all this (who will kick workers from the userspace if they occupy all allocated CPUs?); using sched_tick is much simpler and does not require extra concurrency/cpus. Maybe we can have it both ways: an explicit umcg_kick() for RT use cases and "regular" tick-based preemption?
Quoted 7 lines
Quoted 3 lines
None of the differences above are deal breakers; again, if the TLS/page pinning approach is viable, we will gladly use it.
Urgh, so yeah.. I meant to go look at the whole UMCG thing again with an eye specifically at inter-process support.
Quoted 6 lines
I'm hoping inter-process UMCG can be used to implement custom libpthread that would allow running most of userspace under a custom UMCG scheduler and obviate the need for this horrible piece of shit eBPF sched thing.
But I keep getting side-tracked with other stuff :/ I'll try and bump this stuff up the todo list.
In-process vs cross-process userspace scheduling are two different/distinct use cases for us, and doing them separately means they can be worked on by different people in parallel (i.e. done faster). Can this all be done as one thing that addresses both use cases? Probably yes; the question is how long will it take?