x86: Allow variable-sized event frame
Subsystems (6)
Xin Li (Intel)
maintainerv4 cover x86: Allow variable-sized event frame
This was initially posted as part of the FRED patch series and turned down due to its unacceptable quality:
https://lore.kernel.org/lkml/[email protected]/
Now comes another attempt to meet the bar.
A FRED event frame could contain different amount of information for different event types, e.g., #MCE could push extra bytes of information, or perhaps even for different instances of the same event type. Thus the size of an event frame pushed by a FRED CPU is not fixed and the address of a pt_regs structure that is used to save the user level context of current task is not at a fixed offset from top of current task kernel stack.
This patch set adds a new field named 'user_pt_regs' in the thread_info structure to save the address of user level context pt_regs structure, thus to eliminate the need of any advance information of event frame size and allow a FRED CPU to push variable-sized event frame.
With the above change, we can remove the padding space at top of the init stack because there is no user level context for init task.
Link to v3: https://lore.kernel.org/lkml/[email protected]/
Change in v4:
* Drop the patch that zaps TOP_OF_KERNEL_STACK_PADDING on x86_64 (Brian Gerst, hpa).
Change in v3:
* Replace "(struct pt_regs *)TOP_OF_INIT_STACK - 1" with (struct pt_regs *)__top_init_kernel_stack (Brian Gerst).
* Add declaration for __top_init_kernel_stack[] (Intel lkp).
Change in v2:
* Rebase on latest tip/master.
Xin Li (Intel) (2):
x86/fred: Allow variable-sized event frame
x86: Remove the padding space at top of the init stack
arch/x86/entry/entry_fred.c | 10 ++++++++++ arch/x86/include/asm/processor.h | 28 ++++++++++++++++++++-------- arch/x86/include/asm/thread_info.h | 11 ++++++++--- arch/x86/kernel/process.c | 22 ++++++++++++++++++++++ arch/x86/kernel/vmlinux.lds.S | 7 +++++-- include/linux/thread_info.h | 1 + kernel/fork.c | 6 ++++++ 7 files changed, 72 insertions(+), 13 deletions(-)
base-commit: 8e3f385164626dc6bbf000decf04aa98e943e07e
Signature · 1 line
2.49.0
Xin Li (Intel)
maintainerPATCH v4 1/2 x86/fred: Allow variable-sized event frame
A FRED event frame could contain different amount of information for different event types, or perhaps even for different instances of the same event type. Thus the size of an event frame pushed by a FRED CPU is not fixed and the address of the pt_regs structure that is used to save a user level context of current task is not at a fixed offset from top of current task kernel stack.
Add a new field named 'user_pt_regs' in the thread_info structure to save the address of user level context pt_regs structure, thus to eliminate the need of any advance information of event frame size and allow a FRED CPU to push variable-sized event frame.
For IDT user level event delivery, a pt_regs structure is pushed by hardware and software _always_ at a fixed offset from top of current task kernel stack, so simply initialize user_pt_regs to point to the pt_regs structure no matter whether one is pushed or not.
While for FRED user level event delivery, user_pt_regs is updated with a pt_regs structure pointer generated in asm_fred_entrypoint_user().
---
Change in v3A:
* Add declaration for __top_init_kernel_stack[] (Intel lkp).
Change in v3:
* Replace "(struct pt_regs *)TOP_OF_INIT_STACK - 1" with (struct pt_regs *)__top_init_kernel_stack (Brian Gerst).
--- arch/x86/entry/entry_fred.c | 10 ++++++++++ arch/x86/include/asm/processor.h | 12 ++++++------ arch/x86/include/asm/thread_info.h | 11 ++++++++--- arch/x86/kernel/process.c | 22 ++++++++++++++++++++++ include/linux/thread_info.h | 1 + kernel/fork.c | 6 ++++++ 6 files changed, 53 insertions(+), 9 deletions(-)
arch/x86/entry/entry_fred.c +10 -0
@@ -228,6 +228,16 @@ __visible noinstr void fred_entry_from_user(struct pt_regs *regs)/* Invalidate orig_ax so that syscall_get_nr() works correctly */ regs->orig_ax =-1 ; +/* +* A FRED event frame could contain different amount of information +* for different event types, or perhaps even for different instances +* of the same event type. Thus the size of an event frame pushed by +* a FRED CPU is not fixed and the address of the pt_regs structure +* that is used to save a user level context of current task is not +* at a fixed offset from top of current task stack. +*/ + current->thread_info.user_pt_regs = regs; +switch (regs->fred_ss.type) {case EVENT_TYPE_EXTINT :return fred_extint (regs);
arch/x86/include/asm/processor.h +6 -6
@@ -645,12 +645,12 @@ static __always_inline void prefetchw(const void *x)#define task_top_of_stack (task) ((unsigned long)(task_pt_regs(task) + 1)) -#define task_pt_regs (task) \ -({ \ - unsigned long __ptr = (unsigned long)task_stack_page(task); \ - __ptr += THREAD_SIZE - TOP_OF_KERNEL_STACK_PADDING; \ - ((struct pt_regs *)__ptr) - 1; \ -}) +/* +* Note, this can't be converted to an inline function as this header +* file defines 'struct thread_struct' which is used in the task_struct +* structure definition. +*/ +#define task_pt_regs (task) ((task)->thread_info.user_pt_regs)#ifdef CONFIG_X86_32 #define INIT_THREAD { \
arch/x86/include/asm/thread_info.h +8 -3
@@ -56,6 +56,7 @@*/ #ifndef __ASSEMBLER__struct task_struct ; +struct pt_regs ;#include <asm/cpufeature.h> #include <linux/atomic.h>
@@ -66,11 +67,15 @@ struct thread_info {#ifdef CONFIG_SMP u32 cpu;/* current CPU */ #endif + struct pt_regs *user_pt_regs; }; -#define INIT_THREAD_INFO (tsk) \ -{ \ - .flags = 0, \ +extern unsigned long __top_init_kernel_stack[]; + +#define INIT_THREAD_INFO (tsk) \ +{ \ + .flags = 0, \ + .user_pt_regs = (struct pt_regs *)__top_init_kernel_stack, \ }#else /* !__ASSEMBLER__ */
arch/x86/kernel/process.c +22 -0
@@ -104,6 +104,28 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)return 0 ; } +/* +* Initialize thread_info.user_pt_regs for IDT event delivery. +* +* For IDT user level event delivery, a pt_regs structure is pushed by both +* hardware and software and always resides at a fixed offset from top of +* current task kernel stack, thus thread_info.user_pt_regs is a per-task +* constant and NEVER changes after initialization. +* +* While for FRED user level event delivery, user_pt_regs is updated in +* fred_entry_from_user() immediately after user level event delivery. +* +* Note: thread_info.user_pt_regs of the init task is initialized at build +* time. +*/ +void arch_init_user_pt_regs (struct task_struct *tsk) +{ +unsigned long top_of_stack = (unsigned long )task_stack_page (tsk) +THREAD_SIZE ; + + top_of_stack -=TOP_OF_KERNEL_STACK_PADDING ; + tsk->thread_info.user_pt_regs = (struct pt_regs *)top_of_stack -1 ; +} +#ifdef CONFIG_X86_64 void arch_release_task_struct (struct task_struct *tsk) {
include/linux/thread_info.h +1 -0
@@ -225,6 +225,7 @@ void arch_task_cache_init(void); /* for CONFIG_SH */void arch_release_task_struct (struct task_struct *tsk);int arch_dup_task_struct (struct task_struct *dst,struct task_struct *src); +void arch_init_user_pt_regs (struct task_struct *tsk);#endif /* __KERNEL__ */
kernel/fork.c +6 -0
@@ -1110,6 +1110,10 @@ int __weak arch_dup_task_struct(struct task_struct *dst,return 0 ; } +void __weak arch_init_user_pt_regs (struct task_struct *tsk) +{ +} +void set_task_stack_end_magic (struct task_struct *tsk) {unsigned long *stackend;
@@ -1137,6 +1141,8 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)if (err) goto free_tsk; +arch_init_user_pt_regs (tsk); +#ifdef CONFIG_THREAD_INFO_IN_TASK refcount_set (&tsk->stack_refcount,1 );#endif
Signature · 1 line
2.49.0
Peter Zijlstra
maintainerRe: [PATCH v4 1/2] x86/fred: Allow variable-sized event frame
arch/x86/include/asm/thread_info.h struct thread_info { · line 78
Quoted 9 lines · Xin Li (Intel)
arch/x86/include/asm/thread_info.h +6
-3-#define INIT_THREAD_INFO (tsk) \ -{ \ - .flags = 0, \ +extern unsigned long __top_init_kernel_stack[]; + +#define INIT_THREAD_INFO (tsk) \ +{ \ + .flags = 0, \ + .user_pt_regs = (struct pt_regs *)__top_init_kernel_stack, \
Should that not have a -1 on or something?
Re: [PATCH v4 1/2] x86/fred: Allow variable-sized event frame
Quoted 4 lines · Peter Zijlstra
On Sun, May 11, 2025 at 11:43:52PM -0700, Xin Li (Intel) wrote:
Quoted 11 lines
arch/x86/include/asm/thread_info.h struct thread_info { · line 78
Quoted 9 lines
arch/x86/include/asm/thread_info.h +6
-3-#define INIT_THREAD_INFO (tsk) \ -{ \ - .flags = 0, \ +extern unsigned long __top_init_kernel_stack[]; + +#define INIT_THREAD_INFO (tsk) \ +{ \ + .flags = 0, \ + .user_pt_regs = (struct pt_regs *)__top_init_kernel_stack, \Should that not have a -1 on or something?
No, that symbol already accounts for subtracting the size of pt_regs.
Brian Gerst
Xin Li
maintainerRe: [PATCH v4 1/2] x86/fred: Allow variable-sized event frame
Quoted 17 lines · Brian Gerst
Quoted 4 lines · Peter Zijlstra
On Sun, May 11, 2025 at 11:43:52PM -0700, Xin Li (Intel) wrote:
Quoted 11 lines
arch/x86/include/asm/thread_info.h struct thread_info { · line 78
Quoted 9 lines
arch/x86/include/asm/thread_info.h +6
-3-#define INIT_THREAD_INFO (tsk) \ -{ \ - .flags = 0, \ +extern unsigned long __top_init_kernel_stack[]; + +#define INIT_THREAD_INFO (tsk) \ +{ \ + .flags = 0, \ + .user_pt_regs = (struct pt_regs *)__top_init_kernel_stack, \Should that not have a -1 on or something?
Quoted 1 line
No, that symbol already accounts for subtracting the size of
pt_regs.
Thanks for your answer :)
Xin Li (Intel)
maintainerPATCH v4 2/2 x86: Remove the padding space at top of the init stack
In reply to Xin Li (Intel)Because the owner of the init stack, init task, doesn't have any user level context, there will NEVER be an actual pt_regs structure pushed at top of the init stack.
However a zeroed pt_regs structure is created at build time and kept at top of the init stack for task_pt_regs() to function properly with the init task in the same manner as a normal task with user level context.
Besides, task_pt_regs() no longer converts a fixed offset from top of a task kernel stack to a pt_regs structure pointer, but rather returns whatever in the thread_info.user_pt_regs field, which is initialized at build time to '(struct pt_regs *)TOP_OF_INIT_STACK - 1' for the init task.
As a result, there is no point to reserve any padding space at top of the init stack, so remove the padding space.
--- arch/x86/include/asm/processor.h | 16 ++++++++++++++-- arch/x86/kernel/vmlinux.lds.S | 7 +++++-- 2 files changed, 19 insertions(+), 4 deletions(-)
arch/x86/include/asm/processor.h +14 -2
@@ -640,8 +640,20 @@ static __always_inline void prefetchw(const void *x) "m" (*(const char *)x)); } -#define TOP_OF_INIT_STACK ((unsigned long)&init_stack + sizeof(init_stack) - \ - TOP_OF_KERNEL_STACK_PADDING) +extern unsigned long __end_init_stack[]; + +/* +* No need to reserve extra padding space above the pt_regs structure +* at top of the init stack, because its owner init task doesn't have +* any user level context, thus there will NEVER be an actual pt_regs +* structure pushed at top of the init stack. +* +* However a zeroed pt_regs structure is created at build time and kept +* at top of the init stack for task_pt_regs() to function properly with +* the init task in the same manner as a normal task with user level +* context. +*/ +#define TOP_OF_INIT_STACK ((unsigned long)&__end_init_stack)#define task_top_of_stack (task) ((unsigned long)(task_pt_regs(task) + 1))
arch/x86/kernel/vmlinux.lds.S +5 -2
@@ -181,8 +181,11 @@ SECTIONS /* init_task */ INIT_TASK_DATA(THREAD_SIZE) - /* equivalent to task_pt_regs(&init_task) */ - __top_init_kernel_stack = __end_init_stack - TOP_OF_KERNEL_STACK_PADDING - PTREGS_SIZE; + /* + * task_pt_regs(&init_task) is: + * '(struct pt_regs *)&__end_init_stack - 1' + */ + __top_init_kernel_stack = __end_init_stack - PTREGS_SIZE; #ifdef CONFIG_X86_32 /* 32 bit has nosave before _edata */
Signature · 1 line
2.49.0