Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp858348yba; Thu, 9 May 2019 07:04:20 -0700 (PDT) X-Google-Smtp-Source: APXvYqx3QwO6p4PuDBTbabFZc/awcYfEaSWhc0noFBcAeotZ7ax/F+bkLMjqtxRtzOA4ZTgo2jM3 X-Received: by 2002:a17:902:42a5:: with SMTP id h34mr5236948pld.146.1557410660077; Thu, 09 May 2019 07:04:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557410660; cv=none; d=google.com; s=arc-20160816; b=kDMf8dmEj+fR5Ils7zRMXPhj1Rg0mvh/ao1tKqz/JKYFW+NouVfi7eLSfJ8gxfubHn 3hgoqQQvbTK1Sb/U6M/Tv9u4Vn1uYKU+YL9MgHNIAc+KKL4VQ258+16I/xgW1x92na1a IXcSLSyN3ZWAwGwhWOIYR714tXTmQ6kFMQep8MY1ADvuxM6daYHgaCbwIU6pZwkqHqVE yBQEiLdZASeMahFi5VXyj95G1TByKG7SbFlBkDO5ues9jcaMmTc2uttaFOfDPuZqdxdH Lc+mGzGq60bZEA8lekFqkzH9wONJQnk3GWn4IFHdzQsBhCIlq38FhJUO97GMEpzQBQk/ zKEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=snMZ/QECpcU2zppl7VpElQVGPLrdz5pyPysCQFVfN5Q=; b=eOO6rPYCMGiF00g6GedLyrZaAg0PojApLE4FjcHN0GryV72cMcbhGcS52ESdGxytD9 VAAneGA2YJ+9ZSnXuVTnmZQU8aLhgqeZ2vOutmcsj5hVz3UOmez+4z1qumNqtlU+QL0N 4Ypj1lBpxFSo1atB8UVcGsTj9146/unYnY3+PIhjJoWL6TvGNOlD3oHXlgCMgX+HmrJl 5cbH3npIs6i6MwQgtOgoMV5fGL7NzGRH9nX8vIkHRwn1HGu68WS9TQGtpP7MNrSTm13k ZbGoz4AcI2VevqZP2QQ7jf4OR1b3PRqCdVzdEbWXccwX7DzA73FxczBZLYGWEcD8b+j8 TiUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nXxvOXjK; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p7si3085107pgi.276.2019.05.09.07.03.52; Thu, 09 May 2019 07:04:20 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nXxvOXjK; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726680AbfEIOBR (ORCPT + 99 others); Thu, 9 May 2019 10:01:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:44748 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726192AbfEIOBQ (ORCPT ); Thu, 9 May 2019 10:01:16 -0400 Received: from devnote2 (NE2965lan1.rev.em-net.ne.jp [210.141.244.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CA3492089E; Thu, 9 May 2019 14:01:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1557410474; bh=CeMS/sCd8zS33MpeYALcLP/Yqm9rGmSNsKFNL7ZikOA=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=nXxvOXjKh6V0/dPiWa8hgkLmu0/Skk6GzVerSrb3HrHtR+sq0k4Cfhi3Ifbq95m/b pfGhLtaCKZ2nGUjTQjRczj6HH5/OzviuUXMUTJ9JNI1nVHs2ilpo1L4t3bzILekL7/ nOuTQzGuSpRgzGx2tMjHQDTRBFsiAS82DV/EuU90= Date: Thu, 9 May 2019 23:01:06 +0900 From: Masami Hiramatsu To: Peter Zijlstra Cc: Josh Poimboeuf , linux-kernel@vger.kernel.org, Linus Torvalds , Ingo Molnar , Andrew Morton , Andy Lutomirski , Nicolai Stange , Thomas Gleixner , Borislav Petkov , "H. Peter Anvin" , x86@kernel.org, Jiri Kosina , Miroslav Benes , Petr Mladek , Joe Lawrence , Shuah Khan , Konrad Rzeszutek Wilk , Tim Chen , Sebastian Andrzej Siewior , Mimi Zohar , Juergen Gross , Nick Desaulniers , Nayna Jain , Masahiro Yamada , Joerg Roedel , linux-kselftest@vger.kernel.org Subject: Re: [PATCH 2/4] x86/kprobes: Fix frame pointer annotations Message-Id: <20190509230106.3551b08553440d125e437f66@kernel.org> In-Reply-To: <20190509081431.GO2589@hirez.programming.kicks-ass.net> References: <20190508074901.982470324@infradead.org> <20190508080612.721269814@infradead.org> <20190508115416.nblx7c2kocidpytm@treble> <20190508120416.GL2589@hirez.programming.kicks-ass.net> <20190508124248.u5ukpbhnh4wpiccq@treble> <20190508153907.GM2589@hirez.programming.kicks-ass.net> <20190508184848.qerg3flv3ej3xsev@treble> <20190509102030.dfa62e058f09d0d8cbdd6053@kernel.org> <20190509081431.GO2589@hirez.programming.kicks-ass.net> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.32; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 9 May 2019 10:14:31 +0200 Peter Zijlstra wrote: > On Thu, May 09, 2019 at 10:20:30AM +0900, Masami Hiramatsu wrote: > > Hi Josh, > > > > On Wed, 8 May 2019 13:48:48 -0500 > > Josh Poimboeuf wrote: > > > > > On Wed, May 08, 2019 at 05:39:07PM +0200, Peter Zijlstra wrote: > > > > On Wed, May 08, 2019 at 07:42:48AM -0500, Josh Poimboeuf wrote: > > > > > On Wed, May 08, 2019 at 02:04:16PM +0200, Peter Zijlstra wrote: > > > > > > > > > > Do the x86_64 variants also want some ORC annotation? > > > > > > > > > > Maybe so. Though it looks like regs->ip isn't saved. The saved > > > > > registers might need to be tweaked. I'll need to look into it. > > > > > > > > What all these sites do (and maybe we should look at unifying them > > > > somehow) is turn a CALL frame (aka RET-IP) into an exception frame (aka > > > > pt_regs). > > > > > > > > So regs->ip will be the return address (which is fixed up to be the CALL > > > > address in the handler). > > > > > > But from what I can tell, trampoline_handler() hard-codes regs->ip to > > > point to kretprobe_trampoline(), and the original return address is > > > placed in regs->sp. > > > > > > Masami, is there a reason why regs->ip doesn't have the original return > > > address and regs->sp doesn't have the original SP? I think that would > > > help the unwinder understand things. > > > > Yes, for regs->ip, there is a histrical reason. Since previously, we had > > an int3 at trampoline, so the user (kretprobe) handler expects that > > regs->ip is trampoline address and ri->ret_addr is original return address. > > It is better to check the other archs, but I think it is possible to > > change the regs->ip to original return address, since no one cares such > > "fixed address". :) > > > > For the regs->sp, there are 2 reasons. > > > > For x86-64, it's just for over-optimizing (reduce stack usage). > > I think we can make a gap for putting return address, something like > > > > "kretprobe_trampoline:\n" > > #ifdef CONFIG_X86_64 > > " pushq %rsp\n" /* Make a gap for return address */ > > " pushq 0(%rsp)\n" /* Copy original stack pointer */ > > " pushfq\n" > > SAVE_REGS_STRING > > " movq %rsp, %rdi\n" > > " call trampoline_handler\n" > > /* Push the true return address to the bottom */ > > " movq %rax, 20*8(%rsp)\n" > > RESTORE_REGS_STRING > > " popfq\n" > > " addq $8, %rsp\n" /* Skip original stack pointer */ > > > > For i386 (x86-32), there is no other way to keep ®s->sp as > > the original stack pointer. It has to be changed with this series, > > maybe as same as x86-64. > > Right; I already fixed that in my patch changing i386's pt_regs. I see it, and it is good to me. :) > But what I'd love to do is something like the belwo patch, and make all > the trampolines (very much including ftrace) use that. Such that we then > only have 1 copy of this magic (well, 2 because x86_64 also needs an > implementation of this of course). OK, but I will make kretprobe integrated with func-graph tracer, since it is inefficient that we have 2 different hidden return stack... Anyway, > Changing ftrace over to this would be a little more work but it can > easily chain things a little to get its original context back: > > ENTRY(ftrace_regs_caller) > GLOBAL(ftrace_regs_func) > push ftrace_stub > push ftrace_regs_handler > jmp call_to_exception_trampoline > END(ftrace_regs_caller) > > typedef void (*ftrace_func_t)(unsigned long, unsigned long, struct ftrace_op *, struct pt_regs *); > > struct ftrace_regs_stack { > ftrace_func_t func; > unsigned long parent_ip; > }; > > void ftrace_regs_handler(struct pr_regs *regs) > { > struct ftrace_regs_stack *st = (void *)regs->sp; > ftrace_func_t func = st->func; > > regs->sp += sizeof(long); /* pop func */ Sorry, why pop here? > > func(regs->ip, st->parent_ip, function_trace_op, regs); > } > > Hmm? I didn't look into the function_graph thing, but I imagine it can > be added without too much pain. Yes, that should be good for function_graph trampoline too. We use very similar technic. > > --- > --- a/arch/x86/entry/entry_32.S > +++ b/arch/x86/entry/entry_32.S > @@ -1576,3 +1576,100 @@ ENTRY(rewind_stack_do_exit) > call do_exit > 1: jmp 1b > END(rewind_stack_do_exit) > + > +/* > + * Transforms a CALL frame into an exception frame; IOW it pretends the CALL we > + * just did was in fact scribbled with an INT3. > + * > + * Use this trampoline like: > + * > + * PUSH $func > + * JMP call_to_exception_trampoline > + * > + * $func will see regs->ip point at the CALL instruction and must therefore > + * modify regs->ip in order to make progress (just like a normal INT3 scribbled > + * CALL). > + * > + * NOTE: we do not restore any of the segment registers. > + */ > +ENTRY(call_to_exception_trampoline) > + /* > + * On entry the stack looks like: > + * > + * 2*4(%esp) > + * 1*4(%esp) RET-IP > + * 0*4(%esp) func > + * > + * transform this into: > + * > + * 19*4(%esp) > + * 18*4(%esp) gap / RET-IP > + * 17*4(%esp) gap / func > + * 16*4(%esp) ss > + * 15*415*4(%esp) sp / isn't this "&" ? > + * 14*4(%esp) flags > + * 13*4(%esp) cs > + * 12*4(%esp) ip / RET-IP > + * 11*4(%esp) orig_eax > + * 10*4(%esp) gs > + * 9*4(%esp) fs > + * 8*4(%esp) es > + * 7*4(%esp) ds > + * 6*4(%esp) eax > + * 5*4(%esp) ebp > + * 4*4(%esp) edi > + * 3*4(%esp) esi > + * 2*4(%esp) edx > + * 1*4(%esp) ecx > + * 0*4(%esp) ebx > + */ > + pushl %ss > + pushl %esp # points at ss > + addl $3*4, (%esp) # point it at > + pushfl > + pushl %cs > + pushl 5*4(%esp) # RET-IP > + subl 5, (%esp) # point at CALL instruction > + pushl $-1 > + pushl %gs > + pushl %fs > + pushl %es > + pushl %ds > + pushl %eax > + pushl %ebp > + pushl %edi > + pushl %esi > + pushl %edx > + pushl %ecx > + pushl %ebx > + > + ENCODE_FRAME_POINTER > + > + movl %esp, %eax # 1st argument: pt_regs > + > + movl 17*4(%esp), %ebx # func > + CALL_NOSPEC %ebx > + > + movl PT_OLDESP(%esp), %eax Is PT_OLDESP(%esp) "" or "&"? > + > + movl PT_EIP(%esp), %ecx > + movl %ecx, -1*4(%eax) Ah, OK, so $func must set the true return address to regs->ip instead of returning it. > + > + movl PT_EFLAGS(%esp), %ecx > + movl %ecx, -2*4(%eax) > + > + movl PT_EAX(%esp), %ecx > + movl %ecx, -3*4(%eax) So, at this point, the stack becomes 18*4(%esp) RET-IP 17*4(%esp) eflags 16*4(%esp) eax Correct? > + > + popl %ebx > + popl %ecx > + popl %edx > + popl %esi > + popl %edi > + popl %ebp > + > + lea -3*4(%eax), %esp > + popl %eax > + popfl > + ret > +END(call_to_exception_trampoline) > --- a/arch/x86/kernel/kprobes/core.c > +++ b/arch/x86/kernel/kprobes/core.c > @@ -731,29 +731,8 @@ asm( > ".global kretprobe_trampoline\n" > ".type kretprobe_trampoline, @function\n" > "kretprobe_trampoline:\n" > - /* We don't bother saving the ss register */ > -#ifdef CONFIG_X86_64 > - " pushq %rsp\n" > - " pushfq\n" > - SAVE_REGS_STRING > - " movq %rsp, %rdi\n" > - " call trampoline_handler\n" > - /* Replace saved sp with true return address. */ > - " movq %rax, 19*8(%rsp)\n" > - RESTORE_REGS_STRING > - " popfq\n" > -#else > - " pushl %esp\n" > - " pushfl\n" > - SAVE_REGS_STRING > - " movl %esp, %eax\n" > - " call trampoline_handler\n" > - /* Replace saved sp with true return address. */ > - " movl %eax, 15*4(%esp)\n" > - RESTORE_REGS_STRING > - " popfl\n" > -#endif > - " ret\n" Here, we need a gap for storing ret-ip, because kretprobe_trampoline is the address which is returned from the target function. We have no "ret-ip" here at this point. So something like + "push $0\n" /* This is a gap, will be filled with real return address*/ > + "push trampoline_handler\n" > + "jmp call_to_exception_trampoline\n" > ".size kretprobe_trampoline, .-kretprobe_trampoline\n" > ); > NOKPROBE_SYMBOL(kretprobe_trampoline); > @@ -791,12 +770,7 @@ static __used void *trampoline_handler(s > > INIT_HLIST_HEAD(&empty_rp); > kretprobe_hash_lock(current, &head, &flags); > - /* fixup registers */ > - regs->cs = __KERNEL_CS; > -#ifdef CONFIG_X86_32 > - regs->cs |= get_kernel_rpl(); > - regs->gs = 0; > -#endif > + > /* We use pt_regs->sp for return address holder. */ > frame_pointer = ®s->sp; > regs->ip = trampoline_address; Thank you, -- Masami Hiramatsu