Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp106596ybt; Thu, 25 Jun 2020 16:35:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwohb/WrwLLb7jlbHPfF/B0c5j6lHNF6SvFvehQ/HnZGVz7R+SWkbfFcE/XLwd7cWKBmvB5 X-Received: by 2002:a17:906:69c3:: with SMTP id g3mr190450ejs.47.1593128136795; Thu, 25 Jun 2020 16:35:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593128136; cv=none; d=google.com; s=arc-20160816; b=cpgCgsz/1NIVWr6KkeNmyZAgoy8WycLpjaspx+e4nbgHWFAe+PAJsf/NX6O9ed8PYF 0wZ6PL6wsVxazXs3QYVm+OMB8JYDUiIVw2UO2hBjbV9ksahlUg/KsIafeVRhMhF1cT/a d3aofm/jzOMr9HtsqmjGgB0LJGdggQexDjYC/scYSTb0/DUPxwFpUbKxFJUsRRvVxHmo muqdRHZUcy3Sxy8oBpObwN4oIa5JNz2JGFexI3TI7o1yTDp644P47IbvnMaXK82RWHYJ OEansWLGjRxmRryW7+H7wOd63KnM29lyUb/mC/HPYc+p5tBPmf0gKYOuNor+UUQQXNGV zvmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=HCV92AuRvbw80BHFaZCrTSuoE9qKbEHrDwHHj8qVbjk=; b=nkJ9HdcZNHPIJ9le6QdF7K/B36xzufCsKVeOq8d+HoUKpFKCfeqaghTlXorCtQ6WnB 9MrH6dSyYtW1AsTUPgBSeGO0ggKp9SuKOxXCUg3oxq4cqjqGpYrkYTVAYqH62yWfKkn/ aenBfAfyqlJtuGhDXSIYJrIlpcjZWOZrjciKlAjJv3N5duxUS5Xn/iDgn/V4aogLyW5I NZFLxSEYJxw2u+TZquROTeYglfVAnCYA8yFdYt0gtB1cgu/xtXLVObtGWjNBdK/Hi2sV qpHoqARoZDa+zB3K/LvExTXvUxIIhi6eA91F0b5ZFmQK9LgyxbKBXYXe7hLMTAZ3oJix 6xDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=EpzjcATf; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b30si9782848edj.204.2020.06.25.16.35.13; Thu, 25 Jun 2020 16:35:36 -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; dkim=pass header.i=@google.com header.s=20161025 header.b=EpzjcATf; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2405331AbgFYUzC (ORCPT + 99 others); Thu, 25 Jun 2020 16:55:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2405330AbgFYUzB (ORCPT ); Thu, 25 Jun 2020 16:55:01 -0400 Received: from mail-pl1-x643.google.com (mail-pl1-x643.google.com [IPv6:2607:f8b0:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 421FAC08C5DC for ; Thu, 25 Jun 2020 13:55:01 -0700 (PDT) Received: by mail-pl1-x643.google.com with SMTP id x11so3333831plo.7 for ; Thu, 25 Jun 2020 13:55:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=HCV92AuRvbw80BHFaZCrTSuoE9qKbEHrDwHHj8qVbjk=; b=EpzjcATfHOlWNN51fD+FPClL2ILnY2pWsOQiUtQwDXRODiQO0Dq/mLVCvOgtrJ42q9 2zQ6QUkTaH77E2qPvQB+xzZsPHPinRv/TxQUSNTG6K2c7dh6u+XwedZ54suSXb/yiRgy 8GUfMY3QvmTTBslHF06lGkqrndG4+LG07pjeN22MkzJzr4174+dntReA8MtaQbJyUG5y KvipZ1kqZcfU2pySr/OkzXbyVKIc3YhuU2txgZvjvz9LAX/KLYJ5TaF+lgWvGM0HMjz7 zmvgESfRN67Jq1zG7m28dza2375HdzOcMPAPsZ8Epp3iNNTSlm+LXYKmCoJLRD7ezQc1 Zzlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=HCV92AuRvbw80BHFaZCrTSuoE9qKbEHrDwHHj8qVbjk=; b=clSFRKU38nKdJR0F1xO1/KN59/amMEU7EPnDcv2Jub7CeE7URPFfa6XpN9VtNjvlw4 vTW4zeVCOqSs9/J7Dgy2YH55PBmT6bDuSXizPCtAfw+pWSkrSd/djwDV33N5yUXv+hy6 2DzZ9t4Rc8kAobLCjlsj/1hDHPPeu/8qLxqDMa3wvq56Nt8PZOKmLHozqHnx2mLXvpUU CtU7QV2Ktv4m1syeUr3Qjew+YewvRNLN+2jX/kcoDw6O7Fy9Rn+jaiN+JCRMZRA4L+1j PmK+klKypZDEzDzAh6VJteV/ZDJOb+owCg2CdBdeA32Fxd7gHO9neA1jfA03izWvDMXq sbvw== X-Gm-Message-State: AOAM532kYxpbZf58irG806L0fEbBjO/9iOSwGaNEnAOOzENGXzTDt+I6 8SZLwoDzicW3e2MOiWVwimgVtqaz3rGjYu3GK46F8g== X-Received: by 2002:a17:902:ab8d:: with SMTP id f13mr23724588plr.119.1593118500279; Thu, 25 Jun 2020 13:55:00 -0700 (PDT) MIME-Version: 1.0 References: <20200624203200.78870-1-samitolvanen@google.com> <20200624203200.78870-5-samitolvanen@google.com> <20200624212737.GV4817@hirez.programming.kicks-ass.net> <20200624214530.GA120457@google.com> <20200625074530.GW4817@hirez.programming.kicks-ass.net> <20200625161503.GB173089@google.com> <20200625200235.GQ4781@hirez.programming.kicks-ass.net> In-Reply-To: <20200625200235.GQ4781@hirez.programming.kicks-ass.net> From: Nick Desaulniers Date: Thu, 25 Jun 2020 13:54:48 -0700 Message-ID: Subject: Re: [RFC][PATCH] objtool,x86_64: Replace recordmcount with objtool To: Peter Zijlstra Cc: Sami Tolvanen , Steven Rostedt , Masahiro Yamada , Will Deacon , Greg Kroah-Hartman , "Paul E. McKenney" , Kees Cook , clang-built-linux , Kernel Hardening , linux-arch , Linux ARM , Linux Kbuild mailing list , LKML , linux-pci@vger.kernel.org, "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" , Josh Poimboeuf , mhelsley@vmware.com Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 25, 2020 at 1:02 PM Peter Zijlstra wrote: > > On Thu, Jun 25, 2020 at 09:15:03AM -0700, Sami Tolvanen wrote: > > On Thu, Jun 25, 2020 at 09:45:30AM +0200, Peter Zijlstra wrote: > > > > At least for x86_64 I can do a really quick take for a recordmcount pass > > > in objtool, but I suppose you also need this for ARM64 ? > > > > Sure, sounds good. arm64 uses -fpatchable-function-entry with clang, so we > > don't need recordmcount there. > > This is on top of my local pile: > > git://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git master > > which notably includes the static_call series. > > Not boot tested, but it generates the required sections and they look > more or less as expected, ymmv. > > --- > arch/x86/Kconfig | 1 - > scripts/Makefile.build | 3 ++ > scripts/link-vmlinux.sh | 2 +- > tools/objtool/builtin-check.c | 9 ++--- > tools/objtool/builtin.h | 2 +- > tools/objtool/check.c | 81 +++++++++++++++++++++++++++++++++++++++++++ > tools/objtool/check.h | 1 + > tools/objtool/objtool.h | 1 + > 8 files changed, 91 insertions(+), 9 deletions(-) > > diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig > index a291823f3f26..189575c12434 100644 > --- a/arch/x86/Kconfig > +++ b/arch/x86/Kconfig > @@ -174,7 +174,6 @@ config X86 > select HAVE_EXIT_THREAD > select HAVE_FAST_GUP > select HAVE_FENTRY if X86_64 || DYNAMIC_FTRACE > - select HAVE_FTRACE_MCOUNT_RECORD > select HAVE_FUNCTION_GRAPH_TRACER > select HAVE_FUNCTION_TRACER > select HAVE_GCC_PLUGINS > diff --git a/scripts/Makefile.build b/scripts/Makefile.build > index 2e8810b7e5ed..c774befc57da 100644 > --- a/scripts/Makefile.build > +++ b/scripts/Makefile.build > @@ -227,6 +227,9 @@ endif > ifdef CONFIG_X86_SMAP > objtool_args += --uaccess > endif > +ifdef CONFIG_DYNAMIC_FTRACE > + objtool_args += --mcount > +endif > > # 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory > # 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file > diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh > index 92dd745906f4..00c6e4f28a1a 100755 > --- a/scripts/link-vmlinux.sh > +++ b/scripts/link-vmlinux.sh > @@ -60,7 +60,7 @@ objtool_link() > local objtoolopt; > > if [ -n "${CONFIG_VMLINUX_VALIDATION}" ]; then > - objtoolopt="check" > + objtoolopt="check --vmlinux" > if [ -z "${CONFIG_FRAME_POINTER}" ]; then > objtoolopt="${objtoolopt} --no-fp" > fi > diff --git a/tools/objtool/builtin-check.c b/tools/objtool/builtin-check.c > index 4896c5a89702..a6c3a3fba67d 100644 > --- a/tools/objtool/builtin-check.c > +++ b/tools/objtool/builtin-check.c > @@ -18,7 +18,7 @@ > #include "builtin.h" > #include "objtool.h" > > -bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, validate_dup, vmlinux, fpu; > +bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, validate_dup, vmlinux, fpu, mcount; > > static const char * const check_usage[] = { > "objtool check [] file.o", > @@ -36,12 +36,13 @@ const struct option check_options[] = { > OPT_BOOLEAN('d', "duplicate", &validate_dup, "duplicate validation for vmlinux.o"), > OPT_BOOLEAN('l', "vmlinux", &vmlinux, "vmlinux.o validation"), > OPT_BOOLEAN('F', "fpu", &fpu, "validate FPU context"), > + OPT_BOOLEAN('M', "mcount", &mcount, "generate __mcount_loc"), > OPT_END(), > }; > > int cmd_check(int argc, const char **argv) > { > - const char *objname, *s; > + const char *objname; > > argc = parse_options(argc, argv, check_options, check_usage, 0); > > @@ -50,9 +51,5 @@ int cmd_check(int argc, const char **argv) > > objname = argv[0]; > > - s = strstr(objname, "vmlinux.o"); > - if (s && !s[9]) > - vmlinux = true; > - > return check(objname, false); > } > diff --git a/tools/objtool/builtin.h b/tools/objtool/builtin.h > index 7158e09d4cc9..b51d883ec245 100644 > --- a/tools/objtool/builtin.h > +++ b/tools/objtool/builtin.h > @@ -8,7 +8,7 @@ > #include > > extern const struct option check_options[]; > -extern bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, validate_dup, vmlinux, fpu; > +extern bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, validate_dup, vmlinux, fpu, mcount; > > extern int cmd_check(int argc, const char **argv); > extern int cmd_orc(int argc, const char **argv); > diff --git a/tools/objtool/check.c b/tools/objtool/check.c > index 6647a8d1545b..ee99566bdae9 100644 > --- a/tools/objtool/check.c > +++ b/tools/objtool/check.c > @@ -533,6 +533,65 @@ static int create_static_call_sections(struct objtool_file *file) > return 0; > } > > +static int create_mcount_loc_sections(struct objtool_file *file) > +{ > + struct section *sec, *reloc_sec; > + struct reloc *reloc; > + unsigned long *loc; > + struct instruction *insn; > + int idx; > + > + sec = find_section_by_name(file->elf, "__mcount_loc"); > + if (sec) { > + INIT_LIST_HEAD(&file->mcount_loc_list); > + WARN("file already has __mcount_loc section, skipping"); > + return 0; > + } > + > + if (list_empty(&file->mcount_loc_list)) > + return 0; > + > + idx = 0; > + list_for_each_entry(insn, &file->mcount_loc_list, mcount_loc_node) > + idx++; > + > + sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx); > + if (!sec) > + return -1; > + > + reloc_sec = elf_create_reloc_section(file->elf, sec, SHT_RELA); > + if (!reloc_sec) > + return -1; > + > + idx = 0; > + list_for_each_entry(insn, &file->mcount_loc_list, mcount_loc_node) { > + > + loc = (unsigned long *)sec->data->d_buf + idx; > + memset(loc, 0, sizeof(unsigned long)); > + > + reloc = malloc(sizeof(*reloc)); > + if (!reloc) { > + perror("malloc"); > + return -1; > + } > + memset(reloc, 0, sizeof(*reloc)); calloc(1, sizeof(*reloc))? > + > + reloc->sym = insn->sec->sym; > + reloc->addend = insn->offset; > + reloc->type = R_X86_64_64; > + reloc->offset = idx * sizeof(unsigned long); > + reloc->sec = reloc_sec; > + elf_add_reloc(file->elf, reloc); > + > + idx++; > + } > + > + if (elf_rebuild_reloc_section(file->elf, reloc_sec)) > + return -1; > + > + return 0; > +} > + > /* > * Warnings shouldn't be reported for ignored functions. > */ > @@ -892,6 +951,22 @@ static int add_call_destinations(struct objtool_file *file) > insn->type = INSN_NOP; > } > > + if (mcount && !strcmp(insn->call_dest->name, "__fentry__")) { > + if (reloc) { > + reloc->type = R_NONE; > + elf_write_reloc(file->elf, reloc); > + } > + > + elf_write_insn(file->elf, insn->sec, > + insn->offset, insn->len, > + arch_nop_insn(insn->len)); > + > + insn->type = INSN_NOP; > + > + list_add_tail(&insn->mcount_loc_node, > + &file->mcount_loc_list); > + } > + > /* > * Whatever stack impact regular CALLs have, should be undone > * by the RETURN of the called function. > @@ -3004,6 +3079,7 @@ int check(const char *_objname, bool orc) > INIT_LIST_HEAD(&file.insn_list); > hash_init(file.insn_hash); > INIT_LIST_HEAD(&file.static_call_list); > + INIT_LIST_HEAD(&file.mcount_loc_list); > file.c_file = !vmlinux && find_section_by_name(file.elf, ".comment"); > file.ignore_unreachables = no_unreachable; > file.hints = false; > @@ -3056,6 +3132,11 @@ int check(const char *_objname, bool orc) > goto out; > warnings += ret; > > + ret = create_mcount_loc_sections(&file); > + if (ret < 0) > + goto out; > + warnings += ret; > + > if (orc) { > ret = create_orc(&file); > if (ret < 0) > diff --git a/tools/objtool/check.h b/tools/objtool/check.h > index cd95fca0d237..01f11b5da5dd 100644 > --- a/tools/objtool/check.h > +++ b/tools/objtool/check.h > @@ -24,6 +24,7 @@ struct instruction { > struct list_head list; > struct hlist_node hash; > struct list_head static_call_node; > + struct list_head mcount_loc_node; > struct section *sec; > unsigned long offset; > unsigned int len; > diff --git a/tools/objtool/objtool.h b/tools/objtool/objtool.h > index 9a7cd0b88bd8..f604b22d22cc 100644 > --- a/tools/objtool/objtool.h > +++ b/tools/objtool/objtool.h > @@ -17,6 +17,7 @@ struct objtool_file { > struct list_head insn_list; > DECLARE_HASHTABLE(insn_hash, 20); > struct list_head static_call_list; > + struct list_head mcount_loc_list; > bool ignore_unreachables, c_file, hints, rodata; > }; > -- Thanks, ~Nick Desaulniers