Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp463529ybz; Fri, 17 Apr 2020 04:31:08 -0700 (PDT) X-Google-Smtp-Source: APiQypIS2RDiJ5Vh1BdHYAcP1GUEX2QTYwNAM/M0AnXuZb4Mxudn2FG7iCIx0cip6W4xKmJbl5Mt X-Received: by 2002:a50:cf8a:: with SMTP id h10mr2527592edk.142.1587123068211; Fri, 17 Apr 2020 04:31:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587123068; cv=none; d=google.com; s=arc-20160816; b=QpCQg8YtW4UugKa4MhdDDfAthfdfNrR1GnlTM7V9IM8/gp9oi6Mf9NP7TsPSDPtJA/ GJy+5Zsq8v6PjlJorfN6T54GpcjXCxeo6t3p2rmnkm2xJ9HOEo0FV1xyxZDbf+4y2kCs EqdKoeDFHymGQs2Rf+26rwvWamXTE0ZsHAichwk2/xXbXklPZXW4U8qLHY4HTti0u0dr pb3c0fYrExdhBZSOSRVJ8TNFby69xmbcjXr1YfdzSUBcquo7cOXCs42055ghHPpfhoAJ KgonUzfq6zPsMTcvFhloQwGckowmA9QT67Cat3BLs5Z2lykUDDLirAebnW6R+QcmLzau nh+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date; bh=2qDL1y7SYd06Hge6htubhJTZGkH6Hh5oV4//UIiMuG8=; b=BhCBAXDMfKHYfSKVQxwtKGfbKkXzlRLhQYr+XX1dq/kjjskWNuN7Kc9fj/kSdbYXAf D8xpl/ENja7u5B1Fe2rYfpUgJ3wpJmvVnrDI4mprftQNj8L0OVmcOZlNbQf0gsycRiwF N6AER/z/1xKrIgCffM4jKt5UEELL/yBTfIpZU4myjjEZ+dl7R2sNRT44DqE3bpwwmMgp Bmms+dofiSDKgYr4PqqufvTZ91PVlUg/TLFpxh5+nmhSPQsttwWmkFBRZzXWeNwM9T1e x9ZFGghrs0yEHneNfjm/GD4I4iOGRNnASYOeA2VOYgwC5ymWVbWQ9zw4aPFFBDhpI6nH kGGQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v1si2952720edr.198.2020.04.17.04.30.44; Fri, 17 Apr 2020 04:31:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730301AbgDQL3g (ORCPT + 99 others); Fri, 17 Apr 2020 07:29:36 -0400 Received: from mx2.suse.de ([195.135.220.15]:54828 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730237AbgDQL3f (ORCPT ); Fri, 17 Apr 2020 07:29:35 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id A3C81ABEF; Fri, 17 Apr 2020 11:29:33 +0000 (UTC) Date: Fri, 17 Apr 2020 13:29:32 +0200 (CEST) From: Miroslav Benes To: Peter Zijlstra cc: tglx@linutronix.de, jpoimboe@redhat.com, linux-kernel@vger.kernel.org, x86@kernel.org, mhiramat@kernel.org, jthierry@redhat.com, alexandre.chartre@oracle.com Subject: Re: [PATCH v5 02/17] objtool: Better handle IRET In-Reply-To: <20200416115118.631224674@infradead.org> Message-ID: References: <20200416114706.625340212@infradead.org> <20200416115118.631224674@infradead.org> User-Agent: Alpine 2.21 (LSU 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 16 Apr 2020, Peter Zijlstra wrote: > Teach objtool a little more about IRET so that we can avoid using the > SAVE/RESTORE annotation. In particular, make the weird corner case in > insn->restore go away. > > The purpose of that corner case is to deal with the fact that > UNWIND_HINT_RESTORE lands on the instruction after IRET, but that > instruction can end up being outside the basic block, consider: > > if (cond) > sync_core() > foo(); > > Then the hint will land on foo(), and we'll encounter the restore > hint without ever having seen the save hint. > > By teaching objtool about the arch specific exception frame size, and > assuming that any IRET in an STT_FUNC symbol is an exception frame > sized POP, we can remove the use of save/restore hints for this code. > > Signed-off-by: Peter Zijlstra (Intel) > --- > arch/x86/include/asm/processor.h | 2 -- > tools/objtool/arch.h | 1 + > tools/objtool/arch/x86/decode.c | 14 ++++++++++++-- > tools/objtool/check.c | 29 ++++++++++++++++------------- > 4 files changed, 29 insertions(+), 17 deletions(-) > > --- a/arch/x86/include/asm/processor.h > +++ b/arch/x86/include/asm/processor.h > @@ -727,7 +727,6 @@ static inline void sync_core(void) > unsigned int tmp; > > asm volatile ( > - UNWIND_HINT_SAVE > "mov %%ss, %0\n\t" > "pushq %q0\n\t" > "pushq %%rsp\n\t" > @@ -737,7 +736,6 @@ static inline void sync_core(void) > "pushq %q0\n\t" > "pushq $1f\n\t" > "iretq\n\t" > - UNWIND_HINT_RESTORE > "1:" > : "=&r" (tmp), ASM_CALL_CONSTRAINT : : "cc", "memory"); > #endif > --- a/tools/objtool/arch.h > +++ b/tools/objtool/arch.h > @@ -19,6 +19,7 @@ enum insn_type { > INSN_CALL, > INSN_CALL_DYNAMIC, > INSN_RETURN, > + INSN_EXCEPTION_RETURN, > INSN_CONTEXT_SWITCH, > INSN_STACK, > INSN_BUG, > --- a/tools/objtool/arch/x86/decode.c > +++ b/tools/objtool/arch/x86/decode.c > @@ -435,9 +435,19 @@ int arch_decode_instruction(struct elf * > *type = INSN_RETURN; > break; > > + case 0xcf: /* iret */ > + *type = INSN_EXCEPTION_RETURN; > + > + /* add $40, %rsp */ > + op->src.type = OP_SRC_ADD; > + op->src.reg = CFI_SP; > + op->src.offset = 5*8; > + op->dest.type = OP_DEST_REG; > + op->dest.reg = CFI_SP; > + break; > + > case 0xca: /* retf */ > case 0xcb: /* retf */ > - case 0xcf: /* iret */ > *type = INSN_CONTEXT_SWITCH; > break; > > @@ -483,7 +493,7 @@ int arch_decode_instruction(struct elf * > > *immediate = insn.immediate.nbytes ? insn.immediate.value : 0; > > - if (*type == INSN_STACK) > + if (*type == INSN_STACK || *type == INSN_EXCEPTION_RETURN) > list_add_tail(&op->list, ops_list); > else > free(op); > --- a/tools/objtool/check.c > +++ b/tools/objtool/check.c > @@ -2080,15 +2080,14 @@ static int validate_return(struct symbol > * tools/objtool/Documentation/stack-validation.txt. > */ > static int validate_branch(struct objtool_file *file, struct symbol *func, > - struct instruction *first, struct insn_state state) > + struct instruction *insn, struct insn_state state) > { > struct alternative *alt; > - struct instruction *insn, *next_insn; > + struct instruction *next_insn; > struct section *sec; > u8 visited; > int ret; > > - insn = first; > sec = insn->sec; > > if (insn->alt_group && list_empty(&insn->alts)) { > @@ -2141,16 +2140,6 @@ static int validate_branch(struct objtoo > } > > if (!save_insn->visited) { > - /* > - * Oops, no state to copy yet. > - * Hopefully we can reach this > - * instruction from another branch > - * after the save insn has been > - * visited. > - */ > - if (insn == first) > - return 0; > - > WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo", > sec, insn->offset); > return 1; > @@ -2243,6 +2232,20 @@ static int validate_branch(struct objtoo > > break; > > + case INSN_EXCEPTION_RETURN: > + if (handle_insn_ops(insn, &state)) > + return 1; > + > + /* > + * This handles x86's sync_core() case, where we use an > + * IRET to self. All 'normal' IRET instructions are in > + * STT_NOTYPE entry symbols. > + */ > + if (func) > + break; > + > + return 0; > + > case INSN_CONTEXT_SWITCH: > if (func && (!next_insn || !next_insn->hint)) { > WARN_FUNC("unsupported instruction in callable function", It looks really simple. Have you tried Julien's proposal about removing INSN_STACK altogether, move the x86 to arch/x86/ and call handle_insn_ops() unconditionally, or have you just postponed it? As I said, I think it could be better in the long term, but the above looks good for now as well. Miroslav