The page tables change in a kernel context switch, whereas they don't in a user context switch. Depending on your CPU and OS, this may or may not matter; e.g., modern CPUs typically have caches that are tagged with the context (e.g. PCID on Intel), but the Spectre flavor of the day may require that you simply flush the entire L1 cache on context switch anyway if you're unlucky.
The biggest cost of a context switch, though, is typically that your CPU goes to do something else, which reduces the efficiency of any caches (including branch prediction etc.). This is the same whether you have kernel or userspace context switches, assuming the threads you're switching between touch different code and/or data.
Well, page tables don't change on a thread context switch, that is relevant for this discussion.
Re:Re: 100k Threads? (2002)
by PhilipRoman on Saturday September 23, 2023 @10:04AM
(#37623320)
Why do page tables need to change in a context switch between two threads? AFAIK the kernel is mapped in every process, just not accessible in user mode.
Green threads can be switched between in a single time slice without ever doing a single syscall. This is trivially true: you can make a simple coroutine implementation in C that can switch back and forth between routines thousands of times in a single timeslice.
Re:Re: 100k Threads? (2002)
by otabdeveloper4 on Saturday September 23, 2023 @10:13AM
(#37623390)
Technically true, but nobody ever does this because it is a massive waste of CPU cycles.
Both OS and "green" threads are meant to switch when doing i/o.
Userspace IO is a thing though.
Even with traditional kernel side IO, you might not want to attempt a syscall if the socket is known not to be ready.
Also when implementing purely userspace synchronization primitives, being able to swap to a different context purely in userspace is nice.
User/kernel space switch is a different kind of switch. Also when you do it cooperatively, context switches can be a bit more efficient as the runtime has better knowledge about the set of live registers at the switch point.
Re:Re: 100k Threads? (2002)
by otabdeveloper4 on Saturday September 23, 2023 @10:12AM
(#37623381)
The "different kind of switch" you talk about doesn't exist on any OS of the past 30 years.
I'm talking about raising a process privilege level, via whatever syscall mechanism. Which is a completely different code path than a process/thread context switch and, among other things, doesn't require going through the scheduler.
The cost of crossing the userspace/kernel boundary happens on any system call and has nothing to do with context switching.
(Modern OS's typically context switch during system calls for i/o, exactly the same as userspace threads.)