loongarch.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: Huacai Chen <chenhuacai@kernel.org>
To: Jinyang He <hejinyang@loongson.cn>
Cc: WANG Xuerui <kernel@xen0n.name>, Oleg Nesterov <oleg@redhat.com>,
	loongarch@lists.linux.dev,  LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] LoongArch: Mark kernel stack padding as TOP_OF_KERNEL_STACK_PADDING
Date: Mon, 5 Sep 2022 14:21:03 +0800	[thread overview]
Message-ID: <CAAhV-H5PA6n9a8A9unEitXEowUMEkrrHQoJXdBoOLZJ559-UDg@mail.gmail.com> (raw)
In-Reply-To: <b3040de0-9c89-a6ff-517e-6ebec71faaf7@loongson.cn>

Hi, Jinyang,

On Mon, Sep 5, 2022 at 1:39 PM Jinyang He <hejinyang@loongson.cn> wrote:
>
> On 2022/9/4 20:19, Huacai Chen wrote:
>
> > Hi, Jinyang,
> >
> > I don't know what scenarios this padding is used and how to use.
>
> To be frankly, I don't know its scenarios on LoongArch. It looks like
> the MIPS legacy. This patch only keep this padding, which can be
> easily modified if needed in the future. Now we can completely
> set it to 0 or remove it.  Actually, it can be thought of as another
> buffer for thread_info, while thread_info usually limit to one cache line.
We should make it clear how MIPS use it at least, then we can make a
decision whether to remove it or not, otherwise it may break
something.

>
> > And
> > in x86 there is only TOP_OF_KERNEL_STACK_PADDING but we need
> > TOP_OF_KERNEL_STACK_PADDING and _TOP_OF_KERNEL_STACK_PADDING. Two
> > macros are hard to maintain.
>
> How about put it into loongarch.h if we need this macro?
That is good because we only need one macro, but you can research
whether there is a better place.

Huacai
>
> Thanks
>
> > On Fri, Sep 2, 2022 at 11:01 PM Jinyang He <hejinyang@loongson.cn> wrote:
> >> Mark the top reserved bytes of the kernel stack by a macro. It collects
> >> that info so that we can modify or discarded (let it be 0) this padding
> >> if needed. Its name is referenced from x86_64. It seems that x86_64
> >> think the padding as extension on top of the kernel stack in other mode
> >> (normally the top is only pt_regs). Although LoongArch not produce
> >> such an extension at present, collect them because of the uncertainty
> >> of the future. Note that this patch does not modify macro size. It
> >> requires another strict evaluation.
> >>
> >> Signed-off-by: Jinyang He <hejinyang@loongson.cn>
> >> ---
> >>   arch/loongarch/include/asm/processor.h  | 3 ++-
> >>   arch/loongarch/include/asm/ptrace.h     | 3 ++-
> >>   arch/loongarch/include/asm/stackframe.h | 1 +
> >>   arch/loongarch/kernel/head.S            | 2 +-
> >>   arch/loongarch/kernel/process.c         | 4 ++--
> >>   arch/loongarch/kernel/switch.S          | 2 +-
> >>   6 files changed, 9 insertions(+), 6 deletions(-)
> >>
> >> diff --git a/arch/loongarch/include/asm/processor.h b/arch/loongarch/include/asm/processor.h
> >> index 1c4b43083..6077fd8e3 100644
> >> --- a/arch/loongarch/include/asm/processor.h
> >> +++ b/arch/loongarch/include/asm/processor.h
> >> @@ -193,8 +193,9 @@ static inline void flush_thread(void)
> >>
> >>   unsigned long __get_wchan(struct task_struct *p);
> >>
> >> +#define TOP_OF_KERNEL_STACK_PADDING 32
> >>   #define __KSTK_TOS(tsk) ((unsigned long)task_stack_page(tsk) + \
> >> -                        THREAD_SIZE - 32 - sizeof(struct pt_regs))
> >> +                        THREAD_SIZE - TOP_OF_KERNEL_STACK_PADDING - 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])
> >> diff --git a/arch/loongarch/include/asm/ptrace.h b/arch/loongarch/include/asm/ptrace.h
> >> index 17838c6b7..cdceeaa60 100644
> >> --- a/arch/loongarch/include/asm/ptrace.h
> >> +++ b/arch/loongarch/include/asm/ptrace.h
> >> @@ -133,7 +133,8 @@ 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 -               \
> >> +                          TOP_OF_KERNEL_STACK_PADDING) - 1;            \
> >>   })
> >>
> >>   /* Helpers for working with the user stack pointer */
> >> diff --git a/arch/loongarch/include/asm/stackframe.h b/arch/loongarch/include/asm/stackframe.h
> >> index 4ca953062..e9f849b29 100644
> >> --- a/arch/loongarch/include/asm/stackframe.h
> >> +++ b/arch/loongarch/include/asm/stackframe.h
> >> @@ -216,4 +216,5 @@
> >>          RESTORE_SP_AND_RET \docfi
> >>          .endm
> >>
> >> +#define _TOP_OF_KERNEL_STACK_PADDING 32
> >>   #endif /* _ASM_STACKFRAME_H */
> >> diff --git a/arch/loongarch/kernel/head.S b/arch/loongarch/kernel/head.S
> >> index eb3f641d5..290c53847 100644
> >> --- a/arch/loongarch/kernel/head.S
> >> +++ b/arch/loongarch/kernel/head.S
> >> @@ -75,7 +75,7 @@ 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 - _TOP_OF_KERNEL_STACK_PADDING - PT_SIZE)
> >>          PTR_ADD         sp, sp, tp
> >>          set_saved_sp    sp, t0, t1
> >>          PTR_ADDI        sp, sp, -4 * SZREG      # init stack pointer
> >> diff --git a/arch/loongarch/kernel/process.c b/arch/loongarch/kernel/process.c
> >> index 660492f06..b51cf7975 100644
> >> --- a/arch/loongarch/kernel/process.c
> >> +++ b/arch/loongarch/kernel/process.c
> >> @@ -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 - TOP_OF_KERNEL_STACK_PADDING;
> >>
> >>          /* 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 - TOP_OF_KERNEL_STACK_PADDING;
> >>
> >>          if (stack < begin || stack >= end)
> >>                  return false;
> >> diff --git a/arch/loongarch/kernel/switch.S b/arch/loongarch/kernel/switch.S
> >> index 43ebbc399..1797aceae 100644
> >> --- a/arch/loongarch/kernel/switch.S
> >> +++ b/arch/loongarch/kernel/switch.S
> >> @@ -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 - _TOP_OF_KERNEL_STACK_PADDING
> >>          PTR_ADD         t0, t0, tp
> >>          set_saved_sp    t0, t1, t2
> >>
> >> --
> >> 2.31.1
> >>
>

  reply	other threads:[~2022-09-05  6:21 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-02 15:01 [PATCH] LoongArch: Mark kernel stack padding as TOP_OF_KERNEL_STACK_PADDING Jinyang He
2022-09-04 12:19 ` Huacai Chen
2022-09-05  5:39   ` Jinyang He
2022-09-05  6:21     ` Huacai Chen [this message]
2022-09-06  6:06       ` Jinyang He
2022-09-06  7:00         ` Huacai Chen
2022-09-06  6:09       ` Jinyang He

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAAhV-H5PA6n9a8A9unEitXEowUMEkrrHQoJXdBoOLZJ559-UDg@mail.gmail.com \
    --to=chenhuacai@kernel.org \
    --cc=hejinyang@loongson.cn \
    --cc=kernel@xen0n.name \
    --cc=linux-kernel@vger.kernel.org \
    --cc=loongarch@lists.linux.dev \
    --cc=oleg@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).