From patchwork Tue Dec 21 05:53:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hengqi Chen X-Patchwork-Id: 12689063 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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56517C433EF for ; Tue, 21 Dec 2021 05:53:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233074AbhLUFxw (ORCPT ); Tue, 21 Dec 2021 00:53:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229964AbhLUFxw (ORCPT ); Tue, 21 Dec 2021 00:53:52 -0500 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5674AC061574 for ; Mon, 20 Dec 2021 21:53:52 -0800 (PST) Received: by mail-pf1-x42a.google.com with SMTP id k64so10697517pfd.11 for ; Mon, 20 Dec 2021 21:53:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=h5QnTrFhmy5tkCOL333g8+mzCEEe2uGwZdxuh6g3Ngg=; b=dL6sBZAtD+Iy5AJb5lKhj5z+/gnKIyRI/9aR2yoDe9IsbrdN4F/xPNTn6I2P+zVVr1 tXBWVx2udZAhk3FgDAUWjpsDcnuq/0K3++FN9PGrqjyV8fgtDazVXmMULlk3BErWODHH e8krJwzgrMViY5yU68siiG0I2M39VMEiURXhLt8xFzJRtYK6vzxiUxfWcdGiG0sWF5UV VzxRC+m343042skksQZTiUdfSqMs687vwsETI2wyCcZX1ZwjcsDMJQs0rJYVOcpB7vtG aJk/Hvn9albeMJeg636k6CMWiuIjCS69dqg9NiKhoR60qJmMB8tFy+kd4JC6g2xCJpFe MKlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=h5QnTrFhmy5tkCOL333g8+mzCEEe2uGwZdxuh6g3Ngg=; b=emBX4COaVKyc2lT8xhIgR3dmYIq2vp3dzqF8SZ+8wo5FreDfPmkxbGRFnpKG+uaj1/ e5lBcoO24pbSHPw8Xwo55bfpgtQMlQQNrjKS2qcrjO48WYr1v6IqDpqTU6JBkwa4XKvD cu6KymJL4mGbzbhuOC3fTFKxtT3XmaFa91aIyKNBnQRz423xi5z45s7kAyJNwXmmr/n5 gQs1Kj0FZPsh1E5sag+S5gMoIcGHQt+N6mhh8tWwUQMII32mvbRoXufaBJAPjMDWWMLA wYhIAeMm26h/by6q98KHan+v6PpEie89g3SLgFnSeseP0xV14daGMM31wF4nura1aLg5 xbvg== X-Gm-Message-State: AOAM533C4ft9E3cV5lHaum+dqPdlSTEW7UsuAyJv0iEfNQa1YQCDJUy0 E5BGuSnLNFu6XTXFigIQV8MWoGuadRu/gw== X-Google-Smtp-Source: ABdhPJzrCJTOI3WRzbFxWCEzuy4G6Zqihkemr24SfjP0vI9E9mA2CS2KvzyNJE0CYvqtIU2bOBLxUA== X-Received: by 2002:a63:484c:: with SMTP id x12mr1545845pgk.111.1640066031690; Mon, 20 Dec 2021 21:53:51 -0800 (PST) Received: from VM-32-4-ubuntu.. ([43.132.164.184]) by smtp.gmail.com with ESMTPSA id s16sm20441330pfu.109.2021.12.20.21.53.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Dec 2021 21:53:51 -0800 (PST) From: Hengqi Chen To: bpf@vger.kernel.org, andrii@kernel.org, hengqi.chen@gmail.com Subject: [PATCH bpf-next 1/2] libbpf: Add BPF_KPROBE_SYSCALL/BPF_KRETPROBE_SYSCALL macros Date: Tue, 21 Dec 2021 05:53:11 +0000 Message-Id: <20211221055312.3371414-2-hengqi.chen@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211221055312.3371414-1-hengqi.chen@gmail.com> References: <20211221055312.3371414-1-hengqi.chen@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Add syscall-specific variants of BPF_KPROBE/BPF_KRETPROBE named BPF_KPROBE_SYSCALL/BPF_KRETPROBE_SYSCALL ([0]). These new macros hide the underlying way of getting syscall input arguments and return values. With these new macros, the following code: SEC("kprobe/__x64_sys_close") int BPF_KPROBE(do_sys_close, struct pt_regs *regs) { int fd; fd = PT_REGS_PARM1_CORE(regs); /* do something with fd */ } can be written as: SEC("kprobe/__x64_sys_close") int BPF_KPROBE_SYSCALL(do_sys_close, int fd) { /* do something with fd */ } [0] Closes: https://github.com/libbpf/libbpf/issues/425 Signed-off-by: Hengqi Chen --- tools/lib/bpf/bpf_tracing.h | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) -- 2.30.2 diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h index db05a5937105..eb4b567e443f 100644 --- a/tools/lib/bpf/bpf_tracing.h +++ b/tools/lib/bpf/bpf_tracing.h @@ -489,4 +489,49 @@ typeof(name(0)) name(struct pt_regs *ctx) \ } \ static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args) +#define ___bpf_syscall_args0() ctx, regs +#define ___bpf_syscall_args1(x) \ + ___bpf_syscall_args0(), (void *)PT_REGS_PARM1_CORE(regs) +#define ___bpf_syscall_args2(x, args...) \ + ___bpf_syscall_args1(args), (void *)PT_REGS_PARM2_CORE(regs) +#define ___bpf_syscall_args3(x, args...) \ + ___bpf_syscall_args2(args), (void *)PT_REGS_PARM3_CORE(regs) +#define ___bpf_syscall_args4(x, args...) \ + ___bpf_syscall_args3(args), (void *)PT_REGS_PARM4_CORE(regs) +#define ___bpf_syscall_args5(x, args...) \ + ___bpf_syscall_args4(args), (void *)PT_REGS_PARM5_CORE(regs) +#define ___bpf_syscall_args(args...) \ + ___bpf_apply(___bpf_syscall_args, ___bpf_narg(args))(args) + +/* + * BPF_KPROBE_SYSCALL is a variant of BPF_KPROBE, which is intended for + * tracing syscall functions. It hides the underlying platform-specific + * low-level way of getting syscall input arguments from struct pt_regs, and + * provides a familiar typed and named function arguments syntax and + * semantics of accessing syscall input paremeters. + * + * Original struct pt_regs* context is preserved as 'ctx' argument. This might + * be necessary when using BPF helpers like bpf_perf_event_output(). + */ +#define BPF_KPROBE_SYSCALL(name, args...) \ +name(struct pt_regs *ctx); \ +static __attribute__((always_inline)) typeof(name(0)) \ +____##name(struct pt_regs *ctx, struct pt_regs *regs, ##args); \ +typeof(name(0)) name(struct pt_regs *ctx) \ +{ \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ + struct pt_regs *regs = PT_REGS_PARM1(ctx); \ + return ____##name(___bpf_syscall_args(args)); \ + _Pragma("GCC diagnostic pop") \ +} \ +static __attribute__((always_inline)) typeof(name(0)) \ +____##name(struct pt_regs *ctx, struct pt_regs *regs, ##args) + +/* + * BPF_KRETPROBE_SYSCALL is just an alias to BPF_KRETPROBE, + * it provides optional return value (in addition to `struct pt_regs *ctx`) + */ +#define BPF_KRETPROBE_SYSCALL BPF_KRETPROBE + #endif From patchwork Tue Dec 21 05:53:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hengqi Chen X-Patchwork-Id: 12689065 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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C8373C433EF for ; Tue, 21 Dec 2021 05:54:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229964AbhLUFyC (ORCPT ); Tue, 21 Dec 2021 00:54:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40654 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233087AbhLUFyB (ORCPT ); Tue, 21 Dec 2021 00:54:01 -0500 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 71837C061574 for ; Mon, 20 Dec 2021 21:54:01 -0800 (PST) Received: by mail-pf1-x434.google.com with SMTP id 196so7175381pfw.10 for ; Mon, 20 Dec 2021 21:54:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=uieDkl7DxCjlW74QVZKOedQWqym2cjHiLJMoOOVvQ+Y=; b=pivcQbcbZsxhJsorw1wrRy38VbATwobGfdSpFqWovlLdwNqvhSbO0sGZvNa8Tn2lCm 13kxpRZV0kHmezhH79EPRBN/U+3Ye4QuGBX7tln0LU/K2fiXKsNdRnH0oZEraompzlcA LtI243JopFE/72Gu6Dfp1yEvG1/Mq6U/IgEAfCVVJL0eo7rVVCi38Oev7mTjOl1ESm3D IFbxHR1sDY32LBPMBhCsQ7H0dvQYPa83BI63PJOQ+rBaaZD1rVrKJRg7b9D55i4wyl7P 3tXim6ZYXveemru/6IFkRVhKVRopyHTSRak1OwvfRzV8Fz56o57a6bE1CC7IqIGSH5t6 2cag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=uieDkl7DxCjlW74QVZKOedQWqym2cjHiLJMoOOVvQ+Y=; b=ZRr+p3T6LnWcOgHpI7Yd2R1NPGGBQY+IiXo4fff7QjGBrJzQn4awlX1N+lp6wdbDuP t9a9fFh1Tjg16QWtKFAMfURWYrx+XmQ7UycxPN5EL3NO64G3Gben+Cr8Mf6Ysz7DAfVn 56AMTetciAompLUBm9GbElIGOvxXNvSMsyhgw5gnghavr+1dih0N8vu1QhvLLS3jRca3 uI6VUxoRRl/+7fcuNKObkZCTqU55RLsAjJXh1Wam9rXElR6Qu6myGLA4XcQF24uYbC3H Y61tCl1hF/xhuj8Z3rhM7ZejkdGrkkP/KtUvaXCou0xzeq0o3UtRQJaV+D+G6KOZvCic KtMw== X-Gm-Message-State: AOAM532pk1Q5kITCTCvtvnl8GvO79IHBfaYSNxiyRMXdDOiZ+3zHE3Cf h2b0C66e8YSmkmT4tFqATNKNv5wR/vWs6g== X-Google-Smtp-Source: ABdhPJwHeJ5nzidQ/AyDaX/x9cLlP0GlHi0GohYzNPm5OLDfUh9YOEH7jE01bYzcuh2f80QxZ3/ytQ== X-Received: by 2002:a63:5147:: with SMTP id r7mr1491115pgl.581.1640066040833; Mon, 20 Dec 2021 21:54:00 -0800 (PST) Received: from VM-32-4-ubuntu.. ([43.132.164.184]) by smtp.gmail.com with ESMTPSA id s16sm20441330pfu.109.2021.12.20.21.53.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Dec 2021 21:54:00 -0800 (PST) From: Hengqi Chen To: bpf@vger.kernel.org, andrii@kernel.org, hengqi.chen@gmail.com Subject: [PATCH bpf-next 2/2] selftests/bpf: Test BPF_KPROBE_SYSCALL/BPF_KRETPROBE_SYSCALL macros Date: Tue, 21 Dec 2021 05:53:12 +0000 Message-Id: <20211221055312.3371414-3-hengqi.chen@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211221055312.3371414-1-hengqi.chen@gmail.com> References: <20211221055312.3371414-1-hengqi.chen@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Add tests for the newly added BPF_KPROBE_SYSCALL/BPF_KRETPROBE_SYSCALL macros. Signed-off-by: Hengqi Chen --- .../selftests/bpf/prog_tests/kprobe_syscall.c | 40 ++++++++++++++++++ .../selftests/bpf/progs/test_kprobe_syscall.c | 41 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/kprobe_syscall.c create mode 100644 tools/testing/selftests/bpf/progs/test_kprobe_syscall.c -- 2.30.2 diff --git a/tools/testing/selftests/bpf/prog_tests/kprobe_syscall.c b/tools/testing/selftests/bpf/prog_tests/kprobe_syscall.c new file mode 100644 index 000000000000..a1fad70bbb69 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/kprobe_syscall.c @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Hengqi Chen */ + +#include +#include +#include +#include "test_kprobe_syscall.skel.h" + +void test_kprobe_syscall(void) +{ + struct test_kprobe_syscall *skel; + int err, fd = 0; + + skel = test_kprobe_syscall__open(); + if (!ASSERT_OK_PTR(skel, "could not open BPF object")) + return; + + skel->rodata->my_pid = getpid(); + + err = test_kprobe_syscall__load(skel); + if (!ASSERT_OK(err, "could not load BPF object")) + goto cleanup; + + err = test_kprobe_syscall__attach(skel); + if (!ASSERT_OK(err, "could not attach BPF object")) + goto cleanup; + + fd = socket(AF_UNIX, SOCK_STREAM, 0); + + ASSERT_GT(fd, 0, "socket failed"); + ASSERT_EQ(skel->bss->domain, AF_UNIX, "BPF_KPROBE_SYSCALL failed"); + ASSERT_EQ(skel->bss->type, SOCK_STREAM, "BPF_KPROBE_SYSCALL failed"); + ASSERT_EQ(skel->bss->protocol, 0, "BPF_KPROBE_SYSCALL failed"); + ASSERT_EQ(skel->bss->fd, fd, "BPF_KRETPROBE_SYSCALL failed"); + +cleanup: + if (fd) + close(fd); + test_kprobe_syscall__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_kprobe_syscall.c b/tools/testing/selftests/bpf/progs/test_kprobe_syscall.c new file mode 100644 index 000000000000..ecef9d19007c --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_kprobe_syscall.c @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Hengqi Chen */ + +#include "vmlinux.h" +#include +#include +#include + +const volatile pid_t my_pid = 0; +int domain = 0; +int type = 0; +int protocol = 0; +int fd = 0; + +SEC("kprobe/__x64_sys_socket") +int BPF_KPROBE_SYSCALL(socket_enter, int d, int t, int p) +{ + pid_t pid = bpf_get_current_pid_tgid() >> 32; + + if (pid != my_pid) + return 0; + + domain = d; + type = t; + protocol = p; + return 0; +} + +SEC("kretprobe/__x64_sys_socket") +int BPF_KRETPROBE_SYSCALL(socket_exit, int ret) +{ + pid_t pid = bpf_get_current_pid_tgid() >> 32; + + if (pid != my_pid) + return 0; + + fd = ret; + return 0; +} + +char _license[] SEC("license") = "GPL";