Skip to contentSkip to conversation

bpf: Allow ringbuf memory to be used as map key

PATCH Superseded Dave Marchevsky 3 participants 6 messages Sep 23, 2022

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 bpf: Allow ringbuf memory to be used as map key

This patch adds support for the following pattern:

  struct some_data *data = bpf_ringbuf_reserve(&ringbuf, sizeof(struct some_data, 0));
  if (!data)
    return;
  bpf_map_lookup_elem(&another_map, &data->some_field);
  bpf_ringbuf_submit(data);

Currently the verifier does not consider bpf_ringbuf_reserve's PTR_TO_MEM | MEM_ALLOC ret type a valid key input to bpf_map_lookup_elem. Since PTR_TO_MEM is by definition a valid region of memory, it is safe to use it as a key for lookups.

---
v2->v3: lore.kernel.org/bpf/[email protected]

* Add Yonghong ack, rebase

v1->v2: lore.kernel.org/bpf/[email protected]

* Move test changes into separate patch - patch 2 in this series. (Kumar, Yonghong). That patch's changelog enumerates specific changes from v1
* Remove PTR_TO_MEM addition from this patch - patch 1 (Yonghong)
* I don't have a usecase for PTR_TO_MEM w/o MEM_ALLOC
* Add "if (!data)" error check to example pattern in this patch (Yonghong)
* Remove patch 2 from v1's series, which removed map_key_value_types as it was more-or-less duplicate of mem_types
* Now that PTR_TO_MEM isn't added here, more differences between map_key_value_types and mem_types, and no usecase for PTR_TO_BUF, so drop for now.

 kernel/bpf/verifier.c | 1 +
 1 file changed, 1 insertion(+)
kernel/bpf/verifier.c +1 -0
index 6f6d2d511c06..97351ae3e7a7 100644
@@ -5641,6 +5641,7 @@ static const struct bpf_reg_types map_key_value_types = {
 		PTR_TO_PACKET_META,
 		PTR_TO_MAP_KEY,
 		PTR_TO_MAP_VALUE,
+		PTR_TO_MEM | MEM_ALLOC,
 	},
 };
 
Signature · 1 line
2.30.2
#1 lore raw

PATCH v4 2/2 selftests/bpf: Add test verifying bpf_ringbuf_reserve retval use in map ops

Add a test_ringbuf_map_key test prog, borrowing heavily from extant test_ringbuf.c. The program tries to use the result of bpf_ringbuf_reserve as map_key, which was not possible before previouis commits in this series. The test runner added to prog_tests/ringbuf.c verifies that the program loads and does basic sanity checks to confirm that it runs as expected.

Also, refactor test_ringbuf such that runners for existing test_ringbuf and newly-added test_ringbuf_map_key are subtests of 'ringbuf' top-level test.

---
v3->v4: lore.kernel.org/bpf/[email protected]

* Fix some nits (Yonghong)
* make subtest runner functions static
* don't goto cleanup if -EDONE check fails
* add 'workaround' to comment in test to ease future grepping
* Add Yonghong ack

v2->v3: lore.kernel.org/bpf/[email protected]

* Test that ring_buffer__poll returns -EDONE (Alexei)

v1->v2: lore.kernel.org/bpf/[email protected]

* Actually run the program instead of just loading (Yonghong)
* Add a bpf_map_update_elem call to the test (Yonghong)
* Refactor runner such that existing test and newly-added test are subtests of 'ringbuf' top-level test (Yonghong)
* Remove unused globals in test prog (Yonghong)

 tools/testing/selftests/bpf/Makefile          |  8 ++-
 .../selftests/bpf/prog_tests/ringbuf.c        | 64 ++++++++++++++++-
 .../bpf/progs/test_ringbuf_map_key.c          | 71 +++++++++++++++++++
 3 files changed, 139 insertions(+), 4 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c
tools/testing/selftests/bpf/Makefile +5 -3
index 5898d3828b82..28bd482f34a1 100644
@@ -358,9 +358,11 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h		\
 		test_subskeleton.skel.h test_subskeleton_lib.skel.h	\
 		test_usdt.skel.h
 
-LSKELS := fentry_test.c fexit_test.c fexit_sleep.c \
-	test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c \
-	map_ptr_kern.c core_kern.c core_kern_overflow.c
+LSKELS := fentry_test.c fexit_test.c fexit_sleep.c atomics.c 		\
+	trace_printk.c trace_vprintk.c map_ptr_kern.c 			\
+	core_kern.c core_kern_overflow.c test_ringbuf.c			\
+	test_ringbuf_map_key.c
+
 # Generate both light skeleton and libbpf skeleton for these
 LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test.c \
 	kfunc_call_test_subprog.c
tools/testing/selftests/bpf/prog_tests/ringbuf.c +63 -1

85 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c new file +71 -0

71 lines not on this page · raw message

Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH v4 bpf-next 2/2] selftests/bpf: Add test verifying bpf_ringbuf_reserve retval use in map ops

Quoted 49 lines · Dave Marchevsky

Add a test_ringbuf_map_key test prog, borrowing heavily from extant test_ringbuf.c. The program tries to use the result of bpf_ringbuf_reserve as map_key, which was not possible before previouis commits in this series. The test runner added to prog_tests/ringbuf.c verifies that the program loads and does basic sanity checks to confirm that it runs as expected.

Also, refactor test_ringbuf such that runners for existing test_ringbuf and newly-added test_ringbuf_map_key are subtests of 'ringbuf' top-level test.

---
v3->v4: lore.kernel.org/bpf/[email protected]

* Fix some nits (Yonghong)
* make subtest runner functions static
* don't goto cleanup if -EDONE check fails
* add 'workaround' to comment in test to ease future grepping
* Add Yonghong ack

v2->v3: lore.kernel.org/bpf/[email protected]

* Test that ring_buffer__poll returns -EDONE (Alexei)

v1->v2: lore.kernel.org/bpf/[email protected]

* Actually run the program instead of just loading (Yonghong)
* Add a bpf_map_update_elem call to the test (Yonghong)
* Refactor runner such that existing test and newly-added test are subtests of 'ringbuf' top-level test (Yonghong)
* Remove unused globals in test prog (Yonghong)

 tools/testing/selftests/bpf/Makefile          |  8 ++-
 .../selftests/bpf/prog_tests/ringbuf.c        | 64 ++++++++++++++++-
 .../bpf/progs/test_ringbuf_map_key.c          | 71 +++++++++++++++++++
 3 files changed, 139 insertions(+), 4 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c
tools/testing/selftests/bpf/Makefile +0 -0
index 5898d3828b82..28bd482f34a1 100644
@@ -358,9 +358,11 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h              \
                test_subskeleton.skel.h test_subskeleton_lib.skel.h     \
                test_usdt.skel.h
 

[...]

Quoted 7 lines
tools/testing/selftests/bpf/Makefile +7 -0
+static void ringbuf_map_key_subtest(void)
+{
+       int err;
+
+       skel_map_key = test_ringbuf_map_key_lskel__open();
+       if (!ASSERT_OK_PTR(skel_map_key, "test_ringbuf_map_key_lskel__open"))
+               return;

tools/testing/selftests/bpf/Makefile

Quoted 10 lines
tools/testing/selftests/bpf/Makefile +10 -0
+
+       skel_map_key->maps.ringbuf.max_entries = getpagesize();
+       skel_map_key->bss->pid = getpid();
+
+       err = test_ringbuf_map_key_lskel__load(skel_map_key);
+       if (!ASSERT_OK(err, "test_ringbuf_map_key_lskel__load"))
+               goto cleanup;
+
+       ringbuf = ring_buffer__new(skel_map_key->maps.ringbuf.map_fd,
+                                  process_map_key_sample, NULL, NULL);

if (!ASSERT_OK_PTR(ringbuf, ...))

is missing

Quoted 38 lines
tools/testing/selftests/bpf/Makefile +23 -0
+
+       err = test_ringbuf_map_key_lskel__attach(skel_map_key);
+       if (!ASSERT_OK(err, "test_ringbuf_map_key_lskel__attach"))
+               goto cleanup_ringbuf;
+
+       syscall(__NR_getpgid);
+       ASSERT_EQ(skel_map_key->bss->seq, 1, "skel_map_key->bss->seq");
+       err = ring_buffer__poll(ringbuf, -1);
+       ASSERT_EQ(err, -EDONE, "ring_buffer__poll");
+
+cleanup_ringbuf:
+       ring_buffer__free(ringbuf);
+cleanup:
+       test_ringbuf_map_key_lskel__destroy(skel_map_key);
+}
+
+void test_ringbuf(void)
+{
+       if (test__start_subtest("ringbuf"))
+               ringbuf_subtest();
+       if (test__start_subtest("ringbuf_map_key"))
+               ringbuf_map_key_subtest();
+}
tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c new file +9 -0
new file mode 100644
index 000000000000..44f89c4d1f9e
@@ -0,0 +1,71 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+

tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c line 19

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c new file +10 -0
+struct sample {
+       int pid;
+       int seq;
+       long value;
+       char comm[16];
+};
+
+struct {
+       __uint(type, BPF_MAP_TYPE_RINGBUF);
+       __uint(max_entries, 4096);

you are setting max_entries from user-space, so best drop this line here

Quoted 6 lines
tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c new file +6 -0
+} ringbuf SEC(".maps");
+
+struct {
+       __uint(type, BPF_MAP_TYPE_HASH);
+       __uint(max_entries, 1000);
+       __type(key, struct sample);

tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c line 35

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c new file +10 -0
+       __type(value, int);
+} hash_map SEC(".maps");
+
+/* inputs */
+int pid = 0;
+
+/* inner state */
+long seq = 0;
+
+SEC("fentry/" SYS_PREFIX "sys_getpgid")

if we use SEC("ksyscall/getpgid") this will work on s390x as well. We don't seem to gain anything from using fentry here, do we? Currently entire ringbuf selftests is denied on s390x, but you are switching it to use subtest, so we can enable at least the new subtest?

Quoted 39 lines
tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c new file +36 -1
+int test_ringbuf_mem_map_key(void *ctx)
+{
+       int cur_pid = bpf_get_current_pid_tgid() >> 32;
+       struct sample *sample, sample_copy;
+       int *lookup_val;
+
+       if (cur_pid != pid)
+               return 0;
+
+       sample = bpf_ringbuf_reserve(&ringbuf, sizeof(*sample), 0);
+       if (!sample)
+               return 0;
+
+       sample->pid = pid;
+       bpf_get_current_comm(sample->comm, sizeof(sample->comm));
+       sample->seq = ++seq;
+       sample->value = 42;
+
+       /* test using 'sample' (PTR_TO_MEM | MEM_ALLOC) as map key arg
+        */
+       lookup_val = (int *)bpf_map_lookup_elem(&hash_map, sample);
+
+       /* workaround - memcpy is necessary so that verifier doesn't
+        * complain with:
+        *   verifier internal error: more than one arg with ref_obj_id R3
+        * when trying to do bpf_map_update_elem(&hash_map, sample, &sample->seq, BPF_ANY);
+        *
+        * Since bpf_map_lookup_elem above uses 'sample' as key, test using
+        * sample field as value below
+        */
+       __builtin_memcpy(&sample_copy, sample, sizeof(struct sample));
+       bpf_map_update_elem(&hash_map, &sample_copy, &sample->seq, BPF_ANY);
+
+       bpf_ringbuf_submit(sample, 0);
+       return 0;
+}
--

2.30.2

#3 lore raw
In reply to Dave Marchevsky
Quoted 44 lines · Dave Marchevsky

This patch adds support for the following pattern:

  struct some_data *data = bpf_ringbuf_reserve(&ringbuf, sizeof(struct some_data, 0));
  if (!data)
    return;
  bpf_map_lookup_elem(&another_map, &data->some_field);
  bpf_ringbuf_submit(data);

Currently the verifier does not consider bpf_ringbuf_reserve's PTR_TO_MEM | MEM_ALLOC ret type a valid key input to bpf_map_lookup_elem. Since PTR_TO_MEM is by definition a valid region of memory, it is safe to use it as a key for lookups.

---
v2->v3: lore.kernel.org/bpf/[email protected]

* Add Yonghong ack, rebase

v1->v2: lore.kernel.org/bpf/[email protected]

* Move test changes into separate patch - patch 2 in this series. (Kumar, Yonghong). That patch's changelog enumerates specific changes from v1
* Remove PTR_TO_MEM addition from this patch - patch 1 (Yonghong)
* I don't have a usecase for PTR_TO_MEM w/o MEM_ALLOC
* Add "if (!data)" error check to example pattern in this patch (Yonghong)
* Remove patch 2 from v1's series, which removed map_key_value_types as it was more-or-less duplicate of mem_types
* Now that PTR_TO_MEM isn't added here, more differences between map_key_value_types and mem_types, and no usecase for PTR_TO_BUF, so drop for now.

 kernel/bpf/verifier.c | 1 +
 1 file changed, 1 insertion(+)
kernel/bpf/verifier.c +0 -0
index 6f6d2d511c06..97351ae3e7a7 100644

kernel/bpf/verifier.c static const struct bpf_reg_types map_key_value_types = { · line 5644

Quoted 4 lines
kernel/bpf/verifier.c +1 -0
@@ -5641,6 +5641,7 @@ static const struct bpf_reg_types map_key_value_types = {
                PTR_TO_PACKET_META,
                PTR_TO_MAP_KEY,
                PTR_TO_MAP_VALUE,
+               PTR_TO_MEM | MEM_ALLOC,

are there any differences between mem_types and map_key_value_types? If not, should we just drop map_key_value_types? mem_types also alloc any PTR_TO_MEM (not just ringbuf's MEM_ALLOC) and PTR_TO_BUF (tracepoint context structs, I think?)

Quoted 6 lines
kernel/bpf/verifier.c +0 -1
        },
 };
 
--

2.30.2

#4 lore raw
Quoted 41 lines · Andrii Nakryiko
Quoted 39 lines · Dave Marchevsky

This patch adds support for the following pattern:

  struct some_data *data = bpf_ringbuf_reserve(&ringbuf, sizeof(struct some_data, 0));
  if (!data)
    return;
  bpf_map_lookup_elem(&another_map, &data->some_field);
  bpf_ringbuf_submit(data);

Currently the verifier does not consider bpf_ringbuf_reserve's PTR_TO_MEM | MEM_ALLOC ret type a valid key input to bpf_map_lookup_elem. Since PTR_TO_MEM is by definition a valid region of memory, it is safe to use it as a key for lookups.

---
v2->v3: lore.kernel.org/bpf/[email protected]

* Add Yonghong ack, rebase

v1->v2: lore.kernel.org/bpf/[email protected]

* Move test changes into separate patch - patch 2 in this series. (Kumar, Yonghong). That patch's changelog enumerates specific changes from v1
* Remove PTR_TO_MEM addition from this patch - patch 1 (Yonghong)
* I don't have a usecase for PTR_TO_MEM w/o MEM_ALLOC
* Add "if (!data)" error check to example pattern in this patch (Yonghong)
* Remove patch 2 from v1's series, which removed map_key_value_types as it was more-or-less duplicate of mem_types
* Now that PTR_TO_MEM isn't added here, more differences between map_key_value_types and mem_types, and no usecase for PTR_TO_BUF, so drop for now.

 kernel/bpf/verifier.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 15 lines

kernel/bpf/verifier.c static const struct bpf_reg_types map_key_value_types = { · line 5644

Quoted 9 lines
kernel/bpf/verifier.c +1 -0
index 6f6d2d511c06..97351ae3e7a7 100644
@@ -5641,6 +5641,7 @@ static const struct bpf_reg_types map_key_value_types = {
                PTR_TO_PACKET_META,
                PTR_TO_MAP_KEY,
                PTR_TO_MAP_VALUE,
+               PTR_TO_MEM | MEM_ALLOC,

are there any differences between mem_types and map_key_value_types? If not, should we just drop map_key_value_types? mem_types also alloc any PTR_TO_MEM (not just ringbuf's MEM_ALLOC) and PTR_TO_BUF (tracepoint context structs, I think?)

This was discussed here:
https://lore.kernel.org/bpf/CAP01T76YeOQLfYBX+63Z+cbF+xZUH-4FYG3MyNTiKtP8fLPGtw@mail.gmail.com

I guess we can do it, since it may already be triggered using PTR_TO_MAP_VALUE.

Based on my reading that day, it looked as if passing with offset != 0 would fail in all other cases, but I might be missing some other corner case. I later realised that memcpy does fallback to memmove when it detects overlap, so it wouldn't lead to any corruption, just a warning at runtime.

Quoted 6 lines
Quoted 6 lines
kernel/bpf/verifier.c +0 -1
        },
 };
 
--

2.30.2

#5 lore raw
Quoted 60 lines · Kumar Kartikeya Dwivedi
Quoted 41 lines · Andrii Nakryiko
Quoted 39 lines · Dave Marchevsky

This patch adds support for the following pattern:

  struct some_data *data = bpf_ringbuf_reserve(&ringbuf, sizeof(struct some_data, 0));
  if (!data)
    return;
  bpf_map_lookup_elem(&another_map, &data->some_field);
  bpf_ringbuf_submit(data);

Currently the verifier does not consider bpf_ringbuf_reserve's PTR_TO_MEM | MEM_ALLOC ret type a valid key input to bpf_map_lookup_elem. Since PTR_TO_MEM is by definition a valid region of memory, it is safe to use it as a key for lookups.

---
v2->v3: lore.kernel.org/bpf/[email protected]

* Add Yonghong ack, rebase

v1->v2: lore.kernel.org/bpf/[email protected]

* Move test changes into separate patch - patch 2 in this series. (Kumar, Yonghong). That patch's changelog enumerates specific changes from v1
* Remove PTR_TO_MEM addition from this patch - patch 1 (Yonghong)
* I don't have a usecase for PTR_TO_MEM w/o MEM_ALLOC
* Add "if (!data)" error check to example pattern in this patch (Yonghong)
* Remove patch 2 from v1's series, which removed map_key_value_types as it was more-or-less duplicate of mem_types
* Now that PTR_TO_MEM isn't added here, more differences between map_key_value_types and mem_types, and no usecase for PTR_TO_BUF, so drop for now.

 kernel/bpf/verifier.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 15 lines

kernel/bpf/verifier.c static const struct bpf_reg_types map_key_value_types = { · line 5644

Quoted 9 lines
kernel/bpf/verifier.c +1 -0
index 6f6d2d511c06..97351ae3e7a7 100644
@@ -5641,6 +5641,7 @@ static const struct bpf_reg_types map_key_value_types = {
                PTR_TO_PACKET_META,
                PTR_TO_MAP_KEY,
                PTR_TO_MAP_VALUE,
+               PTR_TO_MEM | MEM_ALLOC,

are there any differences between mem_types and map_key_value_types? If not, should we just drop map_key_value_types? mem_types also alloc any PTR_TO_MEM (not just ringbuf's MEM_ALLOC) and PTR_TO_BUF (tracepoint context structs, I think?)

Quoted 2 lines

This was discussed here:
https://lore.kernel.org/bpf/CAP01T76YeOQLfYBX+63Z+cbF+xZUH-4FYG3MyNTiKtP8fLPGtw@mail.gmail.com

My bad, I skipped previous revisions and didn't see this suggestion.

Quoted 15 lines

I guess we can do it, since it may already be triggered using PTR_TO_MAP_VALUE.

Based on my reading that day, it looked as if passing with offset != 0 would fail in all other cases, but I might be missing some other corner case. I later realised that memcpy does fallback to memmove when it detects overlap, so it wouldn't lead to any corruption, just a warning at runtime.

Quoted 6 lines
Quoted 6 lines
kernel/bpf/verifier.c +0 -1
        },
 };
 
--

2.30.2