Skip to contentSkip to conversation

minor cleanups to EFLAGS initialisation in ret_from_fork

PATCH Superseded Ian Campbell 5 participants 12 messages Jul 25, 2011 – Aug 10, 2011 x86

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover minor cleanups to EFLAGS initialisation in ret_from_fork

The following series removes the use of a global kernel_eflags variable from the x86_64 ret_from_fork path and (very slightly) merges the 32 and 64 bit version of that code path.

kernel_eflags could be made a __read_mostly but actually there is no reason to prefer the value at cpu_init() time to a compile time constant value for the initial eflags after a fork.

Ian.

#1 lore raw

PATCH 1/3 x86: drop unnecessary kernel_eflags variable from 64 bit.

On 64 bit x86 we save the current eflags in cpu_init for use in ret_from_fork. Strictly speaking reserved bits in EFLAGS should be read as written but in practise it is unlikely that EFLAGS could ever be extended in this way and the kernel alread clears any undefined flags early on

The equivalent 32 bit code simply hard codes 0x0202 as the new EFLAGS (fixed to use symbolic constants in a subsequent patch).

This change makes 64 bit use the same mechanism to setup the initial EFLAGS on fork. Note that 64 bit resets EFLAGS before calling schedule_tail() as opposed to 32 bit which calls schedule_tail() first. Therefore the correct value for EFLAGS has opposite IF bit. This will be fixed in a subsequent patch.

---
 arch/x86/include/asm/processor.h |    1 -
 arch/x86/kernel/cpu/common.c     |    4 ----
 arch/x86/kernel/entry_64.S       |    2 +-
 3 files changed, 1 insertions(+), 6 deletions(-)
arch/x86/include/asm/processor.h +0 -1
index 2193715..698af88 100644
@@ -398,7 +398,6 @@ DECLARE_INIT_PER_CPU(irq_stack_union);
 
 DECLARE_PER_CPU(char *, irq_stack_ptr);
 DECLARE_PER_CPU(unsigned int, irq_count);
-extern unsigned long kernel_eflags;
 extern asmlinkage void ignore_sysret(void);
 #else	/* X86_64 */
 #ifdef CONFIG_CC_STACKPROTECTOR
arch/x86/kernel/cpu/common.c +0 -4
index 22a073d..178c3b4 100644
@@ -1077,8 +1077,6 @@ void syscall_init(void)
 	       X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|X86_EFLAGS_IOPL);
 }
 
-unsigned long kernel_eflags;
-
 /*
  * Copies of the original ist values from the tss are only accessed during
  * debugging, no special alignment required.
@@ -1231,8 +1229,6 @@ void __cpuinit cpu_init(void)
 	fpu_init();
 	xsave_init();
 
-	raw_local_save_flags(kernel_eflags);
-
 	if (is_uv_system())
 		uv_cpu_init();
 }
arch/x86/kernel/entry_64.S +1 -1
index e13329d..b50d142 100644
@@ -396,7 +396,7 @@ ENTRY(ret_from_fork)
 
 	LOCK ; btr $TIF_FORK,TI_flags(%r8)
 
-	pushq_cfi kernel_eflags(%rip)
+	pushq_cfi $0x0002
 	popfq_cfi				# reset kernel eflags
 
 	call schedule_tail			# rdi: 'prev' task parameter
Signature · 1 line
1.7.2.5
#2 lore raw

PATCH 2/3 x86: make 64 bit ret_from_fork a little more similar to 32 bit

In reply to Ian Campbell

The 64 bit version resets EFLAGS before calling schedule_tail() and
therefore leaves EFLAGS.IF clear. 32 bit resets EFLAGS after calling schedule_tail() and therefore leaves EFLAGS.IF set. I don't think
there is any practical difference between the two approaches since
interrupts are actually reenabled within schedule_tail (schedule_tail->finish_task_switch->finish_lock_switch->raw_spin_unlock_irq->...->local_irq_enable) so arbitrarily pick the 32 bit version and make 64 bit look like that.

---
 arch/x86/kernel/entry_64.S |    7 ++++---
 1 files changed, 4 insertions(+), 3 deletions(-)
arch/x86/kernel/entry_64.S +4 -3
index b50d142..9c28bd5 100644
@@ -394,13 +394,14 @@ END(save_paranoid)
 ENTRY(ret_from_fork)
 	DEFAULT_FRAME
 
-	LOCK ; btr $TIF_FORK,TI_flags(%r8)
 
-	pushq_cfi $0x0002
-	popfq_cfi				# reset kernel eflags
+	LOCK ; btr $TIF_FORK,TI_flags(%r8)
 
 	call schedule_tail			# rdi: 'prev' task parameter
 
+	pushq_cfi $0x0202
+	popfq_cfi				# reset kernel eflags
+
 	GET_THREAD_INFO(%rcx)
 
 	RESTORE_REST
Signature · 1 line
1.7.2.5
#3 lore raw

PATCH 3/3 x86: ret_from_fork: use symbolic contants for bits in EFLAGS

In reply to Ian Campbell
---
 arch/x86/kernel/entry_32.S |    2 +-
 arch/x86/kernel/entry_64.S |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/entry_32.S +1 -1
index 5c1a9197..f032530 100644
@@ -303,7 +303,7 @@ ENTRY(ret_from_fork)
 	call schedule_tail
 	GET_THREAD_INFO(%ebp)
 	popl_cfi %eax
-	pushl_cfi $0x0202		# Reset kernel eflags
+	pushl_cfi $(X86_EFLAGS_IF|0x2)	# Reset kernel eflags
 	popfl_cfi
 	jmp syscall_exit
 	CFI_ENDPROC
arch/x86/kernel/entry_64.S +1 -1
index 9c28bd5..4c76abf 100644
@@ -399,7 +399,7 @@ ENTRY(ret_from_fork)
 
 	call schedule_tail			# rdi: 'prev' task parameter
 
-	pushq_cfi $0x0202
+	pushq_cfi $(X86_EFLAGS_IF|0x2)
 	popfq_cfi				# reset kernel eflags
 
 	GET_THREAD_INFO(%rcx)
Signature · 1 line
1.7.2.5
#4 lore raw
In reply to Ian Campbell
Quoted 10 lines · Ian Campbell

The following series removes the use of a global kernel_eflags variable from the x86_64 ret_from_fork path and (very slightly) merges the 32 and 64 bit version of that code path.

kernel_eflags could be made a __read_mostly but actually there is no reason to prefer the value at cpu_init() time to a compile time constant value for the initial eflags after a fork.

Ian.

Thanks, Ian! I think noone against this simplification, Peter, Andi?

Cyrill

#5 lore raw
Quoted 9 lines · Cyrill Gorcunov
Quoted 8 lines · Ian Campbell

The following series removes the use of a global kernel_eflags variable from the x86_64 ret_from_fork path and (very slightly) merges the 32 and 64 bit version of that code path.

kernel_eflags could be made a __read_mostly but actually there is no reason to prefer the value at cpu_init() time to a compile time constant value for the initial eflags after a fork.

Quoted 6 lines
Quoted 2 lines

Ian.

Thanks, Ian! I think noone against this simplification, Peter, Andi?

Cyrill

Ian, I've missed in first place that you've opened IRQs window _before_ schedule_tail() call, ie it's not 1:1 code mapping as it was before.

Note kernel_eflags has IF clear and what we have: the ret_from_fork on x86-64 happens _only_ inside context_switch call, ie

schedule (sched.c)

        ...
        raw_spin_lock_irq
        ...
        context_switch
                switch_to
                        "jnz   ret_from_fork\n\t"
                        pushq_cfi kernel_eflags(%rip)
                        popfq_cfi                               # reset kernel eflags

---> irqs are still disabled

call schedule_tail # rdi: 'prev' task parameter
finish_lock_switch
raw_spin_unlock_irq

I bet raw_spin_lock_irq at the beginning of the schedule() is set for a reason and such change is not safe. Though I may be missing something again...

Cyrill

#6 lore raw
Quoted 35 lines · Cyrill Gorcunov
Quoted 9 lines · Cyrill Gorcunov
Quoted 8 lines · Ian Campbell

The following series removes the use of a global kernel_eflags variable from the x86_64 ret_from_fork path and (very slightly) merges the 32 and 64 bit version of that code path.

kernel_eflags could be made a __read_mostly but actually there is no reason to prefer the value at cpu_init() time to a compile time constant value for the initial eflags after a fork.

Quoted 6 lines
Quoted 2 lines

Ian.

Thanks, Ian! I think noone against this simplification, Peter, Andi?

Cyrill

Ian, I've missed in first place that you've opened IRQs window _before_ schedule_tail() call, ie it's not 1:1 code mapping as it was before.

Note kernel_eflags has IF clear and what we have: the ret_from_fork on x86-64 happens _only_ inside context_switch call, ie

schedule (sched.c)

        ...
        raw_spin_lock_irq
        ...
        context_switch
                switch_to
                        "jnz   ret_from_fork\n\t"
                        pushq_cfi kernel_eflags(%rip)
                        popfq_cfi                               # reset kernel eflags

---> irqs are still disabled

Quoted 8 lines

call schedule_tail # rdi: 'prev' task parameter
finish_lock_switch
raw_spin_unlock_irq

I bet raw_spin_lock_irq at the beginning of the schedule() is set for a reason and such change is not safe. Though I may be missing something again...

This definitely doesn't look "obviously safe" to me. However, does anyone see a problem with unconditionally leaving IF disabled even on 32 bits (I haven't traced all the paths yet), i.e. doing the *opposite* of Ian's patch #2?

-hpa

#7 lore raw
Quoted 45 lines · H. Peter Anvin
Quoted 35 lines · Cyrill Gorcunov
Quoted 9 lines · Cyrill Gorcunov
Quoted 8 lines · Ian Campbell

The following series removes the use of a global kernel_eflags variable from the x86_64 ret_from_fork path and (very slightly) merges the 32 and 64 bit version of that code path.

kernel_eflags could be made a __read_mostly but actually there is no reason to prefer the value at cpu_init() time to a compile time constant value for the initial eflags after a fork.

Quoted 6 lines
Quoted 2 lines

Ian.

Thanks, Ian! I think noone against this simplification, Peter, Andi?

Cyrill

Ian, I've missed in first place that you've opened IRQs window _before_ schedule_tail() call, ie it's not 1:1 code mapping as it was before.

Note kernel_eflags has IF clear and what we have: the ret_from_fork on x86-64 happens _only_ inside context_switch call, ie

schedule (sched.c)

        ...
        raw_spin_lock_irq
        ...
        context_switch
                switch_to
                        "jnz   ret_from_fork\n\t"
                        pushq_cfi kernel_eflags(%rip)
                        popfq_cfi                               # reset kernel eflags

---> irqs are still disabled

Quoted 8 lines

call schedule_tail # rdi: 'prev' task parameter
finish_lock_switch
raw_spin_unlock_irq

I bet raw_spin_lock_irq at the beginning of the schedule() is set for a reason and such change is not safe. Though I may be missing something again...

Quoted 7 lines

This definitely doesn't look "obviously safe" to me. However, does anyone see a problem with unconditionally leaving IF disabled even on 32 bits (I haven't traced all the paths yet), i.e. doing the *opposite* of Ian's patch #2?

-hpa

On x86-32 it seems to be similar (not identical in calls though)

copy_thread()

	p->thread.ip = (unsigned long)ret_from_fork;

and the task get queued into tasks queue, but later when switch_to happens irqs are blocked at ret_from_fork call. I better poke PeterZ here /CC'ed/ ;)

Cyrill

#8 lore raw
Quoted 21 lines · Cyrill Gorcunov
Quoted 21 lines
Quoted 20 lines

schedule (sched.c)

        ...
        raw_spin_lock_irq
        ...
        context_switch
                switch_to
                        "jnz   ret_from_fork\n\t"
                        pushq_cfi kernel_eflags(%rip)
                        popfq_cfi                               # reset kernel eflags

---> irqs are still disabled

call schedule_tail # rdi: 'prev' task parameter
finish_lock_switch
raw_spin_unlock_irq

I bet raw_spin_lock_irq at the beginning of the schedule() is set for a reason and such change is not safe. Though I may be missing something again...

Quoted 4 lines
Quoted 4 lines

This definitely doesn't look "obviously safe" to me. However, does anyone see a problem with unconditionally leaving IF disabled even on 32 bits (I haven't traced all the paths yet), i.e. doing the *opposite* of Ian's patch #2?

Right, enabling IRQs there isn't cool, currently there's still __ARCH_WANT_INTERRUPTS_ON_CTXSW but we're working hard on getting rid of that nightmare.

There's a number of very subtle things that can go wrong when you enable interrupts over the context switch.

Leaving IRQs disabled should be the right thing, on x86 we should _never_ have interrupts enabled there.

#9 lore raw
Quoted 31 lines · Peter Zijlstra
Quoted 21 lines · Cyrill Gorcunov
Quoted 21 lines
Quoted 20 lines

schedule (sched.c)

        ...
        raw_spin_lock_irq
        ...
        context_switch
                switch_to
                        "jnz   ret_from_fork\n\t"
                        pushq_cfi kernel_eflags(%rip)
                        popfq_cfi                               # reset kernel eflags

---> irqs are still disabled

call schedule_tail # rdi: 'prev' task parameter
finish_lock_switch
raw_spin_unlock_irq

I bet raw_spin_lock_irq at the beginning of the schedule() is set for a reason and such change is not safe. Though I may be missing something again...

Quoted 4 lines
Quoted 4 lines

This definitely doesn't look "obviously safe" to me. However, does anyone see a problem with unconditionally leaving IF disabled even on 32 bits (I haven't traced all the paths yet), i.e. doing the *opposite* of Ian's patch #2?

Right, enabling IRQs there isn't cool, currently there's still __ARCH_WANT_INTERRUPTS_ON_CTXSW but we're working hard on getting rid of that nightmare.

Quoted 5 lines

There's a number of very subtle things that can go wrong when you enable interrupts over the context switch.

Leaving IRQs disabled should be the right thing, on x86 we should _never_ have interrupts enabled there.

Thanks Peter. I'll flip the (not any longer) "arbitrary" decision in my #2 patch and repost when I return from debconf/vacation.

Ian.

Quoted 6 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#10 lore raw
In reply to Peter Zijlstra
Quoted 31 lines · Peter Zijlstra
Quoted 21 lines · Cyrill Gorcunov
Quoted 21 lines
Quoted 20 lines

schedule (sched.c)

        ...
        raw_spin_lock_irq
        ...
        context_switch
                switch_to
                        "jnz   ret_from_fork\n\t"
                        pushq_cfi kernel_eflags(%rip)
                        popfq_cfi                               # reset kernel eflags

---> irqs are still disabled

call schedule_tail # rdi: 'prev' task parameter
finish_lock_switch
raw_spin_unlock_irq

I bet raw_spin_lock_irq at the beginning of the schedule() is set for a reason and such change is not safe. Though I may be missing something again...

Quoted 4 lines
Quoted 4 lines

This definitely doesn't look "obviously safe" to me. However, does anyone see a problem with unconditionally leaving IF disabled even on 32 bits (I haven't traced all the paths yet), i.e. doing the *opposite* of Ian's patch #2?

Right, enabling IRQs there isn't cool, currently there's still __ARCH_WANT_INTERRUPTS_ON_CTXSW but we're working hard on getting rid of that nightmare.

Quoted 5 lines

There's a number of very subtle things that can go wrong when you enable interrupts over the context switch.

Leaving IRQs disabled should be the right thing, on x86 we should _never_ have interrupts enabled there.

Just getting back to this after my vac, sorry for the delay.

raw_spin_unlock_irq unconditionally re-enables interrupts so I don't really see what I've changed since interrupts are enabled by schedule_tail and I've moved (on 64 bit) the EFLAGS reset to after schedule_tail, so it should have interrupts enabled at that point already and so they should remain enabled. Or are you suggesting that things were already wrong?

However I've switched the order of my second patch anyway, so EFLAGS is reset to 0x0002 (interrupts disabled) on both 32- and 64-bit before the call to schedule_tail, since it does seem like the simpler option. I'll repost shortly.

Ian.

#11 lore raw
Quoted 6 lines · Ian Campbell

raw_spin_unlock_irq unconditionally re-enables interrupts so I don't really see what I've changed since interrupts are enabled by schedule_tail and I've moved (on 64 bit) the EFLAGS reset to after schedule_tail, so it should have interrupts enabled at that point already and so they should remain enabled. Or are you suggesting that things were already wrong?

Ah, I thought this was about the EFLAGS state of new tasks which would get into effect at switch_to(), which is well before we enable interrupts in finish_lock_switch().

Quoted 4 lines

However I've switched the order of my second patch anyway, so EFLAGS is reset to 0x0002 (interrupts disabled) on both 32- and 64-bit before the call to schedule_tail, since it does seem like the simpler option. I'll repost shortly.

OK.