Re: [PATCH 1/5] x86: change THREAD_INFO definition to not depend on KERNEL_STACK_OFFSET

From: Ingo Molnar
Date: Tue Mar 24 2015 - 14:09:16 EST



* Denys Vlasenko <dvlasenk@xxxxxxxxxx> wrote:

> This changes THREAD_INFO definition and all its callsites
> so that they do not count stack position from
> (top of stack - KERNEL_STACK_OFFSET), but from top of stack.
>
> Semi-mysterious expressions THREAD_INFO(%rsp,RIP) - "why RIP??"
> are now replaced by more logical THREAD_INFO(%rsp,SIZEOF_PTREGS) -
> "calculate thread_info's address using information that
> rsp is SIZEOF_PTREGS bytes below top of stack".
>
> While at it, replace "(off)-THREAD_SIZE(reg)" with equivalent
> "((off)-THREAD_SIZE)(reg)". The form without parentheses
> falsely looks like we invoke THREAD_SIZE() macro.
>
> Improve comment atop THREAD_INFO macro definition.
>
> This patch does not change generated code (verified by objdump).

> --- a/arch/x86/include/asm/thread_info.h
> +++ b/arch/x86/include/asm/thread_info.h
> @@ -207,10 +207,12 @@ static inline unsigned long current_stack_pointer(void)
> _ASM_SUB $(THREAD_SIZE-KERNEL_STACK_OFFSET),reg ;
>
> /*
> - * Same if PER_CPU_VAR(kernel_stack) is, perhaps with some offset, already in
> - * a certain register (to be used in assembler memory operands).
> + * ASM operand which evaluates to thread_info address
> + * if it is known that "reg" is exactly "off" bytes below stack top.
> + * Example (fetch thread_info->fieldname):
> + * mov TI_fieldname+THREAD_INFO(reg, off),%eax
> */
> -#define THREAD_INFO(reg, off) KERNEL_STACK_OFFSET+(off)-THREAD_SIZE(reg)
> +#define THREAD_INFO(reg, off) ((off)-THREAD_SIZE)(reg)

We need more assembly hackers, so I have improved this still somewhat
cryptic comment to:

/*
* ASM operand which evaluates to a 'thread_info' address of
* the current task, if it is known that "reg" is exactly "off"
* bytes below the top of the stack currently.
*
* ( The kernel stack's size is known at build time, it is usually
* 2 or 4 pages, and the bottom of the kernel stack contains
* the thread_info structure. So to access the thread_info very
* quickly from assembly code we can calculate down from the
* top of the kernel stack to the bottom, using constant,
* build-time calculations only. )
*
* For example, to fetch the current thread_info->flags value into %eax
* on x86-64 defconfig kernels:
*
* mov TI_flags+THREAD_INFO(%rsp, SIZEOF_PTREGS), %eax
*
* will translate to:
*
* 8b 84 24 b8 c0 ff ff mov -0x3f48(%rsp), %eax
*
* which is below the current RSP by almost 16K.
*/
#define THREAD_INFO(reg, off) ((off)-THREAD_SIZE)(reg)

Agreed?

Thanks,

Ingo
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/