Skip to contentSkip to conversation

bpf: Allow ringbuf memory to be used as map key

PATCH Superseded Dave Marchevsky 3 participants 7 messages Sep 12, 2022 – Sep 14, 2022

Subsystems (4)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 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));
  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 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.

---
 kernel/bpf/verifier.c                         |  2 +
 tools/testing/selftests/bpf/Makefile          |  8 ++-
 .../selftests/bpf/prog_tests/ringbuf.c        | 10 +++
 .../bpf/progs/test_ringbuf_map_key.c          | 69 +++++++++++++++++++
 4 files changed, 86 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c
kernel/bpf/verifier.c +2 -0
index c259d734f863..d093618aed99 100644
@@ -5626,6 +5626,8 @@ 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,
+		PTR_TO_MEM | MEM_ALLOC,
 	},
 };
 
tools/testing/selftests/bpf/Makefile +5 -3
index 6cd327f1f216..231d9c1364c9 100644
@@ -351,9 +351,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 +10 -0
index 9a80fe8a6427..1cf458d1a179 100644
@@ -13,6 +13,7 @@
 #include <linux/perf_event.h>
 #include <linux/ring_buffer.h>
 #include "test_ringbuf.lskel.h"
+#include "test_ringbuf_map_key.lskel.h"
 
 #define EDONE 7777
 
@@ -297,3 +298,12 @@ void test_ringbuf(void)
 	ring_buffer__free(ringbuf);
 	test_ringbuf_lskel__destroy(skel);
 }
+
+void test_ringbuf_map_key(void)
+{
+	struct test_ringbuf_map_key_lskel *skel_map_key;
+
+	skel_map_key = test_ringbuf_map_key_lskel__open_and_load();
+	ASSERT_OK_PTR(skel_map_key, "test_ringbuf_map_key_lskel__open_and_load failed");
+	test_ringbuf_map_key_lskel__destroy(skel_map_key);
+}
tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c new file +69 -0

69 lines not on this page · raw message

Signature · 1 line
2.30.2
#1 lore raw

PATCH 2/2 bpf: Consider all mem_types compatible for map_{key,value} args

After the previous patch, which added PTR_TO_MEM types to map_key_value_types, the only difference between map_key_value_types and mem_types sets is PTR_TO_BUF, which is in the latter set but not the former.

Helpers which expect ARG_PTR_TO_MAP_KEY or ARG_PTR_TO_MAP_VALUE already effectively expect a valid blob of arbitrary memory that isn't necessarily explicitly associated with a map. When validating a PTR_TO_MAP_{KEY,VALUE} arg, the verifier expects meta->map_ptr to have already been set, either by an earlier ARG_CONST_MAP_PTR arg, or custom logic like that in process_timer_func or process_kptr_func.

So let's get rid of map_key_value_types and just use mem_types for those args.

This has the effect of adding PTR_TO_BUF to the set of compatible types for ARG_PTR_TO_MAP_KEY and ARG_PTR_TO_MAP_VALUE.

---
 kernel/bpf/verifier.c | 16 ++--------------
 1 file changed, 2 insertions(+), 14 deletions(-)
kernel/bpf/verifier.c +2 -14
index d093618aed99..ae2259d782bb 100644
@@ -5619,18 +5619,6 @@ struct bpf_reg_types {
 	u32 *btf_id;
 };
 
-static const struct bpf_reg_types map_key_value_types = {
-	.types = {
-		PTR_TO_STACK,
-		PTR_TO_PACKET,
-		PTR_TO_PACKET_META,
-		PTR_TO_MAP_KEY,
-		PTR_TO_MAP_VALUE,
-		PTR_TO_MEM,
-		PTR_TO_MEM | MEM_ALLOC,
-	},
-};
-
 static const struct bpf_reg_types sock_types = {
 	.types = {
 		PTR_TO_SOCK_COMMON,
@@ -5691,8 +5679,8 @@ static const struct bpf_reg_types timer_types = { .types = { PTR_TO_MAP_VALUE }
 static const struct bpf_reg_types kptr_types = { .types = { PTR_TO_MAP_VALUE } };
 
 static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = {
-	[ARG_PTR_TO_MAP_KEY]		= &map_key_value_types,
-	[ARG_PTR_TO_MAP_VALUE]		= &map_key_value_types,
+	[ARG_PTR_TO_MAP_KEY]		= &mem_types,
+	[ARG_PTR_TO_MAP_VALUE]		= &mem_types,
 	[ARG_CONST_SIZE]		= &scalar_types,
 	[ARG_CONST_SIZE_OR_ZERO]	= &scalar_types,
 	[ARG_CONST_ALLOC_SIZE_OR_ZERO]	= &scalar_types,
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH bpf-next 2/2] bpf: Consider all mem_types compatible for map_{key,value} args

Quoted 13 lines · Dave Marchevsky

After the previous patch, which added PTR_TO_MEM types to map_key_value_types, the only difference between map_key_value_types and mem_types sets is PTR_TO_BUF, which is in the latter set but not the former.

Helpers which expect ARG_PTR_TO_MAP_KEY or ARG_PTR_TO_MAP_VALUE already effectively expect a valid blob of arbitrary memory that isn't necessarily explicitly associated with a map. When validating a PTR_TO_MAP_{KEY,VALUE} arg, the verifier expects meta->map_ptr to have already been set, either by an earlier ARG_CONST_MAP_PTR arg, or custom logic like that in process_timer_func or process_kptr_func.

Quoted 8 lines

So let's get rid of map_key_value_types and just use mem_types for those args.

This has the effect of adding PTR_TO_BUF to the set of compatible types for ARG_PTR_TO_MAP_KEY and ARG_PTR_TO_MAP_VALUE.

---

I'm wondering how it is safe when PTR_TO_BUF aliases map_value we are updating.

User can now do e.g. in array map:
map_iter(ctx)
bpf_map_update_elem(map, ctx->key, ctx->value, 0);

Technically such overlapping memcpy is UB.

Looks like for this particular case, overlap will always be exact. Such aliasing pointers always have fixed size memory.
If offset was added for partial overlap, it would not satisfy value_size requirement and users won't be able to pass the pointer. dynptr_from_mem may be a problem, but even there you need to obtain a data slice of at least value_size, if an offset is added slice will always be < value_size.

So it seems we only need to care about dst == src, and should be using memmove when dst == src?

PS: Also it would be better to split verifier and selftest changes in patch 1 into separate patches.

Quoted 40 lines
 kernel/bpf/verifier.c | 16 ++--------------
 1 file changed, 2 insertions(+), 14 deletions(-)
kernel/bpf/verifier.c +2 -15
index d093618aed99..ae2259d782bb 100644
@@ -5619,18 +5619,6 @@ struct bpf_reg_types {
        u32 *btf_id;
 };
 
-static const struct bpf_reg_types map_key_value_types = {
-       .types = {
-               PTR_TO_STACK,
-               PTR_TO_PACKET,
-               PTR_TO_PACKET_META,
-               PTR_TO_MAP_KEY,
-               PTR_TO_MAP_VALUE,
-               PTR_TO_MEM,
-               PTR_TO_MEM | MEM_ALLOC,
-       },
-};
-
 static const struct bpf_reg_types sock_types = {
        .types = {
                PTR_TO_SOCK_COMMON,
@@ -5691,8 +5679,8 @@ static const struct bpf_reg_types timer_types = { .types = { PTR_TO_MAP_VALUE }
 static const struct bpf_reg_types kptr_types = { .types = { PTR_TO_MAP_VALUE } };
 
 static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = {
-       [ARG_PTR_TO_MAP_KEY]            = &map_key_value_types,
-       [ARG_PTR_TO_MAP_VALUE]          = &map_key_value_types,
+       [ARG_PTR_TO_MAP_KEY]            = &mem_types,
+       [ARG_PTR_TO_MAP_VALUE]          = &mem_types,
        [ARG_CONST_SIZE]                = &scalar_types,
        [ARG_CONST_SIZE_OR_ZERO]        = &scalar_types,
        [ARG_CONST_ALLOC_SIZE_OR_ZERO]  = &scalar_types,
--

2.30.2

#3 lore raw

Re: [PATCH bpf-next 2/2] bpf: Consider all mem_types compatible for map_{key,value} args

Quoted 24 lines · Kumar Kartikeya Dwivedi
Quoted 13 lines · Dave Marchevsky

After the previous patch, which added PTR_TO_MEM types to map_key_value_types, the only difference between map_key_value_types and mem_types sets is PTR_TO_BUF, which is in the latter set but not the former.

Helpers which expect ARG_PTR_TO_MAP_KEY or ARG_PTR_TO_MAP_VALUE already effectively expect a valid blob of arbitrary memory that isn't necessarily explicitly associated with a map. When validating a PTR_TO_MAP_{KEY,VALUE} arg, the verifier expects meta->map_ptr to have already been set, either by an earlier ARG_CONST_MAP_PTR arg, or custom logic like that in process_timer_func or process_kptr_func.

Quoted 8 lines

So let's get rid of map_key_value_types and just use mem_types for those args.

This has the effect of adding PTR_TO_BUF to the set of compatible types for ARG_PTR_TO_MAP_KEY and ARG_PTR_TO_MAP_VALUE.

---

Quoted 7 lines

I'm wondering how it is safe when PTR_TO_BUF aliases map_value we are updating.

User can now do e.g. in array map:
map_iter(ctx)
bpf_map_update_elem(map, ctx->key, ctx->value, 0);

Technically such overlapping memcpy is UB.

Hit send too early.
To be fair they can already do this using PTR_TO_MAP_VALUE, so it's not a new problem.

Quoted 14 lines

Looks like for this particular case, overlap will always be exact. Such aliasing pointers always have fixed size memory.
If offset was added for partial overlap, it would not satisfy value_size requirement and users won't be able to pass the pointer. dynptr_from_mem may be a problem, but even there you need to obtain a data slice of at least value_size, if an offset is added slice will always be < value_size.

So it seems we only need to care about dst == src, and should be using memmove when dst == src?

PS: Also it would be better to split verifier and selftest changes in patch 1 into separate patches.

#4 lore raw

Re: [PATCH bpf-next 2/2] bpf: Consider all mem_types compatible for map_{key,value} args

Quoted 31 lines · Kumar Kartikeya Dwivedi
Quoted 30 lines · Kumar Kartikeya Dwivedi
Quoted 13 lines · Dave Marchevsky

After the previous patch, which added PTR_TO_MEM types to map_key_value_types, the only difference between map_key_value_types and mem_types sets is PTR_TO_BUF, which is in the latter set but not the former.

Helpers which expect ARG_PTR_TO_MAP_KEY or ARG_PTR_TO_MAP_VALUE already effectively expect a valid blob of arbitrary memory that isn't necessarily explicitly associated with a map. When validating a PTR_TO_MAP_{KEY,VALUE} arg, the verifier expects meta->map_ptr to have already been set, either by an earlier ARG_CONST_MAP_PTR arg, or custom logic like that in process_timer_func or process_kptr_func.

Quoted 8 lines

So let's get rid of map_key_value_types and just use mem_types for those args.

This has the effect of adding PTR_TO_BUF to the set of compatible types for ARG_PTR_TO_MAP_KEY and ARG_PTR_TO_MAP_VALUE.

---

I'm wondering how it is safe when PTR_TO_BUF aliases map_value we are updating.

User can now do e.g. in array map:
map_iter(ctx)
bpf_map_update_elem(map, ctx->key, ctx->value, 0);

Quoted 6 lines
Quoted 1 line

Technically such overlapping memcpy is UB.

Hit send too early.
To be fair they can already do this using PTR_TO_MAP_VALUE, so it's not a new problem.

Yeah, I see what you mean and agree that it's a bug. But as you concluded it's a preexisting issue, so I didn't attempt to address it in the v2 series I sent out today.

I agree with all other comments and addressed them.

Quoted 14 lines
Quoted 14 lines

Looks like for this particular case, overlap will always be exact. Such aliasing pointers always have fixed size memory.
If offset was added for partial overlap, it would not satisfy value_size requirement and users won't be able to pass the pointer. dynptr_from_mem may be a problem, but even there you need to obtain a data slice of at least value_size, if an offset is added slice will always be < value_size.

So it seems we only need to care about dst == src, and should be using memmove when dst == src?

PS: Also it would be better to split verifier and selftest changes in patch 1 into separate patches.

#5 lore raw

Re: [PATCH bpf-next 2/2] bpf: Consider all mem_types compatible for map_{key,value} args

In reply to Dave Marchevsky
Quoted 4 lines · Dave Marchevsky

After the previous patch, which added PTR_TO_MEM types to map_key_value_types, the only difference between map_key_value_types and mem_types sets is PTR_TO_BUF, which is in the latter set but not the former.

Add a selftest with PTR_TO_BUF as the map key/value?

Quoted 53 lines

Helpers which expect ARG_PTR_TO_MAP_KEY or ARG_PTR_TO_MAP_VALUE already effectively expect a valid blob of arbitrary memory that isn't necessarily explicitly associated with a map. When validating a PTR_TO_MAP_{KEY,VALUE} arg, the verifier expects meta->map_ptr to have already been set, either by an earlier ARG_CONST_MAP_PTR arg, or custom logic like that in process_timer_func or process_kptr_func.

So let's get rid of map_key_value_types and just use mem_types for those args.

This has the effect of adding PTR_TO_BUF to the set of compatible types for ARG_PTR_TO_MAP_KEY and ARG_PTR_TO_MAP_VALUE.

---
  kernel/bpf/verifier.c | 16 ++--------------
  1 file changed, 2 insertions(+), 14 deletions(-)
kernel/bpf/verifier.c +2 -14
index d093618aed99..ae2259d782bb 100644
@@ -5619,18 +5619,6 @@ struct bpf_reg_types {
  	u32 *btf_id;
  };
 
-static const struct bpf_reg_types map_key_value_types = {
-	.types = {
-		PTR_TO_STACK,
-		PTR_TO_PACKET,
-		PTR_TO_PACKET_META,
-		PTR_TO_MAP_KEY,
-		PTR_TO_MAP_VALUE,
-		PTR_TO_MEM,
-		PTR_TO_MEM | MEM_ALLOC,
-	},
-};
-
  static const struct bpf_reg_types sock_types = {
  	.types = {
  		PTR_TO_SOCK_COMMON,
@@ -5691,8 +5679,8 @@ static const struct bpf_reg_types timer_types = { .types = { PTR_TO_MAP_VALUE }
  static const struct bpf_reg_types kptr_types = { .types = { PTR_TO_MAP_VALUE } };
 
  static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = {
-	[ARG_PTR_TO_MAP_KEY]		= &map_key_value_types,
-	[ARG_PTR_TO_MAP_VALUE]		= &map_key_value_types,
+	[ARG_PTR_TO_MAP_KEY]		= &mem_types,
+	[ARG_PTR_TO_MAP_VALUE]		= &mem_types,
  	[ARG_CONST_SIZE]		= &scalar_types,
  	[ARG_CONST_SIZE_OR_ZERO]	= &scalar_types,
  	[ARG_CONST_ALLOC_SIZE_OR_ZERO]	= &scalar_types,
#6 lore raw
In reply to Dave Marchevsky
Quoted 3 lines · Dave Marchevsky

This patch adds support for the following pattern:

   struct some_data *data = bpf_ringbuf_reserve(&ringbuf, sizeof(struct some_data, 0));

Maybe add:

	if (!data)
		return;

in the example code? Otherwise, the code looks invalid.

Quoted 7 lines
   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 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.

bpf_ringbuf_reserve return types also has MEM_ALLOC. Maybe should mention in the above commit message.

Quoted 15 lines
---
  kernel/bpf/verifier.c                         |  2 +
  tools/testing/selftests/bpf/Makefile          |  8 ++-
  .../selftests/bpf/prog_tests/ringbuf.c        | 10 +++
  .../bpf/progs/test_ringbuf_map_key.c          | 69 +++++++++++++++++++
  4 files changed, 86 insertions(+), 3 deletions(-)

create mode 100644 tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c

kernel/bpf/verifier.c +0 -0
index c259d734f863..d093618aed99 100644

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

Quoted 4 lines
kernel/bpf/verifier.c +1 -0
@@ -5626,6 +5626,8 @@ 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,

PTR_TO_MEM is okay. But bpf_ringbuf_reserve() will trigger PTR_TO_MEM | MEM_ALLOC. So suggest to add PTR_TO_MEM unless you find a use case for it.

Quoted 9 lines
kernel/bpf/verifier.c +1 -0
+		PTR_TO_MEM | MEM_ALLOC,
  	},
  };
 
tools/testing/selftests/bpf/Makefile +0 -0
index 6cd327f1f216..231d9c1364c9 100644

tools/testing/selftests/bpf/Makefile LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ · line 357

Quoted 10 lines
tools/testing/selftests/bpf/Makefile +4 -3
@@ -351,9 +351,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

Maybe put the selftest in a separate patch?

Quoted 19 lines
tools/testing/selftests/bpf/Makefile +1 -0
+
  # 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 +1 -0
index 9a80fe8a6427..1cf458d1a179 100644
@@ -13,6 +13,7 @@
  #include <linux/perf_event.h>
  #include <linux/ring_buffer.h>
  #include "test_ringbuf.lskel.h"
+#include "test_ringbuf_map_key.lskel.h"
 
  #define EDONE 7777
 
@@ -297,3 +298,12 @@ void test_ringbuf(void)
  	ring_buffer__free(ringbuf);
  	test_ringbuf_lskel__destroy(skel);

tools/testing/selftests/bpf/prog_tests/ringbuf.c void test_ringbuf(void) · line 309

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/ringbuf.c +9 -0
  }
+
+void test_ringbuf_map_key(void)
+{
+	struct test_ringbuf_map_key_lskel *skel_map_key;
+
+	skel_map_key = test_ringbuf_map_key_lskel__open_and_load();
+	ASSERT_OK_PTR(skel_map_key, "test_ringbuf_map_key_lskel__open_and_load failed");
+	test_ringbuf_map_key_lskel__destroy(skel_map_key);
+}

This adds another *top* test in ringbuf.c. Should we add test_ringbuf_map_key as a subtest for test_ringbuf test so in the future more subtests could be added in the umbrella of test_ringbuf test?

Quoted 28 lines
tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c new file +22 -0
new file mode 100644
index 000000000000..96a791a9762e
@@ -0,0 +1,69 @@
+// 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";
+
+struct sample {
+	int pid;
+	int seq;
+	long value;
+	char comm[16];
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_RINGBUF);
+	__uint(max_entries, 4096);
+} ringbuf SEC(".maps");
+
+struct {

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

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c new file +10 -0
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__uint(max_entries, 1000);
+	__type(key, struct sample);
+	__type(value, int);
+} hash_map SEC(".maps");
+
+/* inputs */
+int pid = 0;
+long value = 0;
+long flags = 0;

I didn't see these values are assigned in user space. So you can simplify the code by remove 'flags' and manually inline the 'value'.

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

Quoted 4 lines
tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c new file +4 -0
+
+/* outputs */
+long total = 0;
+long dropped = 0;

'total' is not used. 'dropped' is not checked by user space application, so it can be dropped too.

Quoted 20 lines
tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c new file +20 -0
+
+/* inner state */
+long seq = 0;
+
+SEC("fentry/" SYS_PREFIX "sys_getpgid")
+int test_ringbuf_mem_map_key(void *ctx)
+{
+	int cur_pid = bpf_get_current_pid_tgid() >> 32;
+	struct sample *sample;
+	int *lookup_val;
+	int zero = 0;
+
+	if (cur_pid != pid)
+		return 0;
+
+	sample = bpf_ringbuf_reserve(&ringbuf, sizeof(*sample), 0);
+	if (!sample) {
+		__sync_fetch_and_add(&dropped, 1);
+		return 0;
+	}

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

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c new file +10 -0
+
+	sample->pid = pid;
+	bpf_get_current_comm(sample->comm, sizeof(sample->comm));
+	sample->value = value;
+	sample->seq = seq++;
+
+	/* This prog is never run, successful load w/ below use of sample mem
+	 * as map key is considered success
+	 */
+	lookup_val = (int *)bpf_map_lookup_elem(&hash_map, sample);

Maybe add code for bpf_map_update_elem() as well since that is also common use case?
This way, you can check the result in user space after bpf_map_update_elem().

Quoted 3 lines
tools/testing/selftests/bpf/progs/test_ringbuf_map_key.c new file +3 -0
+	bpf_ringbuf_submit(sample, 0);
+	return 0;
+}