From patchwork Tue Nov 2 13:11:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jianhua Liu X-Patchwork-Id: 12598843 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7CB6BC433FE for ; Tue, 2 Nov 2021 13:14:37 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 48D8360E08 for ; Tue, 2 Nov 2021 13:14:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 48D8360E08 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=DIxQX6DuCQAAPyDURASPw93ZWz72vdAUS91ui2edeqU=; b=OeUKzPRv/daY69 CKIpBN/PRnQ9mzpDWR1ybfFuE5oO4ePD8UMEs/ZFLYp/oA8k9il3YlNyjxinNemIenZBvSDiDPulO TnYZ24uPu2FrxekSz6uH98QLu+obcqfO4MaRD9T4UC4WkEfpddQibUn91Wl+KIMsIxbLc/QmKzy6P xEHrv4c9VhEhYulbg88CSqhD307G45TzMVZ7FbGmJLP7vsXLbQXTPYTVkKWuGc7FM0xGCCoD691ae 2g+MaXrFFk58jK6lTXlwxysTktTW29keMNBxGDLGQGpQWEpKg5cgmimEhjwwhc5nv3mqwy5J1bMY0 ihriAH2K1jpna9DX2JpA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mhtbM-001lJg-FF; Tue, 02 Nov 2021 13:13:20 +0000 Received: from mail-pg1-x531.google.com ([2607:f8b0:4864:20::531]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mhtbG-001lGr-Mq for linux-arm-kernel@lists.infradead.org; Tue, 02 Nov 2021 13:13:16 +0000 Received: by mail-pg1-x531.google.com with SMTP id g184so20105610pgc.6 for ; Tue, 02 Nov 2021 06:13:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=N6BBgYB9y9o/KEMrM5XpJsnZe4hC3BDSIOBT2qELU7c=; b=bWjsonUYl7kQ0UGD+SKGCAYm/02GkHQmXCEMfMrlh6Bi6hbH5WlKtQ3Y3ixXDkSQow Y6MuVIsteJRLVricj5lDvkFn5r77wAkyIXaiBW9si+4YIfHNsC6iIYVRMyuYren+iy4z 2hV4F6Px/KWOmpONM3vp9LVohzBYUUlJJSVbXJBwjHJ5HCMODkwxaRsg9c/6wPfvLaNb Q8rqSThiE5Nu0z+5lL/1ETZZKcohhbzFbJ996+OMQAEjwzPRp2orrE6gwvbisAqB6sWP wXl2ekivy+3AGhKJjuxSVx9Oze9RhGSIOSzrdu2tyWLVmrVLfYSjge9w9PwIyhVFTSev l5lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=N6BBgYB9y9o/KEMrM5XpJsnZe4hC3BDSIOBT2qELU7c=; b=dT6VoGegd7j30MJ+9gzX1nC22IUiA6YJvC1C/kt0qpH8yMXQVxKzzwbMCkX5qARuMY IWz2R9JQVYqbKPW7ri9vXTf79vdQuIJ09Knpuw0LmFHr+ecmqLE813xbOcIiLz+kb3yN wlLquunnPv+zfp9ncgjdMvRWGY+c4GqoMkU2AAk24F9hp/qjmcPlsZiIWcCsTCLMMHLe hrOVnpNBJwwZ8yjn8J1lj/a2qVg+jNfryUGr4ue9FkbmWlgKB5OG0ZdB+mkm9rmiO4US uouJ4OT+2GKXAYSTCK5e1o6m5CoEg7T5dED6eTykNxQ+36feeKcMJXWFppArUYIG5jud tqsQ== X-Gm-Message-State: AOAM530uXwhqShBHusowAeuKGul9C/KpvYElrS7lSlPRTbWKcCjaQhf5 qhQtlh89jbVxMwjTFg3R11Q= X-Google-Smtp-Source: ABdhPJxk3C46k2gLlz/QKD87GqVb3EVkn/YeXW8qP1kCxzlDbiGCvzh9EIu1RNokv6ZV6c5GX4oADQ== X-Received: by 2002:aa7:8892:0:b0:481:1868:17d9 with SMTP id z18-20020aa78892000000b00481186817d9mr10175338pfe.40.1635858793601; Tue, 02 Nov 2021 06:13:13 -0700 (PDT) Received: from bj04871pcu.spreadtrum.com ([117.18.48.102]) by smtp.gmail.com with ESMTPSA id k13sm20924750pfc.197.2021.11.02.06.13.10 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 Nov 2021 06:13:12 -0700 (PDT) From: Janet Liu To: catalin.marinas@arm.com, will@kernel.org Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, orson.zhai@unisoc.com Subject: [PATCH v2 2/2] arm64: kprobes: add support for KPROBES_ON_FTRACE Date: Tue, 2 Nov 2021 21:11:46 +0800 Message-Id: <1635858706-27320-2-git-send-email-jianhua.ljh@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1635858706-27320-1-git-send-email-jianhua.ljh@gmail.com> References: <1635858706-27320-1-git-send-email-jianhua.ljh@gmail.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211102_061314_783554_B3888A36 X-CRM114-Status: GOOD ( 23.81 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Janet Liu This patch allow kprobes on ftrace call sites. This optimization avoids use of a trap with regular kprobes. This depends on HAVE_DYNAMIC_FTRACE_WITH_REGS which depends on "patchable-function-entry" options which is only implemented with newer toolchains. Signed-off-by: Janet Liu --- arch/arm64/Kconfig | 1 + arch/arm64/kernel/probes/Makefile | 1 + arch/arm64/kernel/probes/ftrace.c | 73 ++++++++++++++++++++++++++++++ arch/arm64/kernel/probes/kprobes.c | 27 +++++++++++ 4 files changed, 102 insertions(+) create mode 100644 arch/arm64/kernel/probes/ftrace.c diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 339130712093..f59005608976 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -200,6 +200,7 @@ config ARM64 select HAVE_SYSCALL_TRACEPOINTS select HAVE_KPROBES select HAVE_OPTPROBES + select HAVE_KPROBES_ON_FTRACE select HAVE_KRETPROBES select HAVE_GENERIC_VDSO select IOMMU_DMA if IOMMU_SUPPORT diff --git a/arch/arm64/kernel/probes/Makefile b/arch/arm64/kernel/probes/Makefile index c77c92ac95fd..d9b204f4795a 100644 --- a/arch/arm64/kernel/probes/Makefile +++ b/arch/arm64/kernel/probes/Makefile @@ -3,5 +3,6 @@ obj-$(CONFIG_KPROBES) += kprobes.o decode-insn.o \ kprobes_trampoline.o \ simulate-insn.o obj-$(CONFIG_OPTPROBES) += opt.o opt_head.o +obj-$(CONFIG_KPROBES_ON_FTRACE) += ftrace.o obj-$(CONFIG_UPROBES) += uprobes.o decode-insn.o \ simulate-insn.o diff --git a/arch/arm64/kernel/probes/ftrace.c b/arch/arm64/kernel/probes/ftrace.c new file mode 100644 index 000000000000..46ea92eb552f --- /dev/null +++ b/arch/arm64/kernel/probes/ftrace.c @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: GPL-2.0-only +// +// Dynamic Ftrace based Kprobes Optimization +// +// Copyright (C) 2021, Unisoc Inc. +// Author: Janet Liu +#include +#include +#include +#include +#include + + +/* Ftrace callback handler for kprobes*/ +void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip, + struct ftrace_ops *ops, struct ftrace_regs *fregs) +{ + struct kprobe *p; + struct kprobe_ctlblk *kcb; + struct pt_regs *regs = ftrace_get_regs(fregs); + int bit; + + bit = ftrace_test_recursion_trylock(ip, parent_ip); + if (bit < 0) + return; + + preempt_disable_notrace(); + p = get_kprobe((kprobe_opcode_t *)ip); + if (unlikely(!p) || kprobe_disabled(p)) + goto end; + + kcb = get_kprobe_ctlblk(); + if (kprobe_running()) { + kprobes_inc_nmissed_count(p); + } else { + unsigned long orig_ip = instruction_pointer(regs); + + instruction_pointer_set(regs, ip); + + __this_cpu_write(current_kprobe, p); + kcb->kprobe_status = KPROBE_HIT_ACTIVE; + if (!p->pre_handler || !p->pre_handler(p, regs)) { + /* + *Emulate singlestep (and also recover regs->pc) + *as if there is a nop + */ + instruction_pointer_set(regs, + (unsigned long)p->addr + MCOUNT_INSN_SIZE); + if (unlikely(p->post_handler)) { + kcb->kprobe_status = KPROBE_HIT_SSDONE; + p->post_handler(p, regs, 0); + } + instruction_pointer_set(regs, orig_ip); + } + + /* + * If pre_handler returns !0,it changes regs->pc. We have to + * skip emulating post_handler. + */ + __this_cpu_write(current_kprobe, NULL); + } +end: + preempt_enable_notrace(); + ftrace_test_recursion_unlock(bit); +} +NOKPROBE_SYMBOL(kprobe_ftrace_handler); + +int arch_prepare_kprobe_ftrace(struct kprobe *p) +{ + p->ainsn.api.insn = NULL; + p->ainsn.api.restore = 0; + return 0; +} diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c index 6dbcc89f6662..3d371d3e4dfa 100644 --- a/arch/arm64/kernel/probes/kprobes.c +++ b/arch/arm64/kernel/probes/kprobes.c @@ -417,6 +417,33 @@ int __kprobes arch_trampoline_kprobe(struct kprobe *p) return 0; } +kprobe_opcode_t __kprobes *kprobe_lookup_name(const char *name, unsigned int offset) +{ + kprobe_opcode_t *addr; + + addr = (kprobe_opcode_t *)kallsyms_lookup_name(name); +#ifdef CONFIG_KPROBES_ON_FTRACE + if (addr && !offset) { + unsigned long faddr; + + faddr = ftrace_location_range((unsigned long)addr, + (unsigned long)addr + 8); + if (faddr) + addr = (kprobe_opcode_t *)faddr; + } +#endif + return addr; +} + +bool __kprobes arch_kprobe_on_func_entry(unsigned long offset) +{ +#ifdef CONFIG_KPROBES_ON_FTRACE + return offset <= 8; +#else + return !offset; +#endif +} + int __init arch_init_kprobes(void) { register_kernel_break_hook(&kprobes_break_hook);