Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp3490761pxp; Tue, 8 Mar 2022 15:48:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJwbA7pJUfRv8p+OGBgbDMrUTLCCanfGdDlWrekogyQPDqwWoRvq6+hmN3IjubKVNHzIw/Zg X-Received: by 2002:a65:6d87:0:b0:374:2525:dcb0 with SMTP id bc7-20020a656d87000000b003742525dcb0mr16071006pgb.248.1646783328945; Tue, 08 Mar 2022 15:48:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646783328; cv=none; d=google.com; s=arc-20160816; b=AZAAfJF1C9oX/aAlOfcmWo+fcbRaxw6F8wKoOBWhjzw7tcrs2qFuHIsweFiOxwuIpG TkHU0C77TX7HB+UW+gcG8h17ec7xmCOXfqPBCD9y63vWqPuQxIgwIK3EAcrUl/MfzT57 XUzUqRvlFmTr8QmZT5ZUdo0CSuQHXIYDRcLfji32J6qLXXoh0t0yRXPD02yYRUHzySm3 KNUHHMGGx+/V/0RwH3uYKiuTRBbRcS7Zq5COd7CDLGgoN0MO+WGMSRXvtUOpXszWjYfM n7GmPwd1ZdSnSujvnLin22MiROwhCW2uy+CDv8xvSa5A39I2SiQ/euJlJ8mZ2ZfvyAIU tZ9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=fuN8TErmMyd6I+TjaRyzbHYSOxjkE9R1TGGItw/QneI=; b=SUOzkvovrEys6xfHcjwxeJRLHvf3TaQuI31sUEVTmTQVIPtyw6cH4+hKSFg4pEttin JcL2ls6BYwj1hpS0wp/o+2hV+OLFqgccmaEJywpbZ/9NZ0EZsHyNnNUBY7apMkYU9wiC rdzHvuQ6cg+ZEVhxrsfxoOVF5+hLV3kPbDtKzYm81dXLr0TA+EpQBaBoW/Jq8POJ2aGB CZ4061cu6+WZVGqFxblBUI2OiY/j7UPid3x3Ag9dV/k2U8MPO3TSRP9ZpFXEY875ns6a mZVKp1RwEEsRo4oxylxANj2ssxke85+O39/nWPqrA/qeP9lfuQfLHse0ULsyqy4CggpJ pBqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=VXbc5n+p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id q20-20020a056a00085400b004e1601efd6bsi269172pfk.183.2022.03.08.15.48.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Mar 2022 15:48:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=VXbc5n+p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 61447C4E30; Tue, 8 Mar 2022 15:29:07 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346285AbiCHLKw (ORCPT + 99 others); Tue, 8 Mar 2022 06:10:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234013AbiCHLKq (ORCPT ); Tue, 8 Mar 2022 06:10:46 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 792BE424BD; Tue, 8 Mar 2022 03:09:50 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 2C285B817CC; Tue, 8 Mar 2022 11:09:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8151CC340EB; Tue, 8 Mar 2022 11:09:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1646737787; bh=u3LXU9B0NsFiJGyUROaEhGHNAJ3nalwJrXEmn9uAx90=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VXbc5n+pOTrB24RFFI7VL7p6tzU0pPNI2P9X7Wm09DBLuRFFzBQuV1jYaga9Zj+cC 4XfMcctO941+494jzCbEnBJ44aKAMWV6UaSbPwP55w2Q5LhCeQo/n9XiDi4e+MUYMp BBB1/3hBDC793SPfcjAMpDzyZ+UgXmy5rAf1PFqDA/cbm4TbdwP7jSHlS2Zgb328U5 hiMqa0sYmIYbuV3paicN8J3khJxz/m4gGNI/8/sjUseseSuraNJHmiaNk7fvYeRij3 RAT73XGzw3k+lgtKGon6vy5ruzRnn1M+D1sgledfYJLwy90fUZb6lO8OLE4IQwKttv LU16LMxxLd+JA== From: Masami Hiramatsu To: Jiri Olsa , Alexei Starovoitov Cc: Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu , netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt , "Naveen N . Rao" , Anil S Keshavamurthy , "David S . Miller" Subject: [PATCH v10 06/12] powerpc: Add rethook support Date: Tue, 8 Mar 2022 20:09:42 +0900 Message-Id: <164673778191.1984170.16191629286250415895.stgit@devnote2> X-Mailer: git-send-email 2.25.1 In-Reply-To: <164673771096.1984170.8155877393151850116.stgit@devnote2> References: <164673771096.1984170.8155877393151850116.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add rethook powerpc64 implementation. Most of the code has been copied from kretprobes on powerpc64. Signed-off-by: Masami Hiramatsu --- Changes in v10: - Add a dummy @mcount to arch_rethook_prepare(). --- arch/powerpc/Kconfig | 1 + arch/powerpc/kernel/Makefile | 1 + arch/powerpc/kernel/rethook.c | 72 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 arch/powerpc/kernel/rethook.c diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig index b779603978e1..5feaa241fb56 100644 --- a/arch/powerpc/Kconfig +++ b/arch/powerpc/Kconfig @@ -229,6 +229,7 @@ config PPC select HAVE_PERF_EVENTS_NMI if PPC64 select HAVE_PERF_REGS select HAVE_PERF_USER_STACK_DUMP + select HAVE_RETHOOK if KPROBES select HAVE_REGS_AND_STACK_ACCESS_API select HAVE_RELIABLE_STACKTRACE select HAVE_RSEQ diff --git a/arch/powerpc/kernel/Makefile b/arch/powerpc/kernel/Makefile index 4d7829399570..feb24ea83ca6 100644 --- a/arch/powerpc/kernel/Makefile +++ b/arch/powerpc/kernel/Makefile @@ -115,6 +115,7 @@ obj-$(CONFIG_SMP) += smp.o obj-$(CONFIG_KPROBES) += kprobes.o obj-$(CONFIG_OPTPROBES) += optprobes.o optprobes_head.o obj-$(CONFIG_KPROBES_ON_FTRACE) += kprobes-ftrace.o +obj-$(CONFIG_RETHOOK) += rethook.o obj-$(CONFIG_UPROBES) += uprobes.o obj-$(CONFIG_PPC_UDBG_16550) += legacy_serial.o udbg_16550.o obj-$(CONFIG_SWIOTLB) += dma-swiotlb.o diff --git a/arch/powerpc/kernel/rethook.c b/arch/powerpc/kernel/rethook.c new file mode 100644 index 000000000000..a8a128748efa --- /dev/null +++ b/arch/powerpc/kernel/rethook.c @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * PowerPC implementation of rethook. This depends on kprobes. + */ + +#include +#include + +/* + * Function return trampoline: + * - init_kprobes() establishes a probepoint here + * - When the probed function returns, this probe + * causes the handlers to fire + */ +asm(".global arch_rethook_trampoline\n" + ".type arch_rethook_trampoline, @function\n" + "arch_rethook_trampoline:\n" + "nop\n" + "blr\n" + ".size arch_rethook_trampoline, .-arch_rethook_trampoline\n"); + +/* + * Called when the probe at kretprobe trampoline is hit + */ +static int trampoline_rethook_handler(struct kprobe *p, struct pt_regs *regs) +{ + unsigned long orig_ret_address; + + orig_ret_address = rethook_trampoline_handler(regs, 0); + /* + * We get here through one of two paths: + * 1. by taking a trap -> kprobe_handler() -> here + * 2. by optprobe branch -> optimized_callback() -> opt_pre_handler() -> here + * + * When going back through (1), we need regs->nip to be setup properly + * as it is used to determine the return address from the trap. + * For (2), since nip is not honoured with optprobes, we instead setup + * the link register properly so that the subsequent 'blr' in + * __kretprobe_trampoline jumps back to the right instruction. + * + * For nip, we should set the address to the previous instruction since + * we end up emulating it in kprobe_handler(), which increments the nip + * again. + */ + regs_set_return_ip(regs, orig_ret_address - 4); + regs->link = orig_ret_address; + + return 0; +} +NOKPROBE_SYMBOL(trampoline_rethook_handler); + +void arch_rethook_prepare(struct rethook_node *rh, struct pt_regs *regs, bool mcount) +{ + rh->ret_addr = regs->link; + rh->frame = 0; + + /* Replace the return addr with trampoline addr */ + regs->link = (unsigned long)arch_rethook_trampoline; +} +NOKPROBE_SYMBOL(arch_prepare_kretprobe); + +static struct kprobe trampoline_p = { + .addr = (kprobe_opcode_t *) &arch_rethook_trampoline, + .pre_handler = trampoline_rethook_handler +}; + +static int init_arch_rethook(void) +{ + return register_kprobe(&trampoline_p); +} + +core_initcall(init_arch_rethook);