Skip to contentSkip to conversation

LoongArch: Remove unused kernel stack padding

PATCH RESEND Superseded Jinyang He 2 participants 4 messages Oct 26, 2022

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jinyang He

author
#0 lore raw

PATCH RESEND v2 1/1 LoongArch: Remove unused kernel stack padding

Kernel stack padding looks like obey MIPS o32 Calling Convention, as LoongArch is inspired by MIPS and keep it. Remove it avoid not clear code.

---
v2: Remove TOP_OF_KERNEL_STACK_PADDING
Remove 'init stack pointer' in head.S

 arch/loongarch/include/asm/processor.h | 2 +-
 arch/loongarch/include/asm/ptrace.h    | 2 +-
 arch/loongarch/kernel/head.S           | 3 +--
 arch/loongarch/kernel/process.c        | 4 ++--
 arch/loongarch/kernel/switch.S         | 2 +-
 5 files changed, 6 insertions(+), 7 deletions(-)
arch/loongarch/include/asm/processor.h +1 -1
index 6954dc5d24e9..7184f1dc61f2 100644
@@ -191,7 +191,7 @@ static inline void flush_thread(void)
 unsigned long __get_wchan(struct task_struct *p);
 
 #define __KSTK_TOS(tsk) ((unsigned long)task_stack_page(tsk) + \
-			 THREAD_SIZE - 32 - sizeof(struct pt_regs))
+			 THREAD_SIZE - sizeof(struct pt_regs))
 #define task_pt_regs(tsk) ((struct pt_regs *)__KSTK_TOS(tsk))
 #define KSTK_EIP(tsk) (task_pt_regs(tsk)->csr_era)
 #define KSTK_ESP(tsk) (task_pt_regs(tsk)->regs[3])
arch/loongarch/include/asm/ptrace.h +1 -1
index 7437b9366c3b..59c4608de91d 100644
@@ -133,7 +133,7 @@ static inline void die_if_kernel(const char *str, struct pt_regs *regs)
 #define current_pt_regs()						\
 ({									\
 	unsigned long sp = (unsigned long)__builtin_frame_address(0);	\
-	(struct pt_regs *)((sp | (THREAD_SIZE - 1)) + 1 - 32) - 1;	\
+	(struct pt_regs *)((sp | (THREAD_SIZE - 1)) + 1) - 1;		\
 })
 
 /* Helpers for working with the user stack pointer */
arch/loongarch/kernel/head.S +1 -2
index 97425779ce9f..84970e266658 100644
@@ -84,10 +84,9 @@ SYM_CODE_START(kernel_entry)			# kernel entry point
 
 	la.pcrel	tp, init_thread_union
 	/* Set the SP after an empty pt_regs.  */
-	PTR_LI		sp, (_THREAD_SIZE - 32 - PT_SIZE)
+	PTR_LI		sp, (_THREAD_SIZE - PT_SIZE)
 	PTR_ADD		sp, sp, tp
 	set_saved_sp	sp, t0, t1
-	PTR_ADDI	sp, sp, -4 * SZREG	# init stack pointer
 
 	bl		start_kernel
 	ASM_BUG()
arch/loongarch/kernel/process.c +2 -2
index 1256e3582475..2526b68f1c0f 100644
@@ -129,7 +129,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 	unsigned long clone_flags = args->flags;
 	struct pt_regs *childregs, *regs = current_pt_regs();
 
-	childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE - 32;
+	childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
 
 	/* set up new TSS. */
 	childregs = (struct pt_regs *) childksp - 1;
@@ -236,7 +236,7 @@ bool in_task_stack(unsigned long stack, struct task_struct *task,
 			struct stack_info *info)
 {
 	unsigned long begin = (unsigned long)task_stack_page(task);
-	unsigned long end = begin + THREAD_SIZE - 32;
+	unsigned long end = begin + THREAD_SIZE;
 
 	if (stack < begin || stack >= end)
 		return false;
arch/loongarch/kernel/switch.S +1 -1
index 43ebbc3990f7..202a163cb32f 100644
@@ -26,7 +26,7 @@ SYM_FUNC_START(__switch_to)
 	move	tp, a2
 	cpu_restore_nonscratch a1
 
-	li.w		t0, _THREAD_SIZE - 32
+	li.w		t0, _THREAD_SIZE
 	PTR_ADD		t0, t0, tp
 	set_saved_sp	t0, t1, t2
 
Signature · 1 line
2.34.3
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 3 lines · Jinyang He

Kernel stack padding looks like obey MIPS o32 Calling Convention, as LoongArch is inspired by MIPS and keep it. Remove it avoid not clear code.

Just some improvement to the commit message so it's clearer:

"The current LoongArch kernel stack is padded as if obeying the MIPS o32 calling convention, signifying the port's MIPS lineage but no longer making sense. Remove the padding for clarity."

Quoted 3 lines

I think this blank line between the "Link" and S-o-b tags could be removed.

Quoted 81 lines

---
v2: Remove TOP_OF_KERNEL_STACK_PADDING
Remove 'init stack pointer' in head.S

  arch/loongarch/include/asm/processor.h | 2 +-
  arch/loongarch/include/asm/ptrace.h    | 2 +-
  arch/loongarch/kernel/head.S           | 3 +--
  arch/loongarch/kernel/process.c        | 4 ++--
  arch/loongarch/kernel/switch.S         | 2 +-
  5 files changed, 6 insertions(+), 7 deletions(-)
arch/loongarch/include/asm/processor.h +1 -1
index 6954dc5d24e9..7184f1dc61f2 100644
@@ -191,7 +191,7 @@ static inline void flush_thread(void)
  unsigned long __get_wchan(struct task_struct *p);
 
  #define __KSTK_TOS(tsk) ((unsigned long)task_stack_page(tsk) + \
-			 THREAD_SIZE - 32 - sizeof(struct pt_regs))
+			 THREAD_SIZE - sizeof(struct pt_regs))
  #define task_pt_regs(tsk) ((struct pt_regs *)__KSTK_TOS(tsk))
  #define KSTK_EIP(tsk) (task_pt_regs(tsk)->csr_era)
  #define KSTK_ESP(tsk) (task_pt_regs(tsk)->regs[3])
arch/loongarch/include/asm/ptrace.h +1 -1
index 7437b9366c3b..59c4608de91d 100644
@@ -133,7 +133,7 @@ static inline void die_if_kernel(const char *str, struct pt_regs *regs)
  #define current_pt_regs()						\
  ({									\
  	unsigned long sp = (unsigned long)__builtin_frame_address(0);	\
-	(struct pt_regs *)((sp | (THREAD_SIZE - 1)) + 1 - 32) - 1;	\
+	(struct pt_regs *)((sp | (THREAD_SIZE - 1)) + 1) - 1;		\
  })
 
  /* Helpers for working with the user stack pointer */
arch/loongarch/kernel/head.S +1 -2
index 97425779ce9f..84970e266658 100644
@@ -84,10 +84,9 @@ SYM_CODE_START(kernel_entry)			# kernel entry point
 
  	la.pcrel	tp, init_thread_union
  	/* Set the SP after an empty pt_regs.  */
-	PTR_LI		sp, (_THREAD_SIZE - 32 - PT_SIZE)
+	PTR_LI		sp, (_THREAD_SIZE - PT_SIZE)
  	PTR_ADD		sp, sp, tp
  	set_saved_sp	sp, t0, t1
-	PTR_ADDI	sp, sp, -4 * SZREG	# init stack pointer
 
  	bl		start_kernel
  	ASM_BUG()
arch/loongarch/kernel/process.c +2 -2
index 1256e3582475..2526b68f1c0f 100644
@@ -129,7 +129,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
  	unsigned long clone_flags = args->flags;
  	struct pt_regs *childregs, *regs = current_pt_regs();
 
-	childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE - 32;
+	childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
 
  	/* set up new TSS. */
  	childregs = (struct pt_regs *) childksp - 1;
@@ -236,7 +236,7 @@ bool in_task_stack(unsigned long stack, struct task_struct *task,
  			struct stack_info *info)
  {
  	unsigned long begin = (unsigned long)task_stack_page(task);
-	unsigned long end = begin + THREAD_SIZE - 32;
+	unsigned long end = begin + THREAD_SIZE;
 
  	if (stack < begin || stack >= end)
  		return false;
arch/loongarch/kernel/switch.S +0 -0
index 43ebbc3990f7..202a163cb32f 100644

arch/loongarch/kernel/switch.S SYM_FUNC_START(__switch_to) · line 32

Quoted 8 lines
arch/loongarch/kernel/switch.S +1 -1
@@ -26,7 +26,7 @@ SYM_FUNC_START(__switch_to)
  	move	tp, a2
  	cpu_restore_nonscratch a1
 
-	li.w		t0, _THREAD_SIZE - 32
+	li.w		t0, _THREAD_SIZE
  	PTR_ADD		t0, t0, tp
  	set_saved_sp	t0, t1, t2
 

Otherwise LGTM, assuming you have tested it before sending (I don't currently have the capacity to immediately test this for you)...

Signature · 3 lines
WANG "xen0n" Xuerui

Linux/LoongArch mailing list: https://lore.kernel.org/loongarch/
author
#2 lore raw

Hi, Xuerui,

Quoted 8 lines · WANG Xuerui
Quoted 3 lines · Jinyang He

Kernel stack padding looks like obey MIPS o32 Calling Convention, as LoongArch is inspired by MIPS and keep it. Remove it avoid not clear code.

Just some improvement to the commit message so it's clearer:

"The current LoongArch kernel stack is padded as if obeying the MIPS o32 calling convention, signifying the port's MIPS lineage but no longer making sense. Remove the padding for clarity."

Thanks, I'll improve it and send it later.

Quoted 6 lines
Quoted 4 lines

I think this blank line between the "Link" and S-o-b tags could be removed.

Ok.

Quoted 88 lines
Quoted 79 lines

---
v2: Remove TOP_OF_KERNEL_STACK_PADDING
     Remove 'init stack pointer' in head.S

  arch/loongarch/include/asm/processor.h | 2 +-
  arch/loongarch/include/asm/ptrace.h    | 2 +-
  arch/loongarch/kernel/head.S           | 3 +--
  arch/loongarch/kernel/process.c        | 4 ++--
  arch/loongarch/kernel/switch.S         | 2 +-

  5 files changed, 6 insertions(+), 7 deletions(-)
arch/loongarch/include/asm/processor.h +1 -1
index 6954dc5d24e9..7184f1dc61f2 100644
@@ -191,7 +191,7 @@ static inline void flush_thread(void)
   unsigned long __get_wchan(struct task_struct *p);
     #define __KSTK_TOS(tsk) ((unsigned long)task_stack_page(tsk) + \
-             THREAD_SIZE - 32 - sizeof(struct pt_regs))
+             THREAD_SIZE - sizeof(struct pt_regs))
   #define task_pt_regs(tsk) ((struct pt_regs *)__KSTK_TOS(tsk))
   #define KSTK_EIP(tsk) (task_pt_regs(tsk)->csr_era)
   #define KSTK_ESP(tsk) (task_pt_regs(tsk)->regs[3])
arch/loongarch/include/asm/ptrace.h +1 -1
index 7437b9366c3b..59c4608de91d 100644
@@ -133,7 +133,7 @@ static inline void die_if_kernel(const char *str, 
 struct pt_regs *regs)
   #define current_pt_regs()                        \
   ({                                    \
       unsigned long sp = (unsigned long)__builtin_frame_address(0);    \
-    (struct pt_regs *)((sp | (THREAD_SIZE - 1)) + 1 - 32) - 1;    \
+    (struct pt_regs *)((sp | (THREAD_SIZE - 1)) + 1) - 1;        \
   })
     /* Helpers for working with the user stack pointer */
arch/loongarch/kernel/head.S +0 -0
index 97425779ce9f..84970e266658 100644
@@ -84,10 +84,9 @@ SYM_CODE_START(kernel_entry)            # kernel 

entry point
        la.pcrel    tp, init_thread_union
      /* Set the SP after an empty pt_regs.  */
-    PTR_LI        sp, (_THREAD_SIZE - 32 - PT_SIZE) +    PTR_LI        sp, (_THREAD_SIZE - PT_SIZE) PTR_ADD        sp, sp, tp
      set_saved_sp    sp, t0, t1
-    PTR_ADDI    sp, sp, -4 * SZREG    # init stack pointer bl        start_kernel
      ASM_BUG()

arch/loongarch/kernel/process.c +2 -1
index 1256e3582475..2526b68f1c0f 100644
@@ -129,7 +129,7 @@ int copy_thread(struct task_struct *p, const 
 struct kernel_clone_args *args)
       unsigned long clone_flags = args->flags;
       struct pt_regs *childregs, *regs = current_pt_regs();
   -    childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE - 32;
+    childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
         /* set up new TSS. */
       childregs = (struct pt_regs *) childksp - 1;
@@ -236,7 +236,7 @@ bool in_task_stack(unsigned long stack, struct 
 task_struct *task,
               struct stack_info *info)
   {
       unsigned long begin = (unsigned long)task_stack_page(task);
-    unsigned long end = begin + THREAD_SIZE - 32;
+    unsigned long end = begin + THREAD_SIZE;
         if (stack < begin || stack >= end)
           return false;
arch/loongarch/kernel/switch.S +0 -0
index 43ebbc3990f7..202a163cb32f 100644
@@ -26,7 +26,7 @@ SYM_FUNC_START(__switch_to)
Quoted 6 lines

      move    tp, a2
      cpu_restore_nonscratch a1
  -    li.w        t0, _THREAD_SIZE - 32
+    li.w        t0, _THREAD_SIZE
      PTR_ADD        t0, t0, tp
      set_saved_sp    t0, t1, t2

Otherwise LGTM, assuming you have tested it before sending (I don't currently have the capacity to immediately test this for you)...

Quoted 1 line

Emm, to be honest, I didn't do tests like LTP. I ususally do development on CLFS-LoongArch while kernel has this patch. And it works normally.

Thanks,

Jinyang

reviewer
#3 lore raw
Quoted 2 lines · Jinyang He

Emm, to be honest, I didn't do tests like LTP. I ususally do development on CLFS-LoongArch while kernel has this patch. And it works normally.

For changes touching fundamental parts that make the system work at all, like this, "it continues to boot and work" usually is enough for confirming correctness. Although some harder test runs would certainly be better ;-)

Signature · 3 lines
WANG "xen0n" Xuerui

Linux/LoongArch mailing list: https://lore.kernel.org/loongarch/