0

I've read that post and would like to know, if a process/program is running faster in root/kernel-mode. Currently I think that it must be the case, because of not going through those OS-rings (user- to kernel-ring) during the cpu-execution. Am I right ?

Community
  • 1
  • 1
Jorgos
  • 141
  • 9

1 Answers1

3

This is true for ring transitions, yes. But most CPU time in typical programs is not spent in ring transitions. If a program spends 10%, which is a lot, it can get faster at most by 10%.

When you write: while (true) ; that never transitions and you will not observe any performance difference.

More generally, the protections that CPU rings, virtual memory and virtualization bring are safety features that do come at a perf cost. The cost is proportional to how much you use those features. A Microsoft study once found that virtual memory typically costs 10-20% of performance if I remember correctly.

Kernel mode memory is virtual just like user mode memory is (at least in current operating systems).

usr
  • 168,620
  • 35
  • 240
  • 369
  • Just to be clear, memory access in kernel mode isn't faster than in user mode. Kernel addresses are virtual. Obviously the kernel's memory management code needs to keep track of which physical page addresses are in use, and actually build page tables. IDK if it would be possible for kernel space to be non-virtual on x86 (i.e. disable paging when entering OS, re-enable when leaving), but no OS I'm aware of works that way. – Peter Cordes Sep 17 '15 at 17:44
  • 1
    On applications that waste a lot of time in virtual memory management, using huge pages can speed things up a lot without losing virtual memory protection. – Zan Lynx Sep 17 '15 at 19:53
  • We now have exploits involving speculative computing called "Spectre" exploits, and the patches slow things down by limiting what that can do. But if you're running in kernel mode, wouldn't you get the full benefits of speculative computing? – sudo Jul 16 '18 at 19:19
  • @sudo I'm certainly not an expert at this but I understand the following: The CPU is still speculating even with all the patches but the kernel performs additional flushing operations when there is a context switch. So it might be that there is no perf cost to spectre protections if there are (almost) no context switches. – usr Jul 16 '18 at 19:59
  • If there are switches then the protections are not applied by operating systems if the switch is in the same "protection domain" for some definition of that term (e.g. same user). So same process switches would not be protected anyway. But running everything in kernel mode would save the most cost I believe. But do not trust what I'm saying here ;-) – usr Jul 16 '18 at 19:59
  • @usr Actually I was referring to the permissions switching when syscalls are made and prompt the kernel mode to kick in and do something on the userspace process's behalf, not the context switching happening when the CPU changes threads. Though syscalls that block do generally trigger context switches. – sudo Jul 16 '18 at 23:23
  • I don't know how Spectre attacks work with context switches. The example and paper I read were about abusing the branch prediction done in the state of elevated permissions. – sudo Jul 16 '18 at 23:30
  • The attacked process does the predictions and the attacker reads them out. I understand that when switching between different protection domains "stuff" gets cleared from the CPU so that reading out is not possible. @sudo – usr Jul 17 '18 at 07:07