Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp6787147ybi; Wed, 5 Jun 2019 06:26:09 -0700 (PDT) X-Google-Smtp-Source: APXvYqyhkaF4TSQZP3V3cKeCXif1q1U7M4VBSargW8XPtpXonw6ICDTgRkYtUEmiYC8zjjd87Qp+ X-Received: by 2002:a63:78cf:: with SMTP id t198mr4262467pgc.82.1559741169052; Wed, 05 Jun 2019 06:26:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559741169; cv=none; d=google.com; s=arc-20160816; b=pSgn8QfVa97wy0StBAg3YFTCysBMCeiOfqVYtkoPqzlNQ85Qnw5/GrooztFx+s2r2G 5jZ3w0Q6Gi3IpLR55GbnnaG3oav9QnZLNFPMtwft6YLk1cHShuKwmSaprkX422d95OZr +YuLW2cx1S4f+nLhIkAtRgyHToWBZ+2zDd44KvOco0JWjTvuCcDBgpq4MPGRUwaB0Xsm TfFZNgTbjrhi46mEV4rx3ZQOkY7zzrPSo+XOw2Ks6OzaA2mT5jbxXrISac4prdp8Ekhy CRQXgfGmerJ2BHoOmFO3dbH5oocPj6SYurjs5P8ou7zcwrY590LE+hotiDg7d0Ywf5ZZ re2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:subject:cc:to :from:date:user-agent:message-id:dkim-signature; bh=2cRvEiy6AX1c/UshB3E5LNGwtAgyIx6T+wlXuCDBGJE=; b=MIOdb4l7Mckk4dgQzGMZeDcjJvHNxEOMEeR5Hd0G4BUj5f18EHLFhp5B4R461HjGiE Yi0jthiVpgHmyav8j6SE+zxl+Fhr4K7VoChKoEPTajTFi4tgfQ06HskkB5iFWVnXjFOg dqQYowrw/uLVyGjh+oedE/4vY6gLdRl03HEpeBc+0CZ7nkqwiuTkjkxOvzzUBNSO8zCh HZlzD+exOWQZafNrsO7i9EV7DuhiaxgV++zpXETLOql43W41Y3fxLlwTdc7FUnbvfAyS jtrI2qokpouEOOgEVBI4AImXSrp7QnHruwlJ+Gn6apW4lUgS/qa6//9wWknV7/MI8565 8HOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=JdfIz4NG; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o5si2772212pju.93.2019.06.05.06.25.52; Wed, 05 Jun 2019 06:26:09 -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=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=JdfIz4NG; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728116AbfFENXW (ORCPT + 99 others); Wed, 5 Jun 2019 09:23:22 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:40792 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728059AbfFENXR (ORCPT ); Wed, 5 Jun 2019 09:23:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-Id:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=2cRvEiy6AX1c/UshB3E5LNGwtAgyIx6T+wlXuCDBGJE=; b=JdfIz4NGxt/cRt+OmVDl21SH4n VuBL7FQRRQ1FrGW7RSH+Kmebn2uJ7LiQd5ubQbmGMMkhKjw+jr8WUeKNAE1mtahF3NQzyTp3s2OrM 2u7bVrRifUGrCwgKyjKlq8TE34/D3hp+nC2/jIx5nXpb7KgGJMWdLDcCyZgYGQxqEQp2WMtIxQbsq B8z91OTbsjvKHJVulow1tzZWxBZk6FwddiIOJwo3xshCOekgvJQemw+WuxNk1AiDt0IZTHcHLoq/r QcF+Twgk5z1Zbq7IJA07VGk0riXGdbgfrH6JhmbB3lSd1ZC/OZk2G1NL+40+MRkAl6583VqbGnhtq /F3IwsnQ==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1hYVsK-0004qa-Q6; Wed, 05 Jun 2019 13:22:44 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 0) id 9B42D20757B45; Wed, 5 Jun 2019 15:22:39 +0200 (CEST) Message-Id: <20190605131945.254721704@infradead.org> User-Agent: quilt/0.65 Date: Wed, 05 Jun 2019 15:08:05 +0200 From: Peter Zijlstra To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Andy Lutomirski , Steven Rostedt , Peter Zijlstra , Ingo Molnar , Thomas Gleixner , Linus Torvalds , Masami Hiramatsu , Jason Baron , Jiri Kosina , David Laight , Borislav Petkov , Julia Cartwright , Jessica Yu , "H. Peter Anvin" , Nadav Amit , Rasmus Villemoes , Edward Cree , Daniel Bristot de Oliveira , Josh Poimboeuf Subject: [PATCH 12/15] x86/static_call: Add out-of-line static call implementation References: <20190605130753.327195108@infradead.org> MIME-Version: 1.0 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 From: Josh Poimboeuf Add the x86 out-of-line static call implementation. For each key, a permanent trampoline is created which is the destination for all static calls for the given key. The trampoline has a direct jump which gets patched by static_call_update() when the destination function changes. Cc: x86@kernel.org Cc: Steven Rostedt Cc: Julia Cartwright Cc: Ingo Molnar Cc: Ard Biesheuvel Cc: Jason Baron Cc: Linus Torvalds Cc: Jiri Kosina Cc: Thomas Gleixner Cc: Masami Hiramatsu Cc: Borislav Petkov Cc: David Laight Cc: Jessica Yu Cc: Andy Lutomirski Cc: "H. Peter Anvin" Signed-off-by: Josh Poimboeuf Signed-off-by: Peter Zijlstra (Intel) Link: https://lkml.kernel.org/r/00b08f2194e80241decbf206624b6580b9b8855b.1543200841.git.jpoimboe@redhat.com --- arch/x86/Kconfig | 1 arch/x86/include/asm/static_call.h | 28 +++++++++++++++++++++++++++ arch/x86/kernel/Makefile | 1 arch/x86/kernel/static_call.c | 38 +++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 arch/x86/include/asm/static_call.h create mode 100644 arch/x86/kernel/static_call.c --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -198,6 +198,7 @@ config X86 select HAVE_FUNCTION_ARG_ACCESS_API select HAVE_STACKPROTECTOR if CC_HAS_SANE_STACKPROTECTOR select HAVE_STACK_VALIDATION if X86_64 + select HAVE_STATIC_CALL select HAVE_RSEQ select HAVE_SYSCALL_TRACEPOINTS select HAVE_UNSTABLE_SCHED_CLOCK --- /dev/null +++ b/arch/x86/include/asm/static_call.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_STATIC_CALL_H +#define _ASM_STATIC_CALL_H + +/* + * Manually construct a 5-byte direct JMP to prevent the assembler from + * optimizing it into a 2-byte JMP. + */ +#define __ARCH_STATIC_CALL_JMP_LABEL(key) ".L" __stringify(key ## _after_jmp) +#define __ARCH_STATIC_CALL_TRAMP_JMP(key, func) \ + ".byte 0xe9 \n" \ + ".long " #func " - " __ARCH_STATIC_CALL_JMP_LABEL(key) "\n" \ + __ARCH_STATIC_CALL_JMP_LABEL(key) ":" + +/* + * This is a permanent trampoline which does a direct jump to the function. + * The direct jump get patched by static_call_update(). + */ +#define ARCH_DEFINE_STATIC_CALL_TRAMP(key, func) \ + asm(".pushsection .text, \"ax\" \n" \ + ".align 4 \n" \ + ".globl " STATIC_CALL_TRAMP_STR(key) " \n" \ + ".type " STATIC_CALL_TRAMP_STR(key) ", @function \n" \ + STATIC_CALL_TRAMP_STR(key) ": \n" \ + __ARCH_STATIC_CALL_TRAMP_JMP(key, func) " \n" \ + ".popsection \n") + +#endif /* _ASM_STATIC_CALL_H */ --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -63,6 +63,7 @@ obj-y += tsc.o tsc_msr.o io_delay.o rt obj-y += pci-iommu_table.o obj-y += resource.o obj-y += irqflags.o +obj-y += static_call.o obj-y += process.o obj-y += fpu/ --- /dev/null +++ b/arch/x86/kernel/static_call.c @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include + +#define CALL_INSN_SIZE 5 + +void arch_static_call_transform(void *site, void *tramp, void *func) +{ + unsigned char opcodes[CALL_INSN_SIZE]; + unsigned char insn_opcode; + unsigned long insn; + s32 dest_relative; + + mutex_lock(&text_mutex); + + insn = (unsigned long)tramp; + + insn_opcode = *(unsigned char *)insn; + if (insn_opcode != 0xE9) { + WARN_ONCE(1, "unexpected static call insn opcode 0x%x at %pS", + insn_opcode, (void *)insn); + goto unlock; + } + + dest_relative = (long)(func) - (long)(insn + CALL_INSN_SIZE); + + opcodes[0] = insn_opcode; + memcpy(&opcodes[1], &dest_relative, CALL_INSN_SIZE - 1); + + text_poke_bp((void *)insn, opcodes, CALL_INSN_SIZE, NULL); + +unlock: + mutex_unlock(&text_mutex); +} +EXPORT_SYMBOL_GPL(arch_static_call_transform);