Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp638151pxb; Tue, 1 Feb 2022 07:27:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJxrabMUkMd2OsQx7/YjLMUnrJYB+d42MsAjHBY4h7zHiTTT5is7+j2CveQfSAgW6FBtfi9B X-Received: by 2002:a17:906:478c:: with SMTP id cw12mr21028625ejc.214.1643729265229; Tue, 01 Feb 2022 07:27:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643729265; cv=none; d=google.com; s=arc-20160816; b=etM8wPjQYkhVc4Ho7LoMlJ4Wsb3M0Vb5YybbBs8QDsEH0TuGBhc9HpPBQ0dkrdxZKo dm/ggglPcdW8roaiTA7IUQzXbfyQ2V/MouM0vL+wWqaEg8xcTl4JwTAeJBUSKOh3xh+I Y4i3FIZcRK1OkP4ju50H0d/9Jm2UTatcg4FFInY6tHDlR7u4TnbHvKvIROA5YQT/5new as4E69TsUcbzZttQOwLEnGrho/Qv9QMKrwArAWYiGNrwSi/QShPl4pCtjlVOg3+Oot8s 4aYN6ZtZEkD8lUQff9Hqmg5dv/dRdwYq6CNM+GcwUXBOzmmrQx7PkpR6chO08gjA4CuD NB6g== 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=Wso++OFhUQ4l5bHVKEut1ObkvF08YDf3Kxrj+aUN+o4=; b=SCEAlgo9Arow3q+vEquPhD+XcvK1Hd33a1Xzb7ujQzOpwIr4EsiqygakE0rcjLIaxI ccx6P0nv4c9ViR4XVY9f5xJMBAPF9oX7dK1MVDLkGkYOUBESlRp5jsuThQ24C2ajnRqW Vm+B4NKzj3fgRIvS8VyvYowHR5wwmvvHFPhCqw0Zeh7EnA33yFSPMo1ykrXn3hLWAEGn CBvmBT7+FkqI05EjuEPcesRT/39oURqff3WIOUAyvU5RhNHwqHDeR0ZohORwa12ET5gO f+8cwXbuxa3kqf/gd3uiFbh7MySJmWzzZoU4hCGLm78oR3cVBEfQ0mUjXM457C1UUm1k N7iQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rnRDwaC9; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 8si9542385ejb.864.2022.02.01.07.27.18; Tue, 01 Feb 2022 07:27:45 -0800 (PST) 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=@kernel.org header.s=k20201202 header.b=rnRDwaC9; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357713AbiAaFDC (ORCPT + 99 others); Mon, 31 Jan 2022 00:03:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357683AbiAaFCd (ORCPT ); Mon, 31 Jan 2022 00:02:33 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A77BC061756; Sun, 30 Jan 2022 21:01:53 -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 dfw.source.kernel.org (Postfix) with ESMTPS id DCAEF611AD; Mon, 31 Jan 2022 05:01:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 19EB2C340E8; Mon, 31 Jan 2022 05:01:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643605312; bh=u5HSB42qpYb2fuxkTMvjkgmWhlnboq1+fVbTsi9o21k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rnRDwaC9g3mtq41ilziR1PF3d57XIG3ZASZWjTVOBrpZQbNnO6dKzaI4IUfDSgkgE K2sXLB5vLHUeXB+PAn+ZwHjyvUQMUkePJUtLTBK+wyaPi/nGh1eJe7O7nAqyglLnIU oKS2BwZTOKOSkV1R7U3BqVNOmZY4jS1nWu28YgWWK9fE+TgVUtNOvs8OFmmGg3+prp vlKiYcFBYNJTUB+N+oqusG3BE5oYfRkm2BjEgjBpZcswc0jtjCgXL0F8qsjHqQTNNY /kNDNxyaFOqp83cCloocnGNRbR+C28ztxxcwr8fKPbzX4O9S9PVt1YuSY7XKowwyNy JYZlKAXxSlC3w== 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 v7 07/10] fprobe: Add exit_handler support Date: Mon, 31 Jan 2022 14:01:46 +0900 Message-Id: <164360530646.65877.3880301440622084582.stgit@devnote2> X-Mailer: git-send-email 2.25.1 In-Reply-To: <164360522462.65877.1891020292202285106.stgit@devnote2> References: <164360522462.65877.1891020292202285106.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add exit_handler to fprobe. fprobe + rethook allows us to hook the kernel function return. The rethook will be enabled only if the fprobe::exit_handler is set. Signed-off-by: Masami Hiramatsu --- Changes in v7: - Fix unregister_fprobe() to ensure the rethook handlers are finished when it returns. - Update Kconfig help. Changes in v6: - Update according to the fprobe update. Changes in v5: - Add dependency for HAVE_RETHOOK. Changes in v4: - Check fprobe is disabled in the exit handler. Changes in v3: - Make sure to clear rethook->data before free. - Handler checks the data is not NULL. - Free rethook only if the rethook is using. --- include/linux/fprobe.h | 6 ++ kernel/trace/Kconfig | 9 ++- kernel/trace/fprobe.c | 130 ++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 130 insertions(+), 15 deletions(-) diff --git a/include/linux/fprobe.h b/include/linux/fprobe.h index b920dc1b2969..acfdcc37acf6 100644 --- a/include/linux/fprobe.h +++ b/include/linux/fprobe.h @@ -5,19 +5,25 @@ #include #include +#include /** * struct fprobe - ftrace based probe. * @ops: The ftrace_ops. * @nmissed: The counter for missing events. * @flags: The status flag. + * @rethook: The rethook data structure. (internal data) * @entry_handler: The callback function for function entry. + * @exit_handler: The callback function for function exit. */ struct fprobe { struct ftrace_ops ops; unsigned long nmissed; unsigned int flags; + struct rethook *rethook; + void (*entry_handler)(struct fprobe *fp, unsigned long entry_ip, struct pt_regs *regs); + void (*exit_handler)(struct fprobe *fp, unsigned long entry_ip, struct pt_regs *regs); }; #define FPROBE_FL_DISABLED 1 diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig index 9e66fd29d94e..1a0f42561bf7 100644 --- a/kernel/trace/Kconfig +++ b/kernel/trace/Kconfig @@ -245,11 +245,14 @@ config FPROBE bool "Kernel Function Probe (fprobe)" depends on FUNCTION_TRACER depends on DYNAMIC_FTRACE_WITH_REGS + depends on HAVE_RETHOOK + select RETHOOK default n help - This option enables kernel function probe (fprobe) based on ftrace, - which is similar to kprobes, but probes only for kernel function - entries and it can probe multiple functions by one fprobe. + This option enables kernel function probe (fprobe) based on ftrace. + The fprobe is similar to kprobes, but probes only for kernel function + entries and exits. This also can probe multiple functions by one + fprobe. If unsure, say N. diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index b5d4f8baaf43..d733c0d9cb09 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -8,12 +8,22 @@ #include #include #include +#include #include #include +#include "trace.h" + +struct fprobe_rethook_node { + struct rethook_node node; + unsigned long entry_ip; +}; + static void fprobe_handler(unsigned long ip, unsigned long parent_ip, struct ftrace_ops *ops, struct ftrace_regs *fregs) { + struct fprobe_rethook_node *fpr; + struct rethook_node *rh; struct fprobe *fp; int bit; @@ -30,10 +40,37 @@ static void fprobe_handler(unsigned long ip, unsigned long parent_ip, if (fp->entry_handler) fp->entry_handler(fp, ip, ftrace_get_regs(fregs)); + if (fp->exit_handler) { + rh = rethook_try_get(fp->rethook); + if (!rh) { + fp->nmissed++; + goto out; + } + fpr = container_of(rh, struct fprobe_rethook_node, node); + fpr->entry_ip = ip; + rethook_hook(rh, ftrace_get_regs(fregs)); + } + +out: ftrace_test_recursion_unlock(bit); } NOKPROBE_SYMBOL(fprobe_handler); +static void fprobe_exit_handler(struct rethook_node *rh, void *data, + struct pt_regs *regs) +{ + struct fprobe *fp = (struct fprobe *)data; + struct fprobe_rethook_node *fpr; + + if (!fp || fprobe_disabled(fp)) + return; + + fpr = container_of(rh, struct fprobe_rethook_node, node); + + fp->exit_handler(fp, fpr->entry_ip, regs); +} +NOKPROBE_SYMBOL(fprobe_exit_handler); + /* Convert ftrace location address from symbols */ static unsigned long *get_ftrace_locations(const char **syms, int num) { @@ -76,6 +113,48 @@ static void fprobe_init(struct fprobe *fp) fp->ops.flags |= FTRACE_OPS_FL_SAVE_REGS; } +static int fprobe_init_rethook(struct fprobe *fp, int num) +{ + int i, size; + + if (num < 0) + return -EINVAL; + + if (!fp->exit_handler) { + fp->rethook = NULL; + return 0; + } + + /* Initialize rethook if needed */ + size = num * num_possible_cpus() * 2; + if (size < 0) + return -E2BIG; + + fp->rethook = rethook_alloc((void *)fp, fprobe_exit_handler); + for (i = 0; i < size; i++) { + struct rethook_node *node; + + node = kzalloc(sizeof(struct fprobe_rethook_node), GFP_KERNEL); + if (!node) { + rethook_free(fp->rethook); + fp->rethook = NULL; + return -ENOMEM; + } + rethook_add_node(fp->rethook, node); + } + return 0; +} + +static void fprobe_fail_cleanup(struct fprobe *fp) +{ + if (fp->rethook) { + /* Don't need to cleanup rethook->handler because this is not used. */ + rethook_free(fp->rethook); + fp->rethook = NULL; + } + ftrace_free_filter(&fp->ops); +} + /** * register_fprobe() - Register fprobe to ftrace by pattern. * @fp: A fprobe data structure to be registered. @@ -89,6 +168,7 @@ static void fprobe_init(struct fprobe *fp) */ int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter) { + struct ftrace_hash *hash; unsigned char *str; int ret, len; @@ -113,10 +193,21 @@ int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter goto out; } - ret = register_ftrace_function(&fp->ops); + /* TODO: + * correctly calculate the total number of filtered symbols + * from both filter and notfilter. + */ + hash = fp->ops.local_hash.filter_hash; + if (WARN_ON_ONCE(!hash)) + goto out; + + ret = fprobe_init_rethook(fp, (int)hash->count); + if (!ret) + ret = register_ftrace_function(&fp->ops); + out: if (ret) - ftrace_free_filter(&fp->ops); + fprobe_fail_cleanup(fp); return ret; } EXPORT_SYMBOL_GPL(register_fprobe); @@ -144,12 +235,15 @@ int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num) fprobe_init(fp); ret = ftrace_set_filter_ips(&fp->ops, addrs, num, 0, 0); + if (ret) + return ret; + + ret = fprobe_init_rethook(fp, num); if (!ret) ret = register_ftrace_function(&fp->ops); if (ret) - ftrace_free_filter(&fp->ops); - + fprobe_fail_cleanup(fp); return ret; } EXPORT_SYMBOL_GPL(register_fprobe_ips); @@ -180,14 +274,16 @@ int register_fprobe_syms(struct fprobe *fp, const char **syms, int num) return PTR_ERR(addrs); ret = ftrace_set_filter_ips(&fp->ops, addrs, num, 0, 0); + kfree(addrs); if (ret) - goto out; - ret = register_ftrace_function(&fp->ops); - if (ret) - ftrace_free_filter(&fp->ops); + return ret; -out: - kfree(addrs); + ret = fprobe_init_rethook(fp, num); + if (!ret) + ret = register_ftrace_function(&fp->ops); + + if (ret) + fprobe_fail_cleanup(fp); return ret; } @@ -208,10 +304,20 @@ int unregister_fprobe(struct fprobe *fp) if (!fp || fp->ops.func != fprobe_handler) return -EINVAL; + /* + * rethook_free() starts disabling the rethook, but the rethook handlers + * may be running on other processors at this point. To make sure that all + * current running handlers are finished, call unregister_ftrace_function() + * after this. + */ + if (fp->rethook) + rethook_free(fp->rethook); + ret = unregister_ftrace_function(&fp->ops); + if (ret < 0) + return ret; - if (!ret) - ftrace_free_filter(&fp->ops); + ftrace_free_filter(&fp->ops); return ret; }