Re: [PATCH V4 8/8] riscv: Add config of thread stack size

Guo Ren
2022-09-21T10:46:45Z

Hi Arnd,

No more coding conventions for the patch, THREAD_ALIGN &
THREAD_SIZE_ORDER are used by vmlinux.lds.S and common code. So here
is my considerable version:

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 76bde12d9f8c..602e577c429c 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -443,6 +443,16 @@ config IRQ_STACKS
Add independent irq & softirq stacks for percpu to prevent
kernel stack
overflows. We may save some memory footprint by disabling IRQ_STACKS.

+config THREAD_SIZE_ORDER
+ int "Kernel stack size (in power-of-two numbers of page size)"
if VMAP_STACK && EXPERT
+ range 0 4
+ default 1 if 32BIT && !KASAN
+ default 3 if 64BIT && KASAN
+ default 2
+ help
+ Specify the Pages of thread stack size (from 4KB to 64KB), which also
+ affects irq stack size, which is equal to thread stack size.
+
endmenu # "Platform type"

menu "Kernel features"
diff --git a/arch/riscv/include/asm/thread_info.h
b/arch/riscv/include/asm/thread_info.h
index 043da8ccc7e6..c970d41dc4c6 100644
--- a/arch/riscv/include/asm/thread_info.h
+++ b/arch/riscv/include/asm/thread_info.h
@@ -11,18 +11,8 @@
#include <asm/page.h>
#include <linux/const.h>

-#ifdef CONFIG_KASAN
-#define KASAN_STACK_ORDER 1
-#else
-#define KASAN_STACK_ORDER 0
-#endif
-
/* thread information allocation */
-#ifdef CONFIG_64BIT
-#define THREAD_SIZE_ORDER (2 + KASAN_STACK_ORDER)
-#else
-#define THREAD_SIZE_ORDER (1 + KASAN_STACK_ORDER)
-#endif
+#define THREAD_SIZE_ORDER CONFIG_THREAD_SIZE_ORDER
#define THREAD_SIZE (PAGE_SIZE << THREAD_SIZE_ORDER)

It's a little bit back to the original, but KASAN_STACK_ORDER has been
deleted. VMAP_STACK && EXPERT added.

On Wed, Sep 21, 2022 at 6:31 PM Guo Ren <guoren@kernel.org> wrote:
>
> On Wed, Sep 21, 2022 at 4:23 PM Guo Ren <guoren@kernel.org> wrote:
> >
> > On Wed, Sep 21, 2022 at 2:13 PM Guo Ren <guoren@kernel.org> wrote:
> > >
> > > On Tue, Sep 20, 2022 at 3:18 PM Arnd Bergmann <arnd@arndb.de> wrote:
> > > >
> > > > On Tue, Sep 20, 2022, at 2:46 AM, Guo Ren wrote:
> > > >
> > > > > diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
> > > > > index dfe600f3526c..8def456f328c 100644
> > > > > --- a/arch/riscv/Kconfig
> > > > > +++ b/arch/riscv/Kconfig
> > > > > @@ -442,6 +442,16 @@ config IRQ_STACKS
> > > > > Add independent irq & softirq stacks for percpu to prevent
> > > > > kernel stack
> > > > > overflows. We may save some memory footprint by disabling IRQ_STACKS.
> > > > >
> > > > > +config THREAD_SIZE
> > > > > + int "Kernel stack size (in bytes)" if EXPERT
> > > > > + range 4096 65536
> > > > > + default 8192 if 32BIT && !KASAN
> > > > > + default 32768 if 64BIT && KASAN
> > > > > + default 16384
> > > > > + help
> > > > > + Specify the Pages of thread stack size (from 4KB to 64KB), which also
> > > > > + affects irq stack size, which is equal to thread stack size.
> > > >
> > > > I still think this should be guarded in a way that prevents
> > > > setting the stack to smaller than default values unless VMAP_STACK
> > > > is set as well.
> > > Current VMAP_STACK would double THREAD_SIZE. Let me see how to reduce
> > > the VMAP_STACK.
> > Sorry, for my miss understanding. I have no idea to reduce the
> > VMAP_STACK's THREAD_ALIGN, THREAD_SIZE*2 is fine. Here is my new
> > patch:
> >
> > diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
> > index 76bde12d9f8c..669ae57356a2 100644
> > --- a/arch/riscv/Kconfig
> > +++ b/arch/riscv/Kconfig
> > @@ -443,6 +443,16 @@ config IRQ_STACKS
> > Add independent irq & softirq stacks for percpu to prevent
> > kernel stack
> > overflows. We may save some memory footprint by disabling IRQ_STACKS.
> >
> > +config THREAD_SIZE
> > + int "Kernel stack size (in bytes)" if VMAP_STACK && EXPERT
> > + range 4096 65536
> > + default 8192 if 32BIT && !KASAN
> > + default 32768 if 64BIT && KASAN
> > + default 16384
> > + help
> > + Specify the Pages of thread stack size (from 4KB to 64KB), which also
> > + affects irq stack size, which is equal to thread stack size.
> > +
> > endmenu # "Platform type"
> >
> > menu "Kernel features"
> > diff --git a/arch/riscv/include/asm/thread_info.h
> > b/arch/riscv/include/asm/thread_info.h
> > index 043da8ccc7e6..e7ae3f13b879 100644
> > --- a/arch/riscv/include/asm/thread_info.h
> > +++ b/arch/riscv/include/asm/thread_info.h
> > @@ -11,32 +11,17 @@
> > #include <asm/page.h>
> > #include <linux/const.h>
> >
> > -#ifdef CONFIG_KASAN
> > -#define KASAN_STACK_ORDER 1
> > -#else
> > -#define KASAN_STACK_ORDER 0
> > -#endif
> > -
> > /* thread information allocation */
> > -#ifdef CONFIG_64BIT
> > -#define THREAD_SIZE_ORDER (2 + KASAN_STACK_ORDER)
> > -#else
> > -#define THREAD_SIZE_ORDER (1 + KASAN_STACK_ORDER)
> > -#endif
> > -#define THREAD_SIZE (PAGE_SIZE << THREAD_SIZE_ORDER)
> > +#define THREAD_SIZE CONFIG_THREAD_SIZE
> >
> > /*
> > * By aligning VMAP'd stacks to 2 * THREAD_SIZE, we can detect overflow by
> > - * checking sp & (1 << THREAD_SHIFT), which we can do cheaply in the entry
> > - * assembly.
> > + * checking sp & THREAD_SIZE, which we can do cheaply in the entry assembly.
> > */
> > #ifdef CONFIG_VMAP_STACK
> > #define THREAD_ALIGN (2 * THREAD_SIZE)
> > -#else
> > -#define THREAD_ALIGN THREAD_SIZE
> > #endif
> >
> > -#define THREAD_SHIFT (PAGE_SHIFT + THREAD_SIZE_ORDER)
> > #define OVERFLOW_STACK_SIZE SZ_4K
> > #define SHADOW_OVERFLOW_STACK_SIZE (1024)
> >
> > diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S
> > index 2207cf44a3bc..71ea850ff0db 100644
> > --- a/arch/riscv/kernel/entry.S
> > +++ b/arch/riscv/kernel/entry.S
> > @@ -29,8 +29,8 @@ _restore_kernel_tpsp:
> >
> > #ifdef CONFIG_VMAP_STACK
> > addi sp, sp, -(PT_SIZE_ON_STACK)
> > - srli sp, sp, THREAD_SHIFT
> > - andi sp, sp, 0x1
> > + srli sp, sp, PAGE_SHIFT
> > + andi sp, sp, (THREAD_ALIGN >> PAGE_SHIFT >> 1)
> > bnez sp, handle_kernel_stack_overflow
> > REG_L sp, TASK_TI_KERNEL_SP(tp)
> > #endif
> Sorry for the update again, fixup !VMAP_STACK compile error.
>
> diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
> index 76bde12d9f8c..602e577c429c 100644
> --- a/arch/riscv/Kconfig
> +++ b/arch/riscv/Kconfig
> @@ -443,6 +443,16 @@ config IRQ_STACKS
> Add independent irq & softirq stacks for percpu to prevent
> kernel stack
> overflows. We may save some memory footprint by disabling IRQ_STACKS.
>
> +config THREAD_SIZE_ORDER
> + int "Kernel stack size (in power-of-two numbers of page size)"
> if VMAP_STACK && EXPERT
> + range 0 4
> + default 1 if 32BIT && !KASAN
> + default 3 if 64BIT && KASAN
> + default 2
> + help
> + Specify the Pages of thread stack size (from 4KB to 64KB), which also
> + affects irq stack size, which is equal to thread stack size.
> +
> endmenu # "Platform type"
>
> menu "Kernel features"
> diff --git a/arch/riscv/include/asm/thread_info.h
> b/arch/riscv/include/asm/thread_info.h
> index 043da8ccc7e6..3f382490d8ed 100644
> --- a/arch/riscv/include/asm/thread_info.h
> +++ b/arch/riscv/include/asm/thread_info.h
> @@ -11,24 +11,13 @@
> #include <asm/page.h>
> #include <linux/const.h>
>
> -#ifdef CONFIG_KASAN
> -#define KASAN_STACK_ORDER 1
> -#else
> -#define KASAN_STACK_ORDER 0
> -#endif
> -
> /* thread information allocation */
> -#ifdef CONFIG_64BIT
> -#define THREAD_SIZE_ORDER (2 + KASAN_STACK_ORDER)
> -#else
> -#define THREAD_SIZE_ORDER (1 + KASAN_STACK_ORDER)
> -#endif
> -#define THREAD_SIZE (PAGE_SIZE << THREAD_SIZE_ORDER)
> +#define THREAD_SIZE_ORDER CONFIG_THREAD_SIZE_ORDER
> +#define THREAD_SIZE (1 << PAGE_SHIFT << THREAD_SIZE_ORDER)
>
> /*
> * By aligning VMAP'd stacks to 2 * THREAD_SIZE, we can detect overflow by
> - * checking sp & (1 << THREAD_SHIFT), which we can do cheaply in the entry
> - * assembly.
> + * checking sp & THREAD_SIZE, which we can do cheaply in the entry assembly.
> */
> #ifdef CONFIG_VMAP_STACK
> #define THREAD_ALIGN (2 * THREAD_SIZE)
> @@ -36,7 +25,6 @@
> #define THREAD_ALIGN THREAD_SIZE
> #endif
>
> -#define THREAD_SHIFT (PAGE_SHIFT + THREAD_SIZE_ORDER)
> #define OVERFLOW_STACK_SIZE SZ_4K
> #define SHADOW_OVERFLOW_STACK_SIZE (1024)
>
> diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S
> index 5cbd6684ef52..62e8f3a3c942 100644
> --- a/arch/riscv/kernel/entry.S
> +++ b/arch/riscv/kernel/entry.S
> @@ -29,8 +29,8 @@ _restore_kernel_tpsp:
>
> #ifdef CONFIG_VMAP_STACK
> addi sp, sp, -(PT_SIZE_ON_STACK)
> - srli sp, sp, THREAD_SHIFT
> - andi sp, sp, 0x1
> + srli sp, sp, PAGE_SHIFT
> + andi sp, sp, (THREAD_ALIGN >> PAGE_SHIFT >> 1)
> bnez sp, handle_kernel_stack_overflow
> REG_L sp, TASK_TI_KERNEL_SP(tp)
> #endif
>
> >
> >
> > >
> > > >
> > > > Arnd
> > >
> > >
> > >
> > > --
> > > Best Regards
> > > Guo Ren
> >
> >
> >
> > --
> > Best Regards
> > Guo Ren
>
>
>
> --
> Best Regards
> Guo Ren

--
Best Regards
Guo Ren