Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932491AbbB0Wjv (ORCPT ); Fri, 27 Feb 2015 17:39:51 -0500 Received: from mail-la0-f52.google.com ([209.85.215.52]:34204 "EHLO mail-la0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754962AbbB0Wjk (ORCPT ); Fri, 27 Feb 2015 17:39:40 -0500 MIME-Version: 1.0 In-Reply-To: <54F0DD9D.8040701@redhat.com> References: <54F0979D.5060001@redhat.com> <54F0DD9D.8040701@redhat.com> From: Andy Lutomirski Date: Fri, 27 Feb 2015 14:39:18 -0800 Message-ID: Subject: Re: [RFC 2/3] x86: Switch all C consumers of kernel_stack to this_cpu_sp0 To: Denys Vlasenko Cc: "linux-kernel@vger.kernel.org" , Borislav Petkov , X86 ML , Konrad Rzeszutek Wilk , Rusty Russell , Boris Ostrovsky , Oleg Nesterov Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3664 Lines: 88 On Feb 27, 2015 1:12 PM, "Denys Vlasenko" wrote: > > On 02/27/2015 08:56 PM, Andy Lutomirski wrote: > > On Feb 27, 2015 8:13 AM, "Denys Vlasenko" wrote: > >> > >> On 02/27/2015 01:07 AM, Andy Lutomirski wrote: > >>> This will make modifying the semantics of kernel_stack easier. > >>> > >>> Cc: Konrad Rzeszutek Wilk > >>> Cc: Boris Ostrovsky > >>> Cc: Rusty Russell > >>> Signed-off-by: Andy Lutomirski > >>> --- > >>> arch/x86/include/asm/thread_info.h | 3 +-- > >>> arch/x86/kernel/traps.c | 2 +- > >>> 2 files changed, 2 insertions(+), 3 deletions(-) > >>> > >>> diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/thread_info.h > >>> index e82e95abc92b..92549053d86d 100644 > >>> --- a/arch/x86/include/asm/thread_info.h > >>> +++ b/arch/x86/include/asm/thread_info.h > >>> @@ -163,8 +163,7 @@ DECLARE_PER_CPU(unsigned long, kernel_stack); > >>> static inline struct thread_info *current_thread_info(void) > >>> { > >>> struct thread_info *ti; > >>> - ti = (void *)(this_cpu_read_stable(kernel_stack) + > >>> - KERNEL_STACK_OFFSET - THREAD_SIZE); > >>> + ti = (void *)(this_cpu_sp0() - THREAD_SIZE); > >>> return ti; > >>> } > >>> > >>> diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c > >>> index c74f2f5652da..d287ea779728 100644 > >>> --- a/arch/x86/kernel/traps.c > >>> +++ b/arch/x86/kernel/traps.c > >>> @@ -174,7 +174,7 @@ void ist_begin_non_atomic(struct pt_regs *regs) > >>> * will catch asm bugs and any attempt to use ist_preempt_enable > >>> * from double_fault. > >>> */ > >>> - BUG_ON(((current_stack_pointer() ^ this_cpu_read_stable(kernel_stack)) > >>> + BUG_ON(((current_stack_pointer() ^ (this_cpu_sp0() - 1)) > >>> & ~(THREAD_SIZE - 1)) != 0); > >> > >> While we are at it, I propose a more readable version of this check: > >> > >> BUG_ON(this_cpu_sp0() - current_stack_pointer() >= THREAD_SIZE); > >> > >> Yes, I am aware that it is not equivalent to the existing condition > >> - it uses the fact that this_cpu_sp0(), previous check > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > > Oops... > I meant to say "...the fact that this_cpu_sp0() points to the very top > of the stack, previous check ..." > > >> wasn't making that assumption. But that assumption is true, > >> so shouldn't be a problem. > > > > You're missing an absolute value in here, though. This isn't a check > > for overflow; it's a check that we aren't on an IST or other per cpu > > stack. > > Yes, that's exactly what the condition checks for. It reads > > "is current stack pointer below task's kernel stack by no more > than THREAD_SIZE?" > > which is only possible if we are on task's kernel stack: > > If current_stack_pointer() is elsewhere, it is either > (a) much smaller than this_cpu_sp0(), and BUG_ON condition > obviously triggers; or > (b) it is somewhere above this_cpu_sp0(), in which case subtraction > overflows and condition triggers too. > Right, it's unsigned. I like yours better if I add a comment. It also avoids relying on the stack being aligned to its full size. --Andy > How would you write this condition so that it's easily readable? > Evidently, my version isn't as readable sa I hoped... -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/