Re: [PATCH x86/mm 6/6] x86-64 ia32 ptrace get/putreg32 current task

Linus Torvalds
2007-11-29T19:51:14Z

On Thu, 29 Nov 2007, Andi Kleen wrote:
>
> For i386 iirc Jeremy/Zach did the benchmarking and they settled
> on %fs because it was faster for something (originally it was %gs too)

Hmm. Context switching ends up having to switch the segment that we do
*not* use for the kernel, and the context switching can be faster for the
case where

(a) the segment selector is identical in both source and destination
*AND*
(b) the selector is either zero or points to a GDT entry.

So yes, context switching can be faster for a NUL selector if both old and
new threads use it, and in fact that's the onle case we check right now:

/*
* Restore %gs if needed (which is common)
*/
if (prev->gs | next->gs)
loadsegment(gs, next->gs);

for the 32-bit case. That is faster if "gs" is normally zero, since that
means that we can avoid that loadsegment.

HOWEVER. That is actually not the right (well, "complete") conditional,
since it's only one sub-case of the thing that matters. The right
conditional is probably

/*
* Restore %gs if needed (which is common).
* We can avoid it if they are identical, and
* point to the GDT.
*/
if ((prev->gs ^ next->gs) | (next->gs & 4))
loadsegment(gs, next->gs);

At that point, we would only have to reload stuff if the user actually
uses a local descriptor table entry (which does happen for threaded apps,
I guess, but at least we avoid it for all the common traditional UNIX
processes).

Linus