From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932239AbbCXSJN (ORCPT ); Tue, 24 Mar 2015 14:09:13 -0400 Received: from mail-we0-f176.google.com ([74.125.82.176]:35350 "EHLO mail-we0-f176.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752309AbbCXSJJ (ORCPT ); Tue, 24 Mar 2015 14:09:09 -0400 Date: Tue, 24 Mar 2015 19:09:04 +0100 From: Ingo Molnar To: Denys Vlasenko Cc: Andy Lutomirski , Linus Torvalds , Steven Rostedt , Borislav Petkov , "H. Peter Anvin" , Oleg Nesterov , Frederic Weisbecker , Alexei Starovoitov , Will Drewry , Kees Cook , x86@kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/5] x86: change THREAD_INFO definition to not depend on KERNEL_STACK_OFFSET Message-ID: <20150324180904.GA15333@gmail.com> References: <1426785469-15125-1-git-send-email-dvlasenk@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1426785469-15125-1-git-send-email-dvlasenk@redhat.com> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org * Denys Vlasenko 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