get_current is __pure__, maybe __const__ even
Quoted 3 lines · Andi Kleen
Please CSE "current" manually. It generates
much better code on some architectures
because the compiler cannot do it for you.
This looks fixable.
At the very least, __attribute__((__pure__)) will apply to your get_current function.
I think __attribute__((__const__)) will too, even though it's technically against the documentation. While you do indeed read from memory, you don't read from memory that could be seen as changing. Nothing done during the lifetime of a task will change "current" as viewed from within that task.
Quoted 11 lines · Albert Cahalan
Quoted 3 lines · Andi Kleen
Please CSE "current" manually. It generates
much better code on some architectures
because the compiler cannot do it for you.This looks fixable.
At the very least, __attribute__((__pure__)) will apply to your
get_currentfunction.
Quoted 7 lines
I think __attribute__((__const__)) will too, even though it's technically against the documentation. While you do indeed read from memory, you don't read from memory that could be seen as changing. Nothing done during the lifetime of a task will change "current" as viewed from within that task.
current will certainly change in schedule (), so either you'd need to avoid using current in schedule() and use some other accessor for the same without such attribute, or #ifdef the attribute out when compiling sched.c.
Jakub
Quoted 3 lines · Andi Kleen
Quoted 3 lines
Please CSE "current" manually. It generates
much better code on some architectures
because the compiler cannot do it for you.
Quoted 10 lines · Albert Cahalan
This looks fixable.
At the very least, __attribute__((__pure__)) will apply to yourget_currentfunction. I think __attribute__((__const__)) will too, even though it's technically against the documentation. While you do indeed read from memory, you don't read from memory that could be seen as changing. Nothing done during the lifetime of a task will change "current" as viewed from within that task.
The fix needs to be made to gcc, not the kernel.
-- wli
Quoted 10 lines · Albert Cahalan
Quoted 10 lines
This looks fixable.
At the very least, __attribute__((__pure__)) will apply to yourget_currentfunction. I think __attribute__((__const__)) will too, even though it's technically against the documentation. While you do indeed read from memory, you don't read from memory that could be seen as changing. Nothing done during the lifetime of a task will change "current" as viewed from within that task.
Quoted 5 lines · Jakub Jelinek
current will certainly change in schedule (), so either you'd need to avoid using current in
schedule()and use some other accessor for the same without such attribute, or #ifdef the attribute out when compilingsched.c.
Why would barrier() not suffice?
-- wli
Quoted 12 lines · William Lee Irwin III
Quoted 10 lines · Albert Cahalan
Quoted 10 lines
This looks fixable.
At the very least, __attribute__((__pure__)) will apply to yourget_currentfunction. I think __attribute__((__const__)) will too, even though it's technically against the documentation. While you do indeed read from memory, you don't read from memory that could be seen as changing. Nothing done during the lifetime of a task will change "current" as viewed from within that task.
Quoted 2 lines
Quoted 1 line · Jakub Jelinek
current will certainly change in schedule (),
Not really!
Quoted 1 line
From the viewpoint of a single task looking
at current, it does not change. The task is paused, and may well start up again on a different CPU, but current doesn't change.
Any state gcc might keep would be stored on the kernel stack or in a register, which will be preserved because tasks don't share these.
AFAIK, gcc generates thread-safe code. It won't convert code to something like this:
int foo(int bar){
static task_struct *__L131241 = get_current(); // blah, blah...
}
Quoted 6 lines
Quoted 4 lines
so either you'd need to avoid using current
inschedule()and use some other accessor
for the same without such attribute, or
#ifdef the attribute out when compilingsched.c.Why would
barrier()not suffice?
I don't think even barrier() is needed. Suppose gcc were to cache the value of current over a schedule. Who cares? It'll be the same after schedule() as it was before.
Quoted 1 line · Jakub Jelinek
Quoted 1 line
Quoted 1 line
current will certainly change in schedule (),
Quoted 1 line · Albert Cahalan
Not really!
Yes it does. The interior of schedule() is C and must be compiled also.
Quoted 1 line · At some point in the past, I wrote:
Quoted 1 line
Why would
barrier()not suffice?
Quoted 5 lines · Albert Cahalan
I don't think even
barrier()is needed. Suppose gcc were to cache the value of current over a schedule. Who cares? It'll be the same afterschedule()as it was before.
Not over a call to schedule(). In the midst of schedule().
-- wli
Quoted 11 lines · William Lee Irwin III
Quoted 5 lines · Albert Cahalan
I don't think even
barrier()is needed. Suppose gcc were to cache the value of current over a schedule. Who cares? It'll be the same afterschedule()as it was before.Not over a call to
schedule(). In the midst ofschedule().
In a way, it is. Because after context_switch, the stack and registers have been replaced by the new task. So if current was cached somewhere before that task had scheduled off, then it still would be correct now that it is scheduled back on.
At points *within* context_switch, current won't be right, but AFAIKS current is never used in there.
Quoted 3 lines · Albert Cahalan
Quoted 3 lines
Quoted 3 lines
I don't think even
barrier()is needed. Suppose gcc were to cache the value of current over a schedule. Who cares? It'll be the same afterschedule()as it was before.
Quoted 1 line · William Lee Irwin III
Quoted 1 line
Not over a call to
schedule(). In the midst ofschedule().
Quoted 7 lines · Nick Piggin
In a way, it is. Because after
context_switch, the stack and registers have been replaced by the new task. So if current was cached somewhere before that task had scheduled off, then it still would be correct now that it is scheduled back on.
At points *within*context_switch, current won't be right, but AFAIKS current is never used in there.
We have a clobber in inline asm and a manual barrier() so we don't need to change anything if current were to be properly CSE'd. It's somewhat academic; if current were (mis)used there, it would change.
-- wli
Quoted 7 lines · William Lee Irwin III
Quoted 1 line · Jakub Jelinek
Quoted 1 line
Quoted 1 line
current will certainly change in schedule (),
Quoted 1 line · Albert Cahalan
Not really!
Yes it does. The interior of
schedule()is C and must be compiled also.
Sure. It doesn't matter. The part that matters is all arch-specific assembly.
Hey, the arm port already uses __const__. Unless the arm port is broken, there's proof.
Quoted 11 lines
Quoted 1 line · At some point in the past, I wrote:
Quoted 1 line
Why would
barrier()not suffice?Quoted 5 lines · Albert Cahalan
I don't think even
barrier()is needed. Suppose gcc were to cache the value of current over a schedule. Who cares? It'll be the same afterschedule()as it was before.Not over a call to
schedule(). In the midst ofschedule().
OK, let's look.
First, there's fork/vfork/clone. At no point does "current" change. A process comes into existance with a ready-made current.
Second, there's sched.c with context_switch(). That does everything via switch_to, like so:
/* Here we just switch the register state and the stack. */ switch_to(prev, next, prev);
No problem. Now I only need to show that switch_to() is safe. Unfortunately, it's arch-specific code. I'll look at a few examples...
x86_64: assembly, and thus OK i386: assembly, and thus OK ppc: assembly, and thus OK arm: already uses __const__ :-)
To find a problem, you need to find an arch which runs C code with current being inconsistent with the stack or registers. That would be wild and evil. In any case, adding __attribute__((__const__)) is an arch-specific change. A truly evil arch running C code with an inconsistent current can just leave off the attribute or, better yet, stop being evil.
Quoted 1 line · William Lee Irwin III
Quoted 1 line
Not over a call to
schedule(). In the midst ofschedule().
Quoted 11 lines · Albert Cahalan
OK, let's look.
First, there's fork/vfork/clone. At no point does "current" change. A process comes into existance with a ready-made current.
Second, there'ssched.cwithcontext_switch(). That does everything viaswitch_to, like so:
/* Here we just switch the register state and the stack. */switch_to(prev, next, prev);
No problem. Now I only need to show thatswitch_to()is safe. Unfortunately, it's arch-specific code. I'll look at a few examples...
gcc has to compile more than Linux. There has to be a rule for how this works, not "gee, Linux will still work and get faster if we change this".
-- wli
Quoted 12 lines · William Lee Irwin III
Quoted 1 line · Jakub Jelinek
Quoted 1 line
Quoted 1 line
current will certainly change in schedule (),
Quoted 1 line · Albert Cahalan
Not really!
Yes it does. The interior of
schedule()is C and must be compiled also.Quoted 1 line · At some point in the past, I wrote:
Quoted 1 line
Why would
barrier()not suffice?
Quoted 8 lines
Quoted 5 lines · Albert Cahalan
I don't think even
barrier()is needed. Suppose gcc were to cache the value of current over a schedule. Who cares? It'll be the same afterschedule()as it was before.Not over a call to
schedule(). In the midst ofschedule().
Actually, I find myself agreeing with Albert here. Consider the following points:
- "current_thread()" depends on the kernel stack pointer.
- the kernel stack pointer is changed when we switch threads.
- the rest of the register set is changed when we switch threads.
Therefore, if we have current_thread() cached in a register, and we away from thread A to thread B, and back to A, has the cached copy become invalid for thread A ? No.
Now look at the same thing from thread B's perspective. Has anything changed because thread A has run? No.
IOW, think from a tasks point of view. It gets into the scheduler, and switch_to() is just a normal function which just happens to sleep for some time.
Signature · 4 lines
Russell King
Linux kernel 2.6 ARM Linux - http://www.arm.linux.org.uk/
maintainer of: 2.6 PCMCIA - http://pcmcia.arm.linux.org.uk/
2.6 Serial coreQuoted 3 lines
IOW, think from a tasks point of view. It gets into the scheduler, and
switch_to()is just a normal function which just happens to sleep for some time.
On x86/x86-64 the stack switch is inlined into schedule()
-Andi
Quoted 5 lines · Andi Kleen
Quoted 3 lines
IOW, think from a tasks point of view. It gets into the scheduler, and
switch_to()is just a normal function which just happens to sleep for some time.On x86/x86-64 the stack switch is inlined into
schedule()
Yes, and does it not save the whole CPU context and restore it, or tell the compiler that certain registers which you don't preserve are clobbered? If it didn't, I think you'd find that you have a bug there.
The scheduler quite rightly expects, for any thread, that any variable which may be stored in a CPU register before the context switch has the same value as after the context switch.
(note - "preserve" above - I don't mean from one thread to another, I mean preserved within the context of one thread.)
Signature · 4 lines
Russell King
Linux kernel 2.6 ARM Linux - http://www.arm.linux.org.uk/
maintainer of: 2.6 PCMCIA - http://pcmcia.arm.linux.org.uk/
2.6 Serial coreQuoted 3 lines
The scheduler quite rightly expects, for any thread, that any variable which may be stored in a CPU register before the context switch has the same value as after the context switch.
Just current isn't a varible, it's some inline assembly in a inline function. The question was if that function could be marked const/pure.
-Andi
Quoted 6 lines · Andi Kleen
Quoted 3 lines
The scheduler quite rightly expects, for any thread, that any variable which may be stored in a CPU register before the context switch has the same value as after the context switch.
Just current isn't a varible, it's some inline assembly in a inline function. The question was if that function could be marked const/pure.
Sigh. What does that matter?
current_thread, which is what we should be talking about here, is indeed a function, but its returned value depends wholely on the stack pointer value.
Since the stack pointer and thread info are invariably coupled together, it is right to mark it const/pure.
Signature · 4 lines
Russell King
Linux kernel 2.6 ARM Linux - http://www.arm.linux.org.uk/
maintainer of: 2.6 PCMCIA - http://pcmcia.arm.linux.org.uk/
2.6 Serial core