Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp4365215pxb; Tue, 25 Jan 2022 08:52:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJwExkYtP9coXLadGlxF9pbucyJqDAfZDFdaHuFGOyvhnLXJSRvWnx91oyxEybPygF1HWFbG X-Received: by 2002:a50:da48:: with SMTP id a8mr20810115edk.155.1643129551175; Tue, 25 Jan 2022 08:52:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643129551; cv=none; d=google.com; s=arc-20160816; b=IPy6PzLMy4IAbXn6vFjYjG3ibzLQjjGG0QocQ4KLf2sb8A/JHAWC0SCtYfCnm1OF9b aS1VnMfqB4KLys5ukOoNt7jzf7dCHgVpzhZIEvBItoA1naqopzLkJID+hEFS5DUlnoh1 J8uoYG+H3UGdKk2J1k3QgLTm6Ze4x99GCs8Ou3CSPjoc6abwEG4b/UeiLU2+fyuWXU5r 34qi23wxn7RR0GpS5U0qYjDn7gIdlGh9idCyQJT7tv4dOXE0cICgyGI9Z0d720TL0wvA MYcabxE0MnjNtstcVXxaA+1qnX7lSLV8fC69oSoqJMbiNSDOts+rCpVaBFge9vUXKPu7 pV5A== 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=YD2kv7ksusBtEpIV4Cwdj87dvaKzqOuKkagiWE9LCbI=; b=Zx6F1AF/MH7Vf0RsJvKGcau4sJUzS2QEir02VTeGzahUy41GXG8UX32SdoTfnwlPfb BKC+JQ4gJjxtqodX9GR+y3a3g9oCKC7D0rhO78yqsu1p98cJJRKAU5gUEMSv+njufkxx clSQ3g0aYN96Lprul6WEJWeqSY6d6vdBDZ3hIadJA4NLHV49LgIxfr9C0FL6cAPvgBmd o7u25KJZEnAIuLSP2SznuaztbefCvadGokcE56RHdN+LBY5d5mMjMqJf2Cf5dNcgftyN YgkUku5/ZcB3OI6+eo2q+7ZrIVoMF+9ePVKm1h0Gwsz91dW+0jK1g/84uRFuYAB6jQbh lDJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QICtXK9E; 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 w16si10523619edl.87.2022.01.25.08.52.06; Tue, 25 Jan 2022 08:52:31 -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=QICtXK9E; 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 S1447294AbiAYMTQ (ORCPT + 99 others); Tue, 25 Jan 2022 07:19:16 -0500 Received: from ams.source.kernel.org ([145.40.68.75]:55544 "EHLO ams.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347223AbiAYMNc (ORCPT ); Tue, 25 Jan 2022 07:13:32 -0500 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 B9E42B817ED; Tue, 25 Jan 2022 12:13:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 220CDC340E8; Tue, 25 Jan 2022 12:13:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643112806; bh=YP8yR2ULzF4ht0C7ZypYUVFYi3bHtPsRHNq8KOsRvFg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QICtXK9EGK6UyhVFqu9JINjfmGO3rDXfJkp8XDa5XBt87C4KS2Y62sfKRZww6dEtc wABAUbMNGJt3hwecun7YjslUMKug9JupiSFIqgVEKcB7L84JHNm1swPUISJFAUV6Wb uRfwCiVR9XDMrzyhTyrs3H2rz0jrJS+mSfvnsZLRDZvS9CY9jc6HzucotRhnJV/k+D kWuWulVVl5+FcOdf7l+vsGgZoXnUxVDblKPvsF/rMgnhn35NbqPsuUq282TVsUb7p2 dP18ld3ehy1nw+6nEM7nKHHKNDu14Z7KKf4aRqvbDc//US5iHZefMwe2gsT9IZLupB CD8A/hvLb3KVA== 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 v5 9/9] docs: fprobe: Add fprobe description to ftrace-use.rst Date: Tue, 25 Jan 2022 21:13:19 +0900 Message-Id: <164311279968.1933078.10295698671764269513.stgit@devnote2> X-Mailer: git-send-email 2.25.1 In-Reply-To: <164311269435.1933078.6963769885544050138.stgit@devnote2> References: <164311269435.1933078.6963769885544050138.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 a documentation of fprobe for the user who needs this interface. Signed-off-by: Masami Hiramatsu --- Documentation/trace/fprobe.rst | 131 ++++++++++++++++++++++++++++++++++++++++ Documentation/trace/index.rst | 1 2 files changed, 132 insertions(+) create mode 100644 Documentation/trace/fprobe.rst diff --git a/Documentation/trace/fprobe.rst b/Documentation/trace/fprobe.rst new file mode 100644 index 000000000000..c53950a1f91e --- /dev/null +++ b/Documentation/trace/fprobe.rst @@ -0,0 +1,131 @@ +.. SPDX-License-Identifier: GPL-2.0 + +================================== +Fprobe - Function entry/exit probe +================================== + +.. Author: Masami Hiramatsu + +Introduction +============ + +Instead of using ftrace full feature, if you only want to attach callbacks +on function entry and exit, similar to the kprobes and kretprobes, you can +use fprobe. Compared with kprobes and kretprobes, fprobe gives faster +instrumentation for multiple functions with single handler. This document +describes how to use fprobe. + +The usage of fprobe +=================== + +The fprobe is a wrapper of ftrace (+ kretprobe-like return callback) to +attach callbacks to multiple function entry and exit. User needs to set up +the `struct fprobe` and pass it to `register_fprobe()`. + +Typically, `fprobe` data structure is initialized with the `syms`, `nentry` +and `entry_handler` and/or `exit_handler` as below. + +.. code-block:: c + + char targets[] = {"func1", "func2", "func3"}; + struct fprobe fp = { + .syms = targets, + .nentry = ARRAY_SIZE(targets), + .entry_handler = my_entry_callback, + .exit_handler = my_exit_callback, + }; + +The ftrace_ops in the fprobe is automatically set. The FTRACE_OPS_FL_SAVE_REGS +and FTRACE_OPS_FL_RECURSION +flag will be set. If you need other flags, please set it by yourself. + +.. code-block:: c + + fp.ops.flags |= FTRACE_OPS_FL_RCU; + +To enable this fprobe, call:: + + register_fprobe(&fp); + +To disable (remove from functions) this fprobe, call:: + + unregister_fprobe(&fp); + +You can temporally (soft) disable the fprobe by:: + + disable_fprobe(&fp); + +and resume by:: + + enable_fprobe(&fp); + +The above is defined by including the header:: + + #include + +Same as ftrace, the registered callback will start being called some time +after the register_fprobe() is called and before it returns. See +:file:`Documentation/trace/ftrace.rst`. + + +The fprobe entry/exit handler +============================= + +The prototype of the entry/exit callback function is as follows: + +.. code-block:: c + + void callback_func(struct fprobe *fp, unsigned long entry_ip, struct pt_regs *regs); + +Note that both entry and exit callback has same ptototype. The @entry_ip is +saved at function entry and passed to exit handler. + +@fp + This is the address of `fprobe` data structure related to this handler. + You can embed the `fprobe` to your data structure and get it by + container_of() macro from @fp. The @fp must not be NULL. + +@entry_ip + This is the entry address of the traced function (both entry and exit). + +@regs + This is the `pt_regs` data structure at the entry and exit. Note that + the instruction pointer of @regs may be different from the @entry_ip + in the entry_handler. If you need traced instruction pointer, you need + to use @entry_ip. On the other hand, in the exit_handler, the instruction + pointer of @regs is set to the currect return address. + + +Use fprobe with raw address list +================================ + +Instead of passing the array of symbols, you can pass a array of raw +function addresses via `fprobe::addrs`. In this case, the value of +this array will be changed automatically to the dynamic ftrace NOP +location addresses in the given kernel function. So please take care +if you share this array with others. + + +The missed counter +================== + +The `fprobe` data structure has `fprobe::nmissed` counter field as same as +kprobes. +This counter counts up when; + + - fprobe fails to take ftrace_recursion lock. This usually means that a function + which is traced by other ftrace users is called from the entry_handler. + + - fprobe fails to setup the function exit because of the shortage of rethook + (the shadow stack for hooking the function return.) + +Note that `fprobe::nmissed` field is counted up in both case. The former case +will skip both of entry and exit callback, and the latter case will skip exit +callback, but in both case the counter is just increased by 1. + +Functions and structures +======================== + +.. kernel-doc:: include/linux/fprobe.h +.. kernel-doc:: kernel/trace/fprobe.c + diff --git a/Documentation/trace/index.rst b/Documentation/trace/index.rst index 3769b9b7aed8..b9f3757f8269 100644 --- a/Documentation/trace/index.rst +++ b/Documentation/trace/index.rst @@ -9,6 +9,7 @@ Linux Tracing Technologies tracepoint-analysis ftrace ftrace-uses + fprobe kprobes kprobetrace uprobetracer