Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp2594663pxb; Thu, 3 Feb 2022 09:46:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJz3QYtwKhZdE3VzUoEBM42rT2VzEP7sYbo1vtksCJaXncKWWK0dDYJ5eeasZI2w/9vPnBnz X-Received: by 2002:a17:907:3e1e:: with SMTP id hp30mr31342392ejc.694.1643910405756; Thu, 03 Feb 2022 09:46:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643910405; cv=none; d=google.com; s=arc-20160816; b=TQ65pi0Wkd4UMxluURoVklMSnmmfvQLUemcADzqZ4MUkCqQ+o/LRk88qmMRf0LpLmt sa57vXboPq83XSVsvDyyYGmB2OIVKzFZNSysn/18ax71f/gIyEN1OdM4e7hzGsu5Grwv FOCNFHoe0xmuGuMT9C5qfOwCFjHC+D43rKGfSv7r2ceVD++/UabD8RpgKHvpwQ9pQPZ0 4d7PaC11jRjCZ8O6v07svRdnJTtOKmENANKgr9G/hzkkOWM7SQt2RdrDvP9cDAXZWhdE 9KH/B7cOi5CDTWuq6SCZFYoNLUe8mcBUVe/dbO7Y+06BrIgdRlqA46v49OQdYLwQwbsb zYxQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ndZKvuTAfuQTlyvC6PEP1xi3KVMCTEpQydu3XzbTIRw=; b=goSHnb4PcgluZcnoSQEnWnlL0UMV6h5nGK2Mg1pXqUnLvRO+EVLH783MUtAHgplzV7 8ZWmEoqdsY+Yk4vPcFK7dQOXYvBbI67E2m1XKJ94M8Ze9bnHYVi9x6a2L+UkVsQxzy8Q nQi3fOzYIVSfw01rL67O54dRm/3Vk/hwG4NBedLUBZS+SowZ4FYCTiAAhDBIjIqYDk0W SU+HGK6w65K/yHcxuzDLTUxEUioebJ6ReI6v/psjwd/mNVvqk67AkLAcBdyfKCMYqqXg EIgOxG7KtiHqR/OZUw2SExZvuacv2WxwolUjfUxFSWTp8HVg/AGpb2jckI3rxFF0I/4Y zz2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=B+F3+zJY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sg33si13573660ejc.560.2022.02.03.09.46.17; Thu, 03 Feb 2022 09:46:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=B+F3+zJY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344721AbiBBNyl (ORCPT + 99 others); Wed, 2 Feb 2022 08:54:41 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:57031 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344738AbiBBNy2 (ORCPT ); Wed, 2 Feb 2022 08:54:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643810067; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ndZKvuTAfuQTlyvC6PEP1xi3KVMCTEpQydu3XzbTIRw=; b=B+F3+zJYMQGXEcQEKAbkiBr4Xwk3mI8zsaxPXEudX+YwaZuKxJ2WARBO3SZ1JCrdts7Y9n gw5lghS74xdgUTXd0FJ5E/uQkwPfbbZ4WM3NrqXSnECQWeU3MGXjnDz3n1n94M2LqpRjPL G7mdP9lq5ixEUzif55s1rbQYcHe5nE4= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-126-zVvEhTUKOXKWxD4i5HOVWQ-1; Wed, 02 Feb 2022 08:54:26 -0500 X-MC-Unique: zVvEhTUKOXKWxD4i5HOVWQ-1 Received: by mail-ed1-f70.google.com with SMTP id h21-20020aa7c955000000b0040390b2bfc5so10425845edt.15 for ; Wed, 02 Feb 2022 05:54:26 -0800 (PST) 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:mime-version:content-transfer-encoding; bh=ndZKvuTAfuQTlyvC6PEP1xi3KVMCTEpQydu3XzbTIRw=; b=e5dgxgdk+vJ+wBlESOj5MOv/0wKxNIBYzvId/g+/LAMNRtAOfZDsbWCfhF7CwL0sOE e2xFBye9PbcDrMOEc37HJDVS0okWWCrHwcht2ytyJ1l/zYIQl74RS73Jpmndni8DrDD+ 8okSNWH7yaNo96+5IN3VPmkISFHM5JUanHHdevhfpbaLdhRb6aWygeHKWZZHyo9JBXpI vQFxrPcX9RykLuYgLSvnO+6OFZIfHD0QMM20ObMtH7cekO5i2NeRpsl/zv3y3u3pC6zA VOXERiKYKbmuppLekTuWE9Jnjn0J8AVqvfhzq3IN/oqs1nsIK2B3XsUKxJky7BGGd0+c vyDw== X-Gm-Message-State: AOAM532MWeWboj3UQQMSqD9fft42MziYsGdNfMr6W0pxWgvST18ky4EG pB8/YkI4xA/3OQ1DgUpTkzSNnll2dDB5j4JVvuxDkB9X/v8sbw6/uPxxzExyq2MHwJX3OPv6r+e 87USPshqkcfbDeIRtcTHJrmt+ X-Received: by 2002:a05:6402:16cf:: with SMTP id r15mr30306469edx.406.1643810065536; Wed, 02 Feb 2022 05:54:25 -0800 (PST) X-Received: by 2002:a05:6402:16cf:: with SMTP id r15mr30306452edx.406.1643810065387; Wed, 02 Feb 2022 05:54:25 -0800 (PST) Received: from krava.redhat.com (nat-pool-brq-u.redhat.com. [213.175.37.12]) by smtp.gmail.com with ESMTPSA id kw5sm8493321ejc.140.2022.02.02.05.54.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Feb 2022 05:54:24 -0800 (PST) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt , Jiri Olsa Subject: [PATCH 8/8] selftest/bpf: Add fprobe test for bpf_cookie values Date: Wed, 2 Feb 2022 14:53:33 +0100 Message-Id: <20220202135333.190761-9-jolsa@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220202135333.190761-1-jolsa@kernel.org> References: <20220202135333.190761-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Adding bpf_cookie test for kprobe attached by fprobe link. Signed-off-by: Jiri Olsa --- .../selftests/bpf/prog_tests/bpf_cookie.c | 73 +++++++++++++++++++ .../selftests/bpf/progs/fprobe_bpf_cookie.c | 62 ++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/fprobe_bpf_cookie.c diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c index cd10df6cd0fc..bf70d859c598 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c @@ -7,6 +7,7 @@ #include #include #include "test_bpf_cookie.skel.h" +#include "fprobe_bpf_cookie.skel.h" /* uprobe attach point */ static void trigger_func(void) @@ -63,6 +64,76 @@ static void kprobe_subtest(struct test_bpf_cookie *skel) bpf_link__destroy(retlink2); } +static void fprobe_subtest(void) +{ + DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts); + int err, prog_fd, link1_fd = -1, link2_fd = -1; + struct fprobe_bpf_cookie *skel = NULL; + __u32 duration = 0, retval; + __u64 addrs[8], cookies[8]; + + skel = fprobe_bpf_cookie__open_and_load(); + if (!ASSERT_OK_PTR(skel, "fentry_raw_skel_load")) + goto cleanup; + + kallsyms_find("bpf_fentry_test1", &addrs[0]); + kallsyms_find("bpf_fentry_test2", &addrs[1]); + kallsyms_find("bpf_fentry_test3", &addrs[2]); + kallsyms_find("bpf_fentry_test4", &addrs[3]); + kallsyms_find("bpf_fentry_test5", &addrs[4]); + kallsyms_find("bpf_fentry_test6", &addrs[5]); + kallsyms_find("bpf_fentry_test7", &addrs[6]); + kallsyms_find("bpf_fentry_test8", &addrs[7]); + + cookies[0] = 1; + cookies[1] = 2; + cookies[2] = 3; + cookies[3] = 4; + cookies[4] = 5; + cookies[5] = 6; + cookies[6] = 7; + cookies[7] = 8; + + opts.fprobe.addrs = (__u64) &addrs; + opts.fprobe.cnt = 8; + opts.fprobe.bpf_cookies = (__u64) &cookies; + prog_fd = bpf_program__fd(skel->progs.test2); + + link1_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_FPROBE, &opts); + if (!ASSERT_GE(link1_fd, 0, "link1_fd")) + return; + + cookies[0] = 8; + cookies[1] = 7; + cookies[2] = 6; + cookies[3] = 5; + cookies[4] = 4; + cookies[5] = 3; + cookies[6] = 2; + cookies[7] = 1; + + opts.flags = BPF_F_FPROBE_RETURN; + prog_fd = bpf_program__fd(skel->progs.test3); + + link2_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_FPROBE, &opts); + if (!ASSERT_GE(link2_fd, 0, "link2_fd")) + goto cleanup; + + prog_fd = bpf_program__fd(skel->progs.test1); + err = bpf_prog_test_run(prog_fd, 1, NULL, 0, + NULL, NULL, &retval, &duration); + ASSERT_OK(err, "test_run"); + ASSERT_EQ(retval, 0, "test_run"); + + ASSERT_EQ(skel->bss->test2_result, 8, "test2_result"); + ASSERT_EQ(skel->bss->test3_result, 8, "test3_result"); + +cleanup: + close(link1_fd); + close(link2_fd); + fprobe_bpf_cookie__destroy(skel); +} + static void uprobe_subtest(struct test_bpf_cookie *skel) { DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts); @@ -249,6 +320,8 @@ void test_bpf_cookie(void) if (test__start_subtest("kprobe")) kprobe_subtest(skel); + if (test__start_subtest("rawkprobe")) + fprobe_subtest(); if (test__start_subtest("uprobe")) uprobe_subtest(skel); if (test__start_subtest("tracepoint")) diff --git a/tools/testing/selftests/bpf/progs/fprobe_bpf_cookie.c b/tools/testing/selftests/bpf/progs/fprobe_bpf_cookie.c new file mode 100644 index 000000000000..42cb109e5a30 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/fprobe_bpf_cookie.c @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include + +char _license[] SEC("license") = "GPL"; + +extern const void bpf_fentry_test1 __ksym; +extern const void bpf_fentry_test2 __ksym; +extern const void bpf_fentry_test3 __ksym; +extern const void bpf_fentry_test4 __ksym; +extern const void bpf_fentry_test5 __ksym; +extern const void bpf_fentry_test6 __ksym; +extern const void bpf_fentry_test7 __ksym; +extern const void bpf_fentry_test8 __ksym; + +/* No tests, just to trigger bpf_fentry_test* through tracing test_run */ +SEC("fentry/bpf_modify_return_test") +int BPF_PROG(test1) +{ + return 0; +} + +__u64 test2_result = 0; + +SEC("kprobe/bpf_fentry_test*") +int test2(struct pt_regs *ctx) +{ + __u64 cookie = bpf_get_attach_cookie(ctx); + __u64 addr = bpf_get_func_ip(ctx); + + test2_result += (const void *) addr == &bpf_fentry_test1 && cookie == 1; + test2_result += (const void *) addr == &bpf_fentry_test2 && cookie == 2; + test2_result += (const void *) addr == &bpf_fentry_test3 && cookie == 3; + test2_result += (const void *) addr == &bpf_fentry_test4 && cookie == 4; + test2_result += (const void *) addr == &bpf_fentry_test5 && cookie == 5; + test2_result += (const void *) addr == &bpf_fentry_test6 && cookie == 6; + test2_result += (const void *) addr == &bpf_fentry_test7 && cookie == 7; + test2_result += (const void *) addr == &bpf_fentry_test8 && cookie == 8; + + return 0; +} + +__u64 test3_result = 0; + +SEC("kretprobe/bpf_fentry_test*") +int test3(struct pt_regs *ctx) +{ + __u64 cookie = bpf_get_attach_cookie(ctx); + __u64 addr = bpf_get_func_ip(ctx); + + test3_result += (const void *) addr == &bpf_fentry_test1 && cookie == 8; + test3_result += (const void *) addr == &bpf_fentry_test2 && cookie == 7; + test3_result += (const void *) addr == &bpf_fentry_test3 && cookie == 6; + test3_result += (const void *) addr == &bpf_fentry_test4 && cookie == 5; + test3_result += (const void *) addr == &bpf_fentry_test5 && cookie == 4; + test3_result += (const void *) addr == &bpf_fentry_test6 && cookie == 3; + test3_result += (const void *) addr == &bpf_fentry_test7 && cookie == 2; + test3_result += (const void *) addr == &bpf_fentry_test8 && cookie == 1; + + return 0; +} -- 2.34.1