Skip to contentSkip to conversation

x86: stop using PER_CPU_VAR(kernel_stack)

PATCH In review Denys Vlasenko 4 participants 11 messages Mar 21, 2015 – Apr 24, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 x86: stop using PER_CPU_VAR(kernel_stack)

Instead of PER_CPU_VAR(kernel_stack), 64-bit code
can use PER_CPU_VAR(cpu_tss + TSS_sp0).

32-bit code switches to PER_CPU_VAR(cpu_current_top_of_stack), which has the same value.

CC Linus Torvalds <[email protected]>
CC Steven Rostedt <[email protected]>
CC Ingo Molnar <[email protected]>
CC Borislav Petkov <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Alexei Starovoitov <[email protected]>
CC Will Drewry <[email protected]>
CC Kees Cook <[email protected]>

---

Andy, this patchset goes on top of PUSH conversion patches

 arch/x86/ia32/ia32entry.S          | 2 +-
 arch/x86/include/asm/thread_info.h | 8 +++++++-
 arch/x86/kernel/entry_64.S         | 2 +-
 arch/x86/xen/xen-asm_64.S          | 5 +++--
 4 files changed, 12 insertions(+), 5 deletions(-)
arch/x86/ia32/ia32entry.S +1 -1
index acbff3f..0a791c5 100644
@@ -317,7 +317,7 @@ ENTRY(ia32_cstar_target)
 	SWAPGS_UNSAFE_STACK
 	movl	%esp,%r8d
 	CFI_REGISTER	rsp,r8
-	movq	PER_CPU_VAR(kernel_stack),%rsp
+	movq	PER_CPU_VAR(cpu_tss + TSS_sp0),%rsp
 	/*
 	 * No need to follow this irqs on/off section: the syscall
 	 * disabled irqs and here we enable it straight after entry:
arch/x86/include/asm/thread_info.h +7 -1
index 89b814e..5be18d1 100644
@@ -201,9 +201,15 @@ static inline unsigned long current_stack_pointer(void)
 #else /* !__ASSEMBLY__ */
 
 /* Load thread_info address into "reg" */
+#ifdef CONFIG_X86_32
 #define GET_THREAD_INFO(reg) \
-	_ASM_MOV PER_CPU_VAR(kernel_stack),reg ; \
+	_ASM_MOV PER_CPU_VAR(cpu_current_top_of_stack),reg ; \
 	_ASM_SUB $(THREAD_SIZE),reg ;
+#else
+#define GET_THREAD_INFO(reg) \
+	_ASM_MOV PER_CPU_VAR(cpu_tss + TSS_sp0),reg ; \
+	_ASM_SUB $(THREAD_SIZE),reg ;
+#endif
 
 /*
  * ASM operand which evaluates to thread_info address
arch/x86/kernel/entry_64.S +1 -1
index 43e7333..e5b5283 100644
@@ -220,7 +220,7 @@ ENTRY(system_call)
 GLOBAL(system_call_after_swapgs)
 
 	movq	%rsp,PER_CPU_VAR(rsp_scratch)
-	movq	PER_CPU_VAR(kernel_stack),%rsp
+	movq	PER_CPU_VAR(cpu_tss + TSS_sp0),%rsp
 
 	/* Construct struct pt_regs on stack */
 	pushq_cfi $__USER_DS			/* pt_regs->ss */
arch/x86/xen/xen-asm_64.S +3 -2
index 985fc3e..acc49e0 100644
@@ -15,6 +15,7 @@
 #include <asm/percpu.h>
 #include <asm/processor-flags.h>
 #include <asm/segment.h>
+#include <asm/asm-offsets.h>
 
 #include <xen/interface/xen.h>
 
@@ -69,7 +70,7 @@ ENTRY(xen_sysret64)
 	 * still with the kernel gs, so we can easily switch back
 	 */
 	movq %rsp, PER_CPU_VAR(rsp_scratch)
-	movq PER_CPU_VAR(kernel_stack), %rsp
+	movq PER_CPU_VAR(cpu_tss + TSS_sp0), %rsp
 
 	pushq $__USER_DS
 	pushq PER_CPU_VAR(rsp_scratch)
@@ -88,7 +89,7 @@ ENTRY(xen_sysret32)
 	 * still with the kernel gs, so we can easily switch back
 	 */
 	movq %rsp, PER_CPU_VAR(rsp_scratch)
-	movq PER_CPU_VAR(kernel_stack), %rsp
+	movq PER_CPU_VAR(cpu_tss + TSS_sp0), %rsp
 
 	pushq $__USER32_DS
 	pushq PER_CPU_VAR(rsp_scratch)
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH 2/2 x86: remove unused kernel_stack per-cpu variable

CC Linus Torvalds <[email protected]>
CC Steven Rostedt <[email protected]>
CC Ingo Molnar <[email protected]>
CC Borislav Petkov <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Alexei Starovoitov <[email protected]>
CC Will Drewry <[email protected]>
CC Kees Cook <[email protected]>
---
 arch/x86/include/asm/thread_info.h | 2 --
 arch/x86/kernel/cpu/common.c       | 4 ----
 arch/x86/kernel/process_32.c       | 5 +----
 arch/x86/kernel/process_64.c       | 3 ---
 arch/x86/kernel/smpboot.c          | 2 --
 arch/x86/xen/smp.c                 | 3 ---
 6 files changed, 1 insertion(+), 18 deletions(-)
arch/x86/include/asm/thread_info.h +0 -2
index 5be18d1..89f7570 100644
@@ -180,8 +180,6 @@ struct thread_info {
  */
 #ifndef __ASSEMBLY__
 
-DECLARE_PER_CPU(unsigned long, kernel_stack);
-
 static inline struct thread_info *current_thread_info(void)
 {
 	return (struct thread_info *)(current_top_of_stack() - THREAD_SIZE);
arch/x86/kernel/cpu/common.c +0 -4
index 9461c83..4d52ef5 100644
@@ -1135,10 +1135,6 @@ static __init int setup_disablecpuid(char *arg)
 }
 __setup("clearcpuid=", setup_disablecpuid);
 
-DEFINE_PER_CPU(unsigned long, kernel_stack) =
-	(unsigned long)&init_thread_union + THREAD_SIZE;
-EXPORT_PER_CPU_SYMBOL(kernel_stack);
-
 #ifdef CONFIG_X86_64
 struct desc_ptr idt_descr = { NR_VECTORS * 16 - 1, (unsigned long) idt_table };
 struct desc_ptr debug_idt_descr = { NR_VECTORS * 16 - 1,
arch/x86/kernel/process_32.c +1 -4
index 5a4c2f8..1a7bf8f 100644
@@ -306,13 +306,10 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
 	arch_end_context_switch(next_p);
 
 	/*
-	 * Reload esp0, kernel_stack, and current_top_of_stack.  This changes
+	 * Reload esp0 and cpu_current_top_of_stack.  This changes
 	 * current_thread_info().
 	 */
 	load_sp0(tss, next);
-	this_cpu_write(kernel_stack,
-		       (unsigned long)task_stack_page(next_p) +
-		       THREAD_SIZE);
 	this_cpu_write(cpu_current_top_of_stack,
 		       (unsigned long)task_stack_page(next_p) +
 		       THREAD_SIZE);
arch/x86/kernel/process_64.c +0 -3
index db49063..ae3dcd8 100644
@@ -408,9 +408,6 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
 	/* Reload esp0 and ss1.  This changes current_thread_info(). */
 	load_sp0(tss, next);
 
-	this_cpu_write(kernel_stack,
-		(unsigned long)task_stack_page(next_p) + THREAD_SIZE);
-
 	/*
 	 * Now maybe reload the debug registers and handle I/O bitmaps
 	 */
arch/x86/kernel/smpboot.c +0 -2
index 7b20ffd..c9647f4 100644
@@ -812,8 +812,6 @@ static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle)
 	clear_tsk_thread_flag(idle, TIF_FORK);
 	initial_gs = per_cpu_offset(cpu);
 #endif
-	per_cpu(kernel_stack, cpu) =
-		(unsigned long)task_stack_page(idle) + THREAD_SIZE;
 	early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
 	initial_code = (unsigned long)start_secondary;
 	stack_start  = idle->thread.sp;
arch/x86/xen/smp.c +0 -3
index 765b768..a2c7e8b 100644
@@ -451,9 +451,6 @@ static int xen_cpu_up(unsigned int cpu, struct task_struct *idle)
 #else
 	clear_tsk_thread_flag(idle, TIF_FORK);
 #endif
-	per_cpu(kernel_stack, cpu) =
-		(unsigned long)task_stack_page(idle) + THREAD_SIZE;
-
 	xen_setup_runstate_info(cpu);
 	xen_setup_timer(cpu);
 	xen_init_lock_cpu(cpu);
Signature · 1 line
1.8.1.4
#2 lore raw
In reply to Denys Vlasenko
Quoted 2 lines · On Sat, 21 Mar 2015 21:44:37 +0100 Denys Vlasenko <[email protected]> wrote:

Instead of PER_CPU_VAR(kernel_stack), 64-bit code
can use PER_CPU_VAR(cpu_tss + TSS_sp0).

The change log here is lacking an answer to "why". It only states what it does. What's wrong with using kernel_stack? The change log should explicitly state that. I have no idea why this patch is needed.

-- Steve

Quoted 4 lines

32-bit code switches to PER_CPU_VAR(cpu_current_top_of_stack), which has the same value.

#3 lore raw
Quoted 9 lines · Steven Rostedt
Quoted 2 lines · On Sat, 21 Mar 2015 21:44:37 +0100 Denys Vlasenko <[email protected]> wrote:

Instead of PER_CPU_VAR(kernel_stack), 64-bit code
can use PER_CPU_VAR(cpu_tss + TSS_sp0).

The change log here is lacking an answer to "why". It only states what it does. What's wrong with using kernel_stack? The change log should explicitly state that. I have no idea why this patch is needed.

Sorry. The reason is:

We want to get rid of kernel_stack, since it is redundant:
in 64-bits, PER_CPU_VAR(cpu_tss + TSS_sp0) can be used instead, in 32-bits, PER_CPU_VAR(cpu_current_top_of_stack) can be used instead.

Patch 2/2 in the same series removes kernel_stack.

maintainer
#4 lore raw
Quoted 3 lines · Denys Vlasenko

We want to get rid of kernel_stack, since it is redundant:
in 64-bits, PER_CPU_VAR(cpu_tss + TSS_sp0) can be used instead, in 32-bits, PER_CPU_VAR(cpu_current_top_of_stack) can be used instead.

We probably should have a macro unifying those on both bitness. Especially 64-bit is really non-telling - I can't really deduce from "cpu_tss + TSS_sp0" that it is the kernel stack ptr.

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--
#5 lore raw
In reply to Denys Vlasenko
Quoted 11 lines · On Mon, 23 Mar 2015 18:10:01 +0100 Denys Vlasenko <[email protected]> wrote:
Quoted 9 lines · Steven Rostedt
Quoted 2 lines · On Sat, 21 Mar 2015 21:44:37 +0100 Denys Vlasenko <[email protected]> wrote:

Instead of PER_CPU_VAR(kernel_stack), 64-bit code
can use PER_CPU_VAR(cpu_tss + TSS_sp0).

The change log here is lacking an answer to "why". It only states what it does. What's wrong with using kernel_stack? The change log should explicitly state that. I have no idea why this patch is needed.

Quoted 5 lines

Sorry. The reason is:

We want to get rid of kernel_stack, since it is redundant:
in 64-bits, PER_CPU_VAR(cpu_tss + TSS_sp0) can be used instead, in 32-bits, PER_CPU_VAR(cpu_current_top_of_stack) can be used instead.

Can we do a:

#define cpu_current_top_of_stack (cpu_tss + TSS_sp0)

in 64-bit, and make it consistent with i386.

"cpu_tss + TSS_sp0" is rather meaningless. "kernel_stack" or "top_of_stack" is at least informative.

Quoted 2 lines

Patch 2/2 in the same series removes kernel_stack.

Understood, but each commit's change log should be able to stand on its own.

-- Steve

#6 lore raw
Quoted 16 lines · Steven Rostedt
Quoted 13 lines · On Mon, 23 Mar 2015 18:10:01 +0100 Denys Vlasenko <[email protected]> wrote:
Quoted 9 lines · Steven Rostedt
Quoted 2 lines · On Sat, 21 Mar 2015 21:44:37 +0100 Denys Vlasenko <[email protected]> wrote:

Instead of PER_CPU_VAR(kernel_stack), 64-bit code
can use PER_CPU_VAR(cpu_tss + TSS_sp0).

The change log here is lacking an answer to "why". It only states what it does. What's wrong with using kernel_stack? The change log should explicitly state that. I have no idea why this patch is needed.

Sorry. The reason is:

Quoted 7 lines
Quoted 3 lines

We want to get rid of kernel_stack, since it is redundant:
in 64-bits, PER_CPU_VAR(cpu_tss + TSS_sp0) can be used instead, in 32-bits, PER_CPU_VAR(cpu_current_top_of_stack) can be used instead.

Can we do a:

#define cpu_current_top_of_stack (cpu_tss + TSS_sp0)

We already do something similar:

static inline unsigned long current_top_of_stack(void) {

#ifdef CONFIG_X86_64
        return this_cpu_read_stable(cpu_tss.x86_tss.sp0);
#else
        /* sp0 on x86_32 is special in and around vm86 mode. */
        return this_cpu_read_stable(cpu_current_top_of_stack);

#endif
}

#7 lore raw
Quoted 8 lines · On Mon, 23 Mar 2015 18:38:09 +0100 Denys Vlasenko <[email protected]> wrote:
Quoted 3 lines

Can we do a:

#define cpu_current_top_of_stack (cpu_tss + TSS_sp0)

We already do something similar:

static inline unsigned long current_top_of_stack(void) {

Quoted 7 lines
#ifdef CONFIG_X86_64
        return this_cpu_read_stable(cpu_tss.x86_tss.sp0);
#else
        /* sp0 on x86_32 is special in and around vm86 mode. */
        return this_cpu_read_stable(cpu_current_top_of_stack);

#endif
}

So can we then have:

#ifdef __ASSEMBLY__

# define cpu_current_top_of_stack (cpu_tss + TSS_sp0)
#else
# define cpu_current_top_of_stack (cpu_tss.x86_tss.sp0)
#endif

And get rid of that if statement in the static inline?

-- Steve

#8 lore raw
Quoted 21 lines · Steven Rostedt
Quoted 8 lines · On Mon, 23 Mar 2015 18:38:09 +0100 Denys Vlasenko <[email protected]> wrote:
Quoted 3 lines

Can we do a:

#define cpu_current_top_of_stack (cpu_tss + TSS_sp0)

We already do something similar:

static inline unsigned long current_top_of_stack(void) {

Quoted 7 lines
#ifdef CONFIG_X86_64
        return this_cpu_read_stable(cpu_tss.x86_tss.sp0);
#else
        /* sp0 on x86_32 is special in and around vm86 mode. */
        return this_cpu_read_stable(cpu_current_top_of_stack);

#endif
}

So can we then have:

Quoted 7 lines

#ifdef __ASSEMBLY__

# define cpu_current_top_of_stack (cpu_tss + TSS_sp0)
#else
# define cpu_current_top_of_stack (cpu_tss.x86_tss.sp0)
#endif

And get rid of that if statement in the static inline?

I prefer less macro indirection in assembly code, but I won't object too strongly if this would be done.

It's up to other x86 maintainers to agree - I'm touching the code recently changed by Andy, he may see the way forward somewhat differently.

#9 lore raw
Quoted 28 lines · Denys Vlasenko
Quoted 27 lines · Steven Rostedt
Quoted 8 lines · On Mon, 23 Mar 2015 18:38:09 +0100 Denys Vlasenko <[email protected]> wrote:
Quoted 3 lines

Can we do a:

#define cpu_current_top_of_stack (cpu_tss + TSS_sp0)

We already do something similar:

static inline unsigned long current_top_of_stack(void) {

Quoted 7 lines
#ifdef CONFIG_X86_64
        return this_cpu_read_stable(cpu_tss.x86_tss.sp0);
#else
        /* sp0 on x86_32 is special in and around vm86 mode. */
        return this_cpu_read_stable(cpu_current_top_of_stack);

#endif
}

So can we then have:

#ifdef __ASSEMBLY__

# define cpu_current_top_of_stack (cpu_tss + TSS_sp0)
#else
# define cpu_current_top_of_stack (cpu_tss.x86_tss.sp0)
#endif
Quoted 9 lines
Quoted 1 line

And get rid of that if statement in the static inline?

I prefer less macro indirection in assembly code, but I won't object too strongly if this would be done.

It's up to other x86 maintainers to agree - I'm touching the code recently changed by Andy, he may see the way forward somewhat differently.

I kind of like Steven's idea. Although.. shouldn't we go all the way and do:

#ifdef __ASSEMBLY__
#ifdef CONFIG_X86_64
#define cpu_current_top_of_stack %gs:(cpu_tss + TSS_sp0) #else
#define cpu_current_top_of_stack %gs:(current_top_of_stack) #endif
#endif

and just not have a macro for non-asm, since we already have the inline?

But maybe keeping the %gs reference explicit in the asm is better.

--Andy

#10 lore raw
Quoted 41 lines · Andy Lutomirski
Quoted 28 lines · Denys Vlasenko
Quoted 27 lines · Steven Rostedt
Quoted 8 lines · On Mon, 23 Mar 2015 18:38:09 +0100 Denys Vlasenko <[email protected]> wrote:
Quoted 3 lines

Can we do a:

#define cpu_current_top_of_stack (cpu_tss + TSS_sp0)

We already do something similar:

static inline unsigned long current_top_of_stack(void) {

Quoted 7 lines
#ifdef CONFIG_X86_64
        return this_cpu_read_stable(cpu_tss.x86_tss.sp0);
#else
        /* sp0 on x86_32 is special in and around vm86 mode. */
        return this_cpu_read_stable(cpu_current_top_of_stack);

#endif
}

So can we then have:

#ifdef __ASSEMBLY__

# define cpu_current_top_of_stack (cpu_tss + TSS_sp0)
#else
# define cpu_current_top_of_stack (cpu_tss.x86_tss.sp0)
#endif
Quoted 9 lines
Quoted 1 line

And get rid of that if statement in the static inline?

I prefer less macro indirection in assembly code, but I won't object too strongly if this would be done.

It's up to other x86 maintainers to agree - I'm touching the code recently changed by Andy, he may see the way forward somewhat differently.

I kind of like Steven's idea. Although.. shouldn't we go all the way and do:

Quoted 7 lines

#ifdef __ASSEMBLY__
#ifdef CONFIG_X86_64
#define cpu_current_top_of_stack %gs:(cpu_tss + TSS_sp0) #else
#define cpu_current_top_of_stack %gs:(current_top_of_stack) #endif
#endif

Which .h file do you propose to have this in? processor.h is not suitable, it is not __ASSEMBLY__-fied.

I'm looking at a place where to put it.
For example, one of the users of the (cpu_tss + TSS_sp0) expression, xen-asm_64.S, has only these few includes:

#include <asm/errno.h>
#include <asm/percpu.h>
#include <asm/processor-flags.h>
#include <asm/segment.h>
#include <asm/asm-offsets.h>

#include <xen/interface/xen.h>

#include "xen-asm.h"

None seems suitable. I will probably have to include thread_info.h too, and put the define there. Ugly ugly ugly.

I'll resend a patchset where a new patch #3 attempts to do this "unification". I personally don't see it as a clear improvement.