From patchwork Wed Aug 18 23:41:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 12445545 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-23.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C25B4C432BE for ; Wed, 18 Aug 2021 23:41:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 95A6B60FC4 for ; Wed, 18 Aug 2021 23:41:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234942AbhHRXmd (ORCPT ); Wed, 18 Aug 2021 19:42:33 -0400 Received: from new4-smtp.messagingengine.com ([66.111.4.230]:52991 "EHLO new4-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234860AbhHRXma (ORCPT ); Wed, 18 Aug 2021 19:42:30 -0400 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailnew.nyi.internal (Postfix) with ESMTP id 64F3D580E0B; Wed, 18 Aug 2021 19:41:55 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Wed, 18 Aug 2021 19:41:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=fm1; bh=Yk3euvXXlMCwY vGhvhfMir2QOXlVuyuGj4zvye1lxR8=; b=CJkcBFuIyHHZndihoY2jocWg5WF/9 g07b8+jeGMwgwtmO2H/K3sqX0X427JFal0Chw8HhAXKeMaFqrI21Q5zhimCp5xqN c23GHoLCYsNO+eyD81Zn2cS2CmeDwfmDFLyv5jPEHSHUijQGiSyAaV72Q9kuEqUB bXP4OCizYzSA+0bAhdCDWkXVjfcGZo1bHDceL1GGH1zI0AFFdnpf/fJkXX9qEQfW e7LGrOml/Jxgdh75T3BegLTILJJP54sBBxqVZvd9Kg4b2tGAm5prgoWVQXlwJFxS 32B3lAMifXMpoYZuo/1en0aScnMmPpgUSwqO9qxFcfjZCTS4dilNpBLLg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=Yk3euvXXlMCwYvGhvhfMir2QOXlVuyuGj4zvye1lxR8=; b=j3K1KuOo 0GEFw0iMAfQlbN+sK5oaDbgGJzipgQ8Cb3U7pj6dnZetDSN82BjOmsq0iEV4w6Xv hbYjswvth7QkXjsy+7lbEH8Y32IycKJD/VJAZyKplfWuGnuivDwQu9sG1FbRjosQ uzSDQcIBwRzk7reI3Qpdj2Zcju2uMwdyLLuFsnofVeqYdEVfW0bIeJfqvsXxerrb 4SoURgVTk7xxzy02X8KSTq0di7IQqr8xnNgq4HUMzUCk8luPDLUOv4fY2piEetRb TKUSOZ70lwRrIbXK79odCq38PjepXmYOkhA3rYOfKEoawKbZRYzOmoO7iCZgTho7 SmqSDjkBOJwtxA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrleeigddvgecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecufghrlhcuvffnffculddvfedmnegovehorghsthgrlh dqhfeguddvqddtvdculdduhedtmdenucfjughrpefhvffufffkofgjfhgggfestdekredt redttdenucfhrhhomhepffgrnhhivghlucgiuhcuoegugihusegugihuuhhurdighiiiqe enucggtffrrghtthgvrhhnpeeugfffffduffekueetieefgeeltdevfeekhffggeeikeej vdduteevkeeugedvtdenucffohhmrghinheplhhkmhhlrdhorhhgpdhgihhthhhusgdrtg homhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegu gihusegugihuuhhurdighiii X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 18 Aug 2021 19:41:54 -0400 (EDT) From: Daniel Xu To: bpf@vger.kernel.org, yhs@fb.com, andriin@fb.com Cc: Daniel Xu , kernel-team@fb.com, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next 1/2] bpf: Add bpf_task_pt_regs() helper Date: Wed, 18 Aug 2021 16:41:41 -0700 Message-Id: <6d269f13f2ff742e319a8c19112ef40f0b4c2f46.1629329560.git.dxu@dxuuu.xyz> X-Mailer: git-send-email 2.33.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net The motivation behind this helper is to access userspace pt_regs in a kprobe handler. uprobe's ctx is the userspace pt_regs. kprobe's ctx is the kernelspace pt_regs. bpf_task_pt_regs() allows accessing userspace pt_regs in a kprobe handler. The final case (kernelspace pt_regs in uprobe) is pretty rare (usermode helper) so I think that can be solved later if necessary. More concretely, this helper is useful in doing BPF-based DWARF stack unwinding. Currently the kernel can only do framepointer based stack unwinds for userspace code. This is because the DWARF state machines are too fragile to be computed in kernelspace [0]. The idea behind DWARF-based stack unwinds w/ BPF is to copy a chunk of the userspace stack (while in prog context) and send it up to userspace for unwinding (probably with libunwind) [1]. This would effectively enable profiling applications with -fomit-frame-pointer using kprobes and uprobes. [0]: https://lkml.org/lkml/2012/2/10/356 [1]: https://github.com/danobi/bpf-dwarf-walk Signed-off-by: Daniel Xu --- include/uapi/linux/bpf.h | 7 +++++++ kernel/trace/bpf_trace.c | 20 ++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 7 +++++++ 3 files changed, 34 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index c4f7892edb2b..47427493206a 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -4871,6 +4871,12 @@ union bpf_attr { * Return * Value specified by user at BPF link creation/attachment time * or 0, if it was not specified. + * + * long bpf_task_pt_regs(struct task_struct *task) + * Description + * Get the struct pt_regs associated with **task**. + * Return + * A pointer to struct pt_regs. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -5048,6 +5054,7 @@ union bpf_attr { FN(timer_cancel), \ FN(get_func_ip), \ FN(get_attach_cookie), \ + FN(task_pt_regs), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index cbc73c08c4a4..5924bb5a1462 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -723,6 +723,24 @@ static const struct bpf_func_proto bpf_get_current_task_btf_proto = { .ret_btf_id = &bpf_get_current_btf_ids[0], }; +BPF_CALL_1(bpf_task_pt_regs, struct task_struct *, task) +{ + return (unsigned long) task_pt_regs(task); +} + +BTF_ID_LIST(bpf_task_pt_regs_ids) +BTF_ID(struct, task_struct) +BTF_ID(struct, pt_regs) + +static const struct bpf_func_proto bpf_task_pt_regs_proto = { + .func = bpf_task_pt_regs, + .gpl_only = true, + .arg1_type = ARG_PTR_TO_BTF_ID, + .arg1_btf_id = &bpf_task_pt_regs_ids[0], + .ret_type = RET_PTR_TO_BTF_ID, + .ret_btf_id = &bpf_task_pt_regs_ids[1], +}; + BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx) { struct bpf_array *array = container_of(map, struct bpf_array, map); @@ -1032,6 +1050,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_get_current_task_proto; case BPF_FUNC_get_current_task_btf: return &bpf_get_current_task_btf_proto; + case BPF_FUNC_task_pt_regs: + return &bpf_task_pt_regs_proto; case BPF_FUNC_get_current_uid_gid: return &bpf_get_current_uid_gid_proto; case BPF_FUNC_get_current_comm: diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index c4f7892edb2b..47427493206a 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -4871,6 +4871,12 @@ union bpf_attr { * Return * Value specified by user at BPF link creation/attachment time * or 0, if it was not specified. + * + * long bpf_task_pt_regs(struct task_struct *task) + * Description + * Get the struct pt_regs associated with **task**. + * Return + * A pointer to struct pt_regs. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -5048,6 +5054,7 @@ union bpf_attr { FN(timer_cancel), \ FN(get_func_ip), \ FN(get_attach_cookie), \ + FN(task_pt_regs), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper From patchwork Wed Aug 18 23:41:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 12445547 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60E86C43214 for ; Wed, 18 Aug 2021 23:42:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4282C61029 for ; Wed, 18 Aug 2021 23:42:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234977AbhHRXme (ORCPT ); Wed, 18 Aug 2021 19:42:34 -0400 Received: from out1-smtp.messagingengine.com ([66.111.4.25]:34493 "EHLO out1-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234882AbhHRXmc (ORCPT ); Wed, 18 Aug 2021 19:42:32 -0400 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 9FB4C5C00BE; Wed, 18 Aug 2021 19:41:56 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Wed, 18 Aug 2021 19:41:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=fm1; bh=L906x9jn1af2k uPVRce6Z0F5sCoQjvZs1jCic4dwp5k=; b=QfsLsRNS+AqvTUCoz3KTX3pgZeqPH tb0Y5ZveW2ckkopo/YDP7AHpxyC367i9DsfeE1rML9ZPMeETi0QEvKGdeS6Qa7v9 1nae5Os80B539Oc01KH3Ywwop6glej5lPjITZM/ojV80FAMrz9R/U2QrOu5ORGhO 42TysI9Qr/VMo9SrAO8CgpIzICR9V1xDx1gPK7TWFQT2zySk6ymrcokSVB0WpO20 2pesy6/FZkm41cYMIu9rRIqBf/NETzdGMzWQbwomYBVluwNmDL9hRz0S9PUIcrrJ otWw653jdUd9sDiMgot7JPEY7mhqI7LkS0TCEL2fB1i2c+TJI3vJPO11Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=L906x9jn1af2kuPVRce6Z0F5sCoQjvZs1jCic4dwp5k=; b=ROImkayA zKh4duHKgDv32d0rqt0BClTu5VmFEH6iAHZqC38LWZFFOxYkZVgNH7wNRKLxkZ6e CFhcfrLe/p2mMPkvZLyXi4MFCgmNxuIcdl/BHfMIFhFbsiFzD01YaBEPWP75dPTH Lj5OUyZvitS46m47ouzRjbiW3S6387IJfrgh3ABLucbYlZnRl3a7d+M0ozOH+34m ab/Qj/M8CGeSNlIZF0fWt4nAmVy5Os2fc34BiJZT3hhIQ1Ofk49I9THESrDEKgL9 FifDxQcqJhM+M9Cc9KEAtsuvSYHbsk9LPCPItxkVvW3JMUrFQYpGYC/u5P9ElifT s5L+6e0KSgFMMA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrleeigddvhecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecufghrlhcuvffnffculdejtddmnecujfgurhephffvuf ffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeffrghnihgvlhcuighuuceougig uhesugiguhhuuhdrgiihiieqnecuggftrfgrthhtvghrnhepgfekudelkefhteevhfeggf dvgeefjeefgfeuvddutdfhgffghfehtdeuueetfeeinecuvehluhhsthgvrhfuihiivgep tdenucfrrghrrghmpehmrghilhhfrhhomhepugiguhesugiguhhuuhdrgiihii X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 18 Aug 2021 19:41:55 -0400 (EDT) From: Daniel Xu To: bpf@vger.kernel.org, yhs@fb.com, andriin@fb.com Cc: Daniel Xu , kernel-team@fb.com, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next 2/2] bpf: selftests: Add bpf_task_pt_regs() selftest Date: Wed, 18 Aug 2021 16:41:42 -0700 Message-Id: X-Mailer: git-send-email 2.33.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net This test retrieves the uprobe's pt_regs in two different ways and compares the contents in an arch-agnostic way. Signed-off-by: Daniel Xu --- .../selftests/bpf/prog_tests/task_pt_regs.c | 50 +++++++++++++++++++ .../selftests/bpf/progs/test_task_pt_regs.c | 29 +++++++++++ 2 files changed, 79 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/task_pt_regs.c create mode 100644 tools/testing/selftests/bpf/progs/test_task_pt_regs.c diff --git a/tools/testing/selftests/bpf/prog_tests/task_pt_regs.c b/tools/testing/selftests/bpf/prog_tests/task_pt_regs.c new file mode 100644 index 000000000000..3f0fc2267c1c --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/task_pt_regs.c @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include +#include +#include "test_task_pt_regs.skel.h" + +void test_task_pt_regs(void) +{ + int duration = 0; + struct test_task_pt_regs *skel; + struct bpf_link *uprobe_link; + size_t uprobe_offset; + ssize_t base_addr; + bool match; + + base_addr = get_base_addr(); + if (CHECK(base_addr < 0, "get_base_addr", + "failed to find base addr: %zd", base_addr)) + return; + uprobe_offset = get_uprobe_offset(&get_base_addr, base_addr); + + skel = test_task_pt_regs__open_and_load(); + if (CHECK(!skel, "skel_open", "failed to open skeleton\n")) + return; + if (CHECK(!skel->bss, "check_bss", ".bss wasn't mmap()-ed\n")) + goto cleanup; + + uprobe_link = bpf_program__attach_uprobe(skel->progs.handle_uprobe, + false /* retprobe */, + 0 /* self pid */, + "/proc/self/exe", + uprobe_offset); + if (!ASSERT_OK_PTR(uprobe_link, "attach_uprobe")) + goto cleanup; + skel->links.handle_uprobe = uprobe_link; + + /* trigger & validate uprobe */ + get_base_addr(); + + if (CHECK(skel->bss->uprobe_res != 1, "check_uprobe_res", + "wrong uprobe res: %d\n", skel->bss->uprobe_res)) + goto cleanup; + + match = !memcmp(&skel->bss->current_regs, &skel->bss->ctx_regs, + sizeof(skel->bss->current_regs)); + CHECK(!match, "check_regs_match", "registers did not match"); + +cleanup: + test_task_pt_regs__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_task_pt_regs.c b/tools/testing/selftests/bpf/progs/test_task_pt_regs.c new file mode 100644 index 000000000000..6c059f1cfa1b --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_task_pt_regs.c @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include + +struct pt_regs current_regs = {}; +struct pt_regs ctx_regs = {}; +int uprobe_res = 0; + +SEC("uprobe/trigger_func") +int handle_uprobe(struct pt_regs *ctx) +{ + struct task_struct *current; + struct pt_regs *regs; + + current = bpf_get_current_task_btf(); + regs = (struct pt_regs *) bpf_task_pt_regs(current); + __builtin_memcpy(¤t_regs, regs, sizeof(*regs)); + __builtin_memcpy(&ctx_regs, ctx, sizeof(*ctx)); + + /* Prove that uprobe was run */ + uprobe_res = 1; + + return 0; +} + +char _license[] SEC("license") = "GPL";