Skip to contentSkip to conversation

libbpf: clarify raw-address single kprobe attach behavior

PATCH Superseded Hoyeon Lee 2 participants 7 messages Mar 31 – Apr 1

Subsystems (4)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Hoyeon Lee

author
#0 lore raw

v4 cover libbpf: clarify raw-address single kprobe attach behavior

Today libbpf documents single-kprobe attach through func_name, with an optional offset. For the PMU-based non-legacy path, func_name = NULL with an absolute address in offset already works as well, but that is not described in the API.

This patchset clarifies this behavior. The first commit clarifies that, for the PMU-based path, callers can use func_name = NULL with an absolute address in offset for raw-address attach, and makes legacy tracefs/debugfs kprobes reject that form. The second commit adds PERF and LINK selftests for the raw-address form, and checks that LEGACY rejects it.

---
Changes in v4:
- Inline raw-address error formatting and remove the probe_target buffer

Changes in v3:
- Drop bpf_kprobe_opts.addr and reuse offset when func_name is NULL
- Make legacy tracefs/debugfs kprobes reject the raw-address form
- Update selftests to cover PERF/LINK raw-address attach and LEGACY reject

Changes in v2:
- Fix line wrapping and indentation

Hoyeon Lee (2):
libbpf: clarify raw-address single kprobe attach behavior selftests/bpf: add test for raw-address single kprobe attach

 tools/lib/bpf/libbpf.c                        | 21 +++--
 tools/lib/bpf/libbpf.h                        |  3 +-
 .../selftests/bpf/prog_tests/attach_probe.c   | 82 +++++++++++++++++++
 3 files changed, 96 insertions(+), 10 deletions(-)
Signature · 1 line
2.52.0
author
#1 lore raw

PATCH v4 1/2 libbpf: clarify raw-address single kprobe attach behavior

bpf_program__attach_kprobe_opts() documents single-kprobe attach through func_name, with an optional offset. For the PMU-based non-legacy path, func_name = NULL with an absolute address in offset already works as well, but that is not described in the API.

This commit clarifies this existing non-legacy behavior. For PMU-based attach, callers can use func_name = NULL with an absolute address in offset as the raw-address form. For legacy tracefs/debugfs kprobes, reject this form explicitly.

---
 tools/lib/bpf/libbpf.c | 21 ++++++++++++---------
 tools/lib/bpf/libbpf.h |  3 ++-
 2 files changed, 14 insertions(+), 10 deletions(-)
tools/lib/bpf/libbpf.c +12 -9
index 9ea41f40dc82..9083f542a3b0 100644
@@ -11523,7 +11523,8 @@ static int determine_uprobe_retprobe_bit(void)
 #define PERF_UPROBE_REF_CTR_OFFSET_SHIFT 32
 
 static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
-				 uint64_t offset, int pid, size_t ref_ctr_off)
+				 uint64_t offset_or_addr, int pid,
+				 size_t ref_ctr_off)
 {
 	const size_t attr_sz = sizeof(struct perf_event_attr);
 	struct perf_event_attr attr;
@@ -11558,7 +11559,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
 	attr.type = type;
 	attr.config |= (__u64)ref_ctr_off << PERF_UPROBE_REF_CTR_OFFSET_SHIFT;
 	attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */
-	attr.config2 = offset;		 /* kprobe_addr or probe_offset */
+	attr.config2 = offset_or_addr;	 /* kprobe_addr or probe_offset */
 
 	/* pid filter is meaningful only for uprobes */
 	pfd = syscall(__NR_perf_event_open, &attr,
@@ -11816,6 +11817,8 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
 	default:
 		return libbpf_err_ptr(-EINVAL);
 	}
+	if (!func_name && legacy)
+		return libbpf_err_ptr(-ENOTSUP);
 
 	if (!legacy) {
 		pfd = perf_event_open_probe(false /* uprobe */, retprobe,
@@ -11835,21 +11838,21 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
 						    offset, -1 /* pid */);
 	}
 	if (pfd < 0) {
-		err = -errno;
-		pr_warn("prog '%s': failed to create %s '%s+0x%zx' perf event: %s\n",
+		err = pfd;
+		pr_warn("prog '%s': failed to create %s '%s%s0x%zx' perf event: %s\n",
 			prog->name, retprobe ? "kretprobe" : "kprobe",
-			func_name, offset,
-			errstr(err));
+			func_name ?: "", func_name ? "+" : "",
+			offset, errstr(err));
 		goto err_out;
 	}
 	link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
 	err = libbpf_get_error(link);
 	if (err) {
 		close(pfd);
-		pr_warn("prog '%s': failed to attach to %s '%s+0x%zx': %s\n",
+		pr_warn("prog '%s': failed to attach to %s '%s%s0x%zx': %s\n",
 			prog->name, retprobe ? "kretprobe" : "kprobe",
-			func_name, offset,
-			errstr(err));
+			func_name ?: "", func_name ? "+" : "",
+			offset, errstr(err));
 		goto err_clean_legacy;
 	}
 	if (legacy) {
tools/lib/bpf/libbpf.h +2 -1
index 0be34852350f..f75f3ab0f20c 100644
@@ -557,7 +557,7 @@ struct bpf_kprobe_opts {
 	size_t sz;
 	/* custom user-provided value fetchable through bpf_get_attach_cookie() */
 	__u64 bpf_cookie;
-	/* function's offset to install kprobe to */
+	/* function offset, or raw address if func_name == NULL (non-legacy) */
 	size_t offset;
 	/* kprobe is return probe */
 	bool retprobe;
@@ -565,6 +565,7 @@ struct bpf_kprobe_opts {
 	enum probe_attach_mode attach_mode;
 	size_t :0;
 };
+
 #define bpf_kprobe_opts__last_field attach_mode
 
 LIBBPF_API struct bpf_link *
Signature · 1 line
2.52.0
#2 lore raw
Quoted 22 lines · Hoyeon Lee

bpf_program__attach_kprobe_opts() documents single-kprobe attach through func_name, with an optional offset. For the PMU-based non-legacy path, func_name = NULL with an absolute address in offset already works as well, but that is not described in the API.

This commit clarifies this existing non-legacy behavior. For PMU-based attach, callers can use func_name = NULL with an absolute address in offset as the raw-address form. For legacy tracefs/debugfs kprobes, reject this form explicitly.

---
 tools/lib/bpf/libbpf.c | 21 ++++++++++++---------
 tools/lib/bpf/libbpf.h |  3 ++-
 2 files changed, 14 insertions(+), 10 deletions(-)
tools/lib/bpf/libbpf.c +0 -0
index 9ea41f40dc82..9083f542a3b0 100644

tools/lib/bpf/libbpf.c static int determine_uprobe_retprobe_bit(void) · line 11526

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -1
@@ -11523,7 +11523,8 @@ static int determine_uprobe_retprobe_bit(void)
 #define PERF_UPROBE_REF_CTR_OFFSET_SHIFT 32
 
 static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
-                                uint64_t offset, int pid, size_t ref_ctr_off)
+                                uint64_t offset_or_addr, int pid,

don't rename it, keep it as "offset"

Quoted 14 lines
tools/lib/bpf/libbpf.c +2 -1
+                                size_t ref_ctr_off)
 {
        const size_t attr_sz = sizeof(struct perf_event_attr);
        struct perf_event_attr attr;
@@ -11558,7 +11559,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
        attr.type = type;
        attr.config |= (__u64)ref_ctr_off << PERF_UPROBE_REF_CTR_OFFSET_SHIFT;
        attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */
-       attr.config2 = offset;           /* kprobe_addr or probe_offset */
+       attr.config2 = offset_or_addr;   /* kprobe_addr or probe_offset */
 
        /* pid filter is meaningful only for uprobes */
        pfd = syscall(__NR_perf_event_open, &attr,

tools/lib/bpf/libbpf.c bpf_program__attach_kprobe_opts(const struct bpf_program *prog, · line 11821

Quoted 5 lines
tools/lib/bpf/libbpf.c +2 -0
@@ -11816,6 +11817,8 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
        default:
                return libbpf_err_ptr(-EINVAL);
        }
+       if (!func_name && legacy)
+               return libbpf_err_ptr(-ENOTSUP);

-EOPNOTSUPP

pw-bot: cr

Quoted 37 lines
tools/lib/bpf/libbpf.c +7 -7
        if (!legacy) {
                pfd = perf_event_open_probe(false /* uprobe */, retprobe,
@@ -11835,21 +11838,21 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
                                                    offset, -1 /* pid */);
        }
        if (pfd < 0) {
-               err = -errno;
-               pr_warn("prog '%s': failed to create %s '%s+0x%zx' perf event: %s\n",
+               err = pfd;
+               pr_warn("prog '%s': failed to create %s '%s%s0x%zx' perf event: %s\n",
                        prog->name, retprobe ? "kretprobe" : "kprobe",
-                       func_name, offset,
-                       errstr(err));
+                       func_name ?: "", func_name ? "+" : "",
+                       offset, errstr(err));
                goto err_out;
        }
        link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
        err = libbpf_get_error(link);
        if (err) {
                close(pfd);
-               pr_warn("prog '%s': failed to attach to %s '%s+0x%zx': %s\n",
+               pr_warn("prog '%s': failed to attach to %s '%s%s0x%zx': %s\n",
                        prog->name, retprobe ? "kretprobe" : "kprobe",
-                       func_name, offset,
-                       errstr(err));
+                       func_name ?: "", func_name ? "+" : "",
+                       offset, errstr(err));
                goto err_clean_legacy;
        }
        if (legacy) {
tools/lib/bpf/libbpf.h +0 -0
index 0be34852350f..f75f3ab0f20c 100644

tools/lib/bpf/libbpf.h struct bpf_kprobe_opts { · line 560

Quoted 5 lines
tools/lib/bpf/libbpf.h +1 -1
@@ -557,7 +557,7 @@ struct bpf_kprobe_opts {
        size_t sz;
        /* custom user-provided value fetchable through bpf_get_attach_cookie() */
        __u64 bpf_cookie;
-       /* function's offset to install kprobe to */
+       /* function offset, or raw address if func_name == NULL (non-legacy) */

legacy mode will be useful on very-very old kernels only, let's not distract with constant mentions for "non-legacy", please remove "(non-legacy)" here and other legacy mentions above

but also, while you are at it, let's add a proper doc comment to bpf_program__attach_kprobe and bpf_program__attach_kprobe_opts APIs? That seems like a more obvious place to explain name+offset and offset as absolute address behavior, IMO

Quoted 14 lines
tools/lib/bpf/libbpf.h +1 -1
        size_t offset;
        /* kprobe is return probe */
        bool retprobe;
@@ -565,6 +565,7 @@ struct bpf_kprobe_opts {
        enum probe_attach_mode attach_mode;
        size_t :0;
 };
+
 #define bpf_kprobe_opts__last_field attach_mode
 
 LIBBPF_API struct bpf_link *
--

2.52.0

author
#3 lore raw
Quoted 26 lines · Andrii Nakryiko
Quoted 24 lines · Hoyeon Lee

bpf_program__attach_kprobe_opts() documents single-kprobe attach through func_name, with an optional offset. For the PMU-based non-legacy path, func_name = NULL with an absolute address in offset already works as well, but that is not described in the API.

This commit clarifies this existing non-legacy behavior. For PMU-based attach, callers can use func_name = NULL with an absolute address in offset as the raw-address form. For legacy tracefs/debugfs kprobes, reject this form explicitly.

---
 tools/lib/bpf/libbpf.c | 21 ++++++++++++---------
 tools/lib/bpf/libbpf.h |  3 ++-
 2 files changed, 14 insertions(+), 10 deletions(-)
tools/lib/bpf/libbpf.c +0 -0
index 9ea41f40dc82..9083f542a3b0 100644
@@ -11523,7 +11523,8 @@ static int determine_uprobe_retprobe_bit(void)
 #define PERF_UPROBE_REF_CTR_OFFSET_SHIFT 32
 
Quoted 6 lines

tools/lib/bpf/libbpf.c static int determine_uprobe_retprobe_bit(void) · line 11526

Quoted 3 lines
tools/lib/bpf/libbpf.c +1 -1
 static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
-                                uint64_t offset, int pid, size_t ref_ctr_off)
+                                uint64_t offset_or_addr, int pid,

don't rename it, keep it as "offset"

Got it, I'll drop it in v5.

Quoted 72 lines
Quoted 14 lines
tools/lib/bpf/libbpf.c +2 -1
+                                size_t ref_ctr_off)
 {
        const size_t attr_sz = sizeof(struct perf_event_attr);
        struct perf_event_attr attr;
@@ -11558,7 +11559,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
        attr.type = type;
        attr.config |= (__u64)ref_ctr_off << PERF_UPROBE_REF_CTR_OFFSET_SHIFT;
        attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */
-       attr.config2 = offset;           /* kprobe_addr or probe_offset */
+       attr.config2 = offset_or_addr;   /* kprobe_addr or probe_offset */
 
        /* pid filter is meaningful only for uprobes */
        pfd = syscall(__NR_perf_event_open, &attr,

tools/lib/bpf/libbpf.c bpf_program__attach_kprobe_opts(const struct bpf_program *prog, · line 11821

Quoted 5 lines
tools/lib/bpf/libbpf.c +2 -0
@@ -11816,6 +11817,8 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
        default:
                return libbpf_err_ptr(-EINVAL);
        }
+       if (!func_name && legacy)
+               return libbpf_err_ptr(-ENOTSUP);

-EOPNOTSUPP

pw-bot: cr

Quoted 37 lines
tools/lib/bpf/libbpf.c +7 -7
        if (!legacy) {
                pfd = perf_event_open_probe(false /* uprobe */, retprobe,
@@ -11835,21 +11838,21 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
                                                    offset, -1 /* pid */);
        }
        if (pfd < 0) {
-               err = -errno;
-               pr_warn("prog '%s': failed to create %s '%s+0x%zx' perf event: %s\n",
+               err = pfd;
+               pr_warn("prog '%s': failed to create %s '%s%s0x%zx' perf event: %s\n",
                        prog->name, retprobe ? "kretprobe" : "kprobe",
-                       func_name, offset,
-                       errstr(err));
+                       func_name ?: "", func_name ? "+" : "",
+                       offset, errstr(err));
                goto err_out;
        }
        link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
        err = libbpf_get_error(link);
        if (err) {
                close(pfd);
-               pr_warn("prog '%s': failed to attach to %s '%s+0x%zx': %s\n",
+               pr_warn("prog '%s': failed to attach to %s '%s%s0x%zx': %s\n",
                        prog->name, retprobe ? "kretprobe" : "kprobe",
-                       func_name, offset,
-                       errstr(err));
+                       func_name ?: "", func_name ? "+" : "",
+                       offset, errstr(err));
                goto err_clean_legacy;
        }
        if (legacy) {
tools/lib/bpf/libbpf.h +0 -0
index 0be34852350f..f75f3ab0f20c 100644

tools/lib/bpf/libbpf.h struct bpf_kprobe_opts { · line 560

Quoted 5 lines
tools/lib/bpf/libbpf.h +1 -1
@@ -557,7 +557,7 @@ struct bpf_kprobe_opts {
        size_t sz;
        /* custom user-provided value fetchable through bpf_get_attach_cookie() */
        __u64 bpf_cookie;
-       /* function's offset to install kprobe to */
+       /* function offset, or raw address if func_name == NULL (non-legacy) */

legacy mode will be useful on very-very old kernels only, let's not distract with constant mentions for "non-legacy", please remove "(non-legacy)" here and other legacy mentions above

Quoted 5 lines

but also, while you are at it, let's add a proper doc comment to bpf_program__attach_kprobe and bpf_program__attach_kprobe_opts APIs? That seems like a more obvious place to explain name+offset and offset as absolute address behavior, IMO

Right. I'll drop the "(non-legacy)" wording and add proper API documentation to bpf_program__attach_kprobe() and bpf_program__attach_kprobe_opts() in v5.

Quoted 14 lines
Quoted 14 lines
tools/lib/bpf/libbpf.h +1 -1
        size_t offset;
        /* kprobe is return probe */
        bool retprobe;
@@ -565,6 +565,7 @@ struct bpf_kprobe_opts {
        enum probe_attach_mode attach_mode;
        size_t :0;
 };
+
 #define bpf_kprobe_opts__last_field attach_mode
 
 LIBBPF_API struct bpf_link *
--

2.52.0

author
#4 lore raw

PATCH v4 2/2 selftests/bpf: add test for raw-address single kprobe attach

In reply to Hoyeon Lee

Currently, attach_probe covers manual single-kprobe attaches by func_name, but not the raw-address form that the PMU-based single-kprobe path can accept.

This commit adds PERF and LINK raw-address subtests by resolving SYS_NANOSLEEP_KPROBE_NAME through kallsyms, passing the absolute address in bpf_kprobe_opts.offset with func_name = NULL, and verifying that kprobe and kretprobe are still triggered. It also verifies that LEGACY rejects the same form.

---
 .../selftests/bpf/prog_tests/attach_probe.c   | 82 +++++++++++++++++++
 1 file changed, 82 insertions(+)
tools/testing/selftests/bpf/prog_tests/attach_probe.c +82 -0
index 9e77e5da7097..817c4794d54e 100644
@@ -123,6 +123,82 @@ static void test_attach_probe_manual(enum probe_attach_mode attach_mode)
 	test_attach_probe_manual__destroy(skel);
 }
 
+/* manual attach address-based kprobe/kretprobe testings */
+static void test_attach_kprobe_by_addr(enum probe_attach_mode attach_mode)
+{
+	DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, kprobe_opts);
+	struct test_attach_probe_manual *skel;
+	unsigned long func_addr;
+
+	if (!ASSERT_OK(load_kallsyms(), "load_kallsyms"))
+		return;
+
+	func_addr = ksym_get_addr(SYS_NANOSLEEP_KPROBE_NAME);
+	if (!ASSERT_NEQ(func_addr, 0UL, "func_addr"))
+		return;
+
+	skel = test_attach_probe_manual__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_kprobe_manual_open_and_load"))
+		return;
+
+	kprobe_opts.attach_mode = attach_mode;
+	kprobe_opts.retprobe = false;
+	kprobe_opts.offset = func_addr;
+	skel->links.handle_kprobe =
+		bpf_program__attach_kprobe_opts(skel->progs.handle_kprobe,
+						NULL, &kprobe_opts);
+	if (!ASSERT_OK_PTR(skel->links.handle_kprobe, "attach_kprobe_by_addr"))
+		goto cleanup;
+
+	kprobe_opts.retprobe = true;
+	skel->links.handle_kretprobe =
+		bpf_program__attach_kprobe_opts(skel->progs.handle_kretprobe,
+						NULL, &kprobe_opts);
+	if (!ASSERT_OK_PTR(skel->links.handle_kretprobe,
+			   "attach_kretprobe_by_addr"))
+		goto cleanup;
+
+	/* trigger & validate kprobe && kretprobe */
+	usleep(1);
+
+	ASSERT_EQ(skel->bss->kprobe_res, 1, "check_kprobe_res");
+	ASSERT_EQ(skel->bss->kretprobe_res, 2, "check_kretprobe_res");
+
+cleanup:
+	test_attach_probe_manual__destroy(skel);
+}
+
+/* reject legacy address-based kprobe attach */
+static void test_attach_kprobe_legacy_by_addr_reject(void)
+{
+	DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, kprobe_opts);
+	struct test_attach_probe_manual *skel;
+	unsigned long func_addr;
+
+	if (!ASSERT_OK(load_kallsyms(), "load_kallsyms"))
+		return;
+
+	func_addr = ksym_get_addr(SYS_NANOSLEEP_KPROBE_NAME);
+	if (!ASSERT_NEQ(func_addr, 0UL, "func_addr"))
+		return;
+
+	skel = test_attach_probe_manual__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_kprobe_manual_open_and_load"))
+		return;
+
+	kprobe_opts.attach_mode = PROBE_ATTACH_MODE_LEGACY;
+	kprobe_opts.offset = func_addr;
+	skel->links.handle_kprobe =
+		bpf_program__attach_kprobe_opts(skel->progs.handle_kprobe,
+						NULL, &kprobe_opts);
+	if (ASSERT_ERR_PTR(skel->links.handle_kprobe,
+			   "attach_kprobe_legacy_by_addr"))
+		ASSERT_EQ(libbpf_get_error(skel->links.handle_kprobe), -ENOTSUP,
+			  "attach_kprobe_legacy_by_addr_err");
+
+	test_attach_probe_manual__destroy(skel);
+}
+
 /* attach uprobe/uretprobe long event name testings */
 static void test_attach_uprobe_long_event_name(void)
 {
@@ -416,6 +492,12 @@ void test_attach_probe(void)
 		test_attach_probe_manual(PROBE_ATTACH_MODE_PERF);
 	if (test__start_subtest("manual-link"))
 		test_attach_probe_manual(PROBE_ATTACH_MODE_LINK);
+	if (test__start_subtest("kprobe-perf-by-addr"))
+		test_attach_kprobe_by_addr(PROBE_ATTACH_MODE_PERF);
+	if (test__start_subtest("kprobe-link-by-addr"))
+		test_attach_kprobe_by_addr(PROBE_ATTACH_MODE_LINK);
+	if (test__start_subtest("kprobe-legacy-by-addr-reject"))
+		test_attach_kprobe_legacy_by_addr_reject();
 
 	if (test__start_subtest("auto"))
 		test_attach_probe_auto(skel);
Signature · 1 line
2.52.0
#5 lore raw

Re: [PATCH bpf-next v4 2/2] selftests/bpf: add test for raw-address single kprobe attach

Quoted 21 lines · Hoyeon Lee

Currently, attach_probe covers manual single-kprobe attaches by func_name, but not the raw-address form that the PMU-based single-kprobe path can accept.

This commit adds PERF and LINK raw-address subtests by resolving SYS_NANOSLEEP_KPROBE_NAME through kallsyms, passing the absolute address in bpf_kprobe_opts.offset with func_name = NULL, and verifying that kprobe and kretprobe are still triggered. It also verifies that LEGACY rejects the same form.

---
 .../selftests/bpf/prog_tests/attach_probe.c   | 82 +++++++++++++++++++
 1 file changed, 82 insertions(+)
tools/testing/selftests/bpf/prog_tests/attach_probe.c +0 -0
index 9e77e5da7097..817c4794d54e 100644

tools/testing/selftests/bpf/prog_tests/attach_probe.c static void test_attach_probe_manual(enum probe_attach_mode attach_mode) · line 129

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/attach_probe.c +4 -0
@@ -123,6 +123,82 @@ static void test_attach_probe_manual(enum probe_attach_mode attach_mode)
        test_attach_probe_manual__destroy(skel);
 }
 
+/* manual attach address-based kprobe/kretprobe testings */
+static void test_attach_kprobe_by_addr(enum probe_attach_mode attach_mode)
+{
+       DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, kprobe_opts);

DECLARE_LIBBPF_OPTS is a legacy backwards-compatibility macro, prefer using shorter LIBBPF_OPTS

Quoted 91 lines
tools/testing/selftests/bpf/prog_tests/attach_probe.c +78 -1
+       struct test_attach_probe_manual *skel;
+       unsigned long func_addr;
+
+       if (!ASSERT_OK(load_kallsyms(), "load_kallsyms"))
+               return;
+
+       func_addr = ksym_get_addr(SYS_NANOSLEEP_KPROBE_NAME);
+       if (!ASSERT_NEQ(func_addr, 0UL, "func_addr"))
+               return;
+
+       skel = test_attach_probe_manual__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "skel_kprobe_manual_open_and_load"))
+               return;
+
+       kprobe_opts.attach_mode = attach_mode;
+       kprobe_opts.retprobe = false;
+       kprobe_opts.offset = func_addr;
+       skel->links.handle_kprobe =
+               bpf_program__attach_kprobe_opts(skel->progs.handle_kprobe,
+                                               NULL, &kprobe_opts);
+       if (!ASSERT_OK_PTR(skel->links.handle_kprobe, "attach_kprobe_by_addr"))
+               goto cleanup;
+
+       kprobe_opts.retprobe = true;
+       skel->links.handle_kretprobe =
+               bpf_program__attach_kprobe_opts(skel->progs.handle_kretprobe,
+                                               NULL, &kprobe_opts);
+       if (!ASSERT_OK_PTR(skel->links.handle_kretprobe,
+                          "attach_kretprobe_by_addr"))
+               goto cleanup;
+
+       /* trigger & validate kprobe && kretprobe */
+       usleep(1);
+
+       ASSERT_EQ(skel->bss->kprobe_res, 1, "check_kprobe_res");
+       ASSERT_EQ(skel->bss->kretprobe_res, 2, "check_kretprobe_res");
+
+cleanup:
+       test_attach_probe_manual__destroy(skel);
+}
+
+/* reject legacy address-based kprobe attach */
+static void test_attach_kprobe_legacy_by_addr_reject(void)
+{
+       DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, kprobe_opts);
+       struct test_attach_probe_manual *skel;
+       unsigned long func_addr;
+
+       if (!ASSERT_OK(load_kallsyms(), "load_kallsyms"))
+               return;
+
+       func_addr = ksym_get_addr(SYS_NANOSLEEP_KPROBE_NAME);
+       if (!ASSERT_NEQ(func_addr, 0UL, "func_addr"))
+               return;
+
+       skel = test_attach_probe_manual__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "skel_kprobe_manual_open_and_load"))
+               return;
+
+       kprobe_opts.attach_mode = PROBE_ATTACH_MODE_LEGACY;
+       kprobe_opts.offset = func_addr;
+       skel->links.handle_kprobe =
+               bpf_program__attach_kprobe_opts(skel->progs.handle_kprobe,
+                                               NULL, &kprobe_opts);
+       if (ASSERT_ERR_PTR(skel->links.handle_kprobe,
+                          "attach_kprobe_legacy_by_addr"))
+               ASSERT_EQ(libbpf_get_error(skel->links.handle_kprobe), -ENOTSUP,
+                         "attach_kprobe_legacy_by_addr_err");
+
+       test_attach_probe_manual__destroy(skel);
+}
+
 /* attach uprobe/uretprobe long event name testings */
 static void test_attach_uprobe_long_event_name(void)
 {
@@ -416,6 +492,12 @@ void test_attach_probe(void)
                test_attach_probe_manual(PROBE_ATTACH_MODE_PERF);
        if (test__start_subtest("manual-link"))
                test_attach_probe_manual(PROBE_ATTACH_MODE_LINK);
+       if (test__start_subtest("kprobe-perf-by-addr"))
+               test_attach_kprobe_by_addr(PROBE_ATTACH_MODE_PERF);
+       if (test__start_subtest("kprobe-link-by-addr"))
+               test_attach_kprobe_by_addr(PROBE_ATTACH_MODE_LINK);
+       if (test__start_subtest("kprobe-legacy-by-addr-reject"))
+               test_attach_kprobe_legacy_by_addr_reject();
 
        if (test__start_subtest("auto"))
                test_attach_probe_auto(skel);
--

2.52.0

author
#6 lore raw

Re: [PATCH bpf-next v4 2/2] selftests/bpf: add test for raw-address single kprobe attach

Quoted 26 lines · Andrii Nakryiko
Quoted 24 lines · Hoyeon Lee

Currently, attach_probe covers manual single-kprobe attaches by func_name, but not the raw-address form that the PMU-based single-kprobe path can accept.

This commit adds PERF and LINK raw-address subtests by resolving SYS_NANOSLEEP_KPROBE_NAME through kallsyms, passing the absolute address in bpf_kprobe_opts.offset with func_name = NULL, and verifying that kprobe and kretprobe are still triggered. It also verifies that LEGACY rejects the same form.

---
 .../selftests/bpf/prog_tests/attach_probe.c   | 82 +++++++++++++++++++
 1 file changed, 82 insertions(+)
tools/testing/selftests/bpf/prog_tests/attach_probe.c +0 -0
index 9e77e5da7097..817c4794d54e 100644
@@ -123,6 +123,82 @@ static void test_attach_probe_manual(enum probe_attach_mode attach_mode)
        test_attach_probe_manual__destroy(skel);
 }
 
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/attach_probe.c static void test_attach_probe_manual(enum probe_attach_mode attach_mode) · line 129

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/attach_probe.c +4 -0
+/* manual attach address-based kprobe/kretprobe testings */
+static void test_attach_kprobe_by_addr(enum probe_attach_mode attach_mode)
+{
+       DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, kprobe_opts);

DECLARE_LIBBPF_OPTS is a legacy backwards-compatibility macro, prefer using shorter LIBBPF_OPTS

Got it, I'll switch to LIBBPF_OPTS and updated the reject expectation to -EOPNOTSUPP in v5.

Quoted 91 lines
Quoted 91 lines
tools/testing/selftests/bpf/prog_tests/attach_probe.c +78 -1
+       struct test_attach_probe_manual *skel;
+       unsigned long func_addr;
+
+       if (!ASSERT_OK(load_kallsyms(), "load_kallsyms"))
+               return;
+
+       func_addr = ksym_get_addr(SYS_NANOSLEEP_KPROBE_NAME);
+       if (!ASSERT_NEQ(func_addr, 0UL, "func_addr"))
+               return;
+
+       skel = test_attach_probe_manual__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "skel_kprobe_manual_open_and_load"))
+               return;
+
+       kprobe_opts.attach_mode = attach_mode;
+       kprobe_opts.retprobe = false;
+       kprobe_opts.offset = func_addr;
+       skel->links.handle_kprobe =
+               bpf_program__attach_kprobe_opts(skel->progs.handle_kprobe,
+                                               NULL, &kprobe_opts);
+       if (!ASSERT_OK_PTR(skel->links.handle_kprobe, "attach_kprobe_by_addr"))
+               goto cleanup;
+
+       kprobe_opts.retprobe = true;
+       skel->links.handle_kretprobe =
+               bpf_program__attach_kprobe_opts(skel->progs.handle_kretprobe,
+                                               NULL, &kprobe_opts);
+       if (!ASSERT_OK_PTR(skel->links.handle_kretprobe,
+                          "attach_kretprobe_by_addr"))
+               goto cleanup;
+
+       /* trigger & validate kprobe && kretprobe */
+       usleep(1);
+
+       ASSERT_EQ(skel->bss->kprobe_res, 1, "check_kprobe_res");
+       ASSERT_EQ(skel->bss->kretprobe_res, 2, "check_kretprobe_res");
+
+cleanup:
+       test_attach_probe_manual__destroy(skel);
+}
+
+/* reject legacy address-based kprobe attach */
+static void test_attach_kprobe_legacy_by_addr_reject(void)
+{
+       DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, kprobe_opts);
+       struct test_attach_probe_manual *skel;
+       unsigned long func_addr;
+
+       if (!ASSERT_OK(load_kallsyms(), "load_kallsyms"))
+               return;
+
+       func_addr = ksym_get_addr(SYS_NANOSLEEP_KPROBE_NAME);
+       if (!ASSERT_NEQ(func_addr, 0UL, "func_addr"))
+               return;
+
+       skel = test_attach_probe_manual__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "skel_kprobe_manual_open_and_load"))
+               return;
+
+       kprobe_opts.attach_mode = PROBE_ATTACH_MODE_LEGACY;
+       kprobe_opts.offset = func_addr;
+       skel->links.handle_kprobe =
+               bpf_program__attach_kprobe_opts(skel->progs.handle_kprobe,
+                                               NULL, &kprobe_opts);
+       if (ASSERT_ERR_PTR(skel->links.handle_kprobe,
+                          "attach_kprobe_legacy_by_addr"))
+               ASSERT_EQ(libbpf_get_error(skel->links.handle_kprobe), -ENOTSUP,
+                         "attach_kprobe_legacy_by_addr_err");
+
+       test_attach_probe_manual__destroy(skel);
+}
+
 /* attach uprobe/uretprobe long event name testings */
 static void test_attach_uprobe_long_event_name(void)
 {
@@ -416,6 +492,12 @@ void test_attach_probe(void)
                test_attach_probe_manual(PROBE_ATTACH_MODE_PERF);
        if (test__start_subtest("manual-link"))
                test_attach_probe_manual(PROBE_ATTACH_MODE_LINK);
+       if (test__start_subtest("kprobe-perf-by-addr"))
+               test_attach_kprobe_by_addr(PROBE_ATTACH_MODE_PERF);
+       if (test__start_subtest("kprobe-link-by-addr"))
+               test_attach_kprobe_by_addr(PROBE_ATTACH_MODE_LINK);
+       if (test__start_subtest("kprobe-legacy-by-addr-reject"))
+               test_attach_kprobe_legacy_by_addr_reject();
 
        if (test__start_subtest("auto"))
                test_attach_probe_auto(skel);
--

2.52.0