Live data from Hacker News

Evolution of the x86 context switch in Linux (2018)

maizure.org

11–20 of 35 posts

Re: Evolution of the x86 context switch in Linux (2018)

#11
post #3

Enjoyed this article, anybody know the significance of adding the do..while(0) loop within the macro starting Linux 1.3? Was curious if it guards against some C pre-processor issues. /** include/asm-i386/system.h */ #define switch_to(tsk) do { [...] } while (0)

Good answer here: https://stackoverflow.com/questions/257418/do-while-0-what-i...

Just to add more context, this is a very common cpp (c pre-processor) idiom. You'll find it in most non-trivial C projects somewhere.

Re: Evolution of the x86 context switch in Linux (2018)

#12
post #4

Enjoyed this article, anybody know the significance of adding the do..while(0) loop within the macro starting Linux 1.3? Was curious if it guards against some C pre-processor issues. /** include/asm-i386/system.h */ #define switch_to(tsk) do { [...] } while (0)

It enables you to invoke the macro as if it were an expression statement consisting of a function call, regardless of where it appears. For details, see http://c-faq.com/cpp/multistmt.html :

In particular, a single statement. I'm sure the link covers it, but:

    if (foo)
      MULTI_LINE_MACRO;
Breaks without some wrapper like if (1) { A; B; } or do { A; B; } while (0):

    if (foo)
      A;
      B;  // oops, unconditional (e.g., "goto fail")

Re: Evolution of the x86 context switch in Linux (2018)

#13
post #11
post #3

Earlier quoted context omitted.

Good answer here: https://stackoverflow.com/questions/257418/do-while-0-what-i...

Just to add more context, this is a very common cpp (c pre-processor) idiom. You'll find it in most non-trivial C projects somewhere.

Abbreviating the C Preprocessor as cpp is very confusing imho.

Re: Evolution of the x86 context switch in Linux (2018)

#14
post #11

Earlier quoted context omitted.

Just to add more context, this is a very common cpp (c pre-processor) idiom. You'll find it in most non-trivial C projects somewhere.

Abbreviating the C Preprocessor as cpp is very confusing imho.

Tell that to CPPFLAGS.

Re: Evolution of the x86 context switch in Linux (2018)

#15
post #8

An addition to "Linux 2.2 (1999)" is: introduced meltdown vulnerability. That was the then-unknown cost of software context switching. Later, with Red Hat's 4g4g kernels that Linus rejected, the problem would go away for people who installed Red Hat's version of the OS on systems with many gigabytes of memory.

Can you elaborate? How does relying on pure TSS for context switching prevent meltdown?

What were the 4g4g kernels? Might you have any literature and/or on those?

Re: Evolution of the x86 context switch in Linux (2018)

#16
post #8

An addition to "Linux 2.2 (1999)" is: introduced meltdown vulnerability. That was the then-unknown cost of software context switching. Later, with Red Hat's 4g4g kernels that Linus rejected, the problem would go away for people who installed Red Hat's version of the OS on systems with many gigabytes of memory.

Can you elaborate? How does relying on pure TSS for context switching prevent meltdown? What were the 4g4g kernels? Might you have any literature and/or on those?

Separate address space for kernel and user. Hardware will use TSS to switch address space as needed for syscalls.

Re: Evolution of the x86 context switch in Linux (2018)

#17
Can someone say why is using the TSS still mandatory with software-based task switching? Is this a requirement imposed by x86?

In looking at the OS dev wiki I see the following:

>"The TSS is primarily suited for hardware multitasking, where each individual process has its own TSS. In Software multitasking, one or two TSS's are also generally used, as they allow for entering Ring 0 code after an interrupt."

Would you not be able to enter Ring0 after an interrupt with a TSS entry? Is this why it is still required?

Re: Evolution of the x86 context switch in Linux (2018)

#18

Can someone say why is using the TSS still mandatory with software-based task switching? Is this a requirement imposed by x86? In looking at the OS dev wiki I see the following: >"The TSS is primarily suited for hardware multitasking, where each individual process has its own TSS. In Software multitasking, one or two TSS's are also generally used, as they allow for entering Ring 0 code after an interrupt." Would you…

The interrupt stack pointer comes from the TSS. Without that you're still running on the untrusted user stack with no way of bootstrapping a kernel context without corrupting the user state.

Re: Evolution of the x86 context switch in Linux (2018)

#19
post #9

I recall reading a paper comparing Linux and Solaris context switch times in ~98 and Linux was 10-100X faster. Solaris did something incredibly slow and safe.

Real context switches on Solaris were very slow which is why they had LWP. But Linux process context switches were also faster than Sun's LWP switches.

Re: Evolution of the x86 context switch in Linux (2018)

#20
post #11

Earlier quoted context omitted.

Just to add more context, this is a very common cpp (c pre-processor) idiom. You'll find it in most non-trivial C projects somewhere.

Abbreviating the C Preprocessor as cpp is very confusing imho.

It's a common abbreviation older than C++. You used to even be able to run the c pre processor on arbitrary non-C files by using the cpp command.
Post reply on HN