tools/testing/selftests/bpf/prog_tests/user_ringbuf.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/user_ringbuf.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/user_ringbuf.c- Extension
.c- Size
- 18588 bytes
- Lines
- 702
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/compiler.hlinux/ring_buffer.hlinux/build_bug.hpthread.hstdio.hstdlib.hsys/mman.hsys/syscall.hsys/sysinfo.htest_progs.huapi/linux/bpf.hunistd.huser_ringbuf_fail.skel.huser_ringbuf_success.skel.h../progs/test_user_ringbuf.h
Detected Declarations
function drain_current_samplesfunction write_samplesfunction test_user_ringbuf_mappingsfunction load_skel_create_ringbufsfunction load_skel_create_user_ringbuffunction manually_write_test_invalid_samplefunction test_user_ringbuf_post_misalignedfunction test_user_ringbuf_post_producer_wrong_offsetfunction test_user_ringbuf_post_larger_than_ringbuf_szfunction test_user_ringbuf_basicfunction test_user_ringbuf_sample_full_ring_bufferfunction test_user_ringbuf_post_alignment_autoadjustfunction test_user_ringbuf_overfillfunction test_user_ringbuf_discards_properly_ignoredfunction test_user_ringbuf_loopfunction send_test_messagefunction kick_kernel_read_messagesfunction handle_kernel_msgfunction drain_kernel_messages_bufferfunction test_user_ringbuf_msg_protocolfunction spawn_kick_thread_for_pollfunction test_user_ringbuf_blocking_reservefunction test_user_ringbuf
Annotated Snippet
if (!entry) {
err = -errno;
goto done;
}
entry->pid = getpid();
entry->seq = i;
entry->value = i * i;
read = snprintf(entry->comm, sizeof(entry->comm), "%u", i);
if (read <= 0) {
/* Assert on the error path to avoid spamming logs with
* mostly success messages.
*/
ASSERT_GT(read, 0, "snprintf_comm");
err = read;
user_ring_buffer__discard(ringbuf, entry);
goto done;
}
user_ring_buffer__submit(ringbuf, entry);
}
done:
drain_current_samples();
return err;
}
static struct user_ringbuf_success *open_load_ringbuf_skel(void)
{
struct user_ringbuf_success *skel;
int err;
skel = user_ringbuf_success__open();
if (!ASSERT_OK_PTR(skel, "skel_open"))
return NULL;
err = bpf_map__set_max_entries(skel->maps.user_ringbuf, c_ringbuf_size);
if (!ASSERT_OK(err, "set_max_entries"))
goto cleanup;
err = bpf_map__set_max_entries(skel->maps.kernel_ringbuf, c_ringbuf_size);
if (!ASSERT_OK(err, "set_max_entries"))
goto cleanup;
err = user_ringbuf_success__load(skel);
if (!ASSERT_OK(err, "skel_load"))
goto cleanup;
return skel;
cleanup:
user_ringbuf_success__destroy(skel);
return NULL;
}
static void test_user_ringbuf_mappings(void)
{
int err, rb_fd;
int page_size = getpagesize();
void *mmap_ptr;
struct user_ringbuf_success *skel;
skel = open_load_ringbuf_skel();
if (!skel)
return;
rb_fd = bpf_map__fd(skel->maps.user_ringbuf);
/* cons_pos can be mapped R/O, can't add +X with mprotect. */
mmap_ptr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, rb_fd, 0);
ASSERT_OK_PTR(mmap_ptr, "ro_cons_pos");
ASSERT_ERR(mprotect(mmap_ptr, page_size, PROT_WRITE), "write_cons_pos_protect");
ASSERT_ERR(mprotect(mmap_ptr, page_size, PROT_EXEC), "exec_cons_pos_protect");
ASSERT_ERR_PTR(mremap(mmap_ptr, 0, 4 * page_size, MREMAP_MAYMOVE), "wr_prod_pos");
err = -errno;
ASSERT_ERR(err, "wr_prod_pos_err");
ASSERT_OK(munmap(mmap_ptr, page_size), "unmap_ro_cons");
/* prod_pos can be mapped RW, can't add +X with mprotect. */
mmap_ptr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED,
rb_fd, page_size);
ASSERT_OK_PTR(mmap_ptr, "rw_prod_pos");
ASSERT_ERR(mprotect(mmap_ptr, page_size, PROT_EXEC), "exec_prod_pos_protect");
err = -errno;
ASSERT_ERR(err, "wr_prod_pos_err");
ASSERT_OK(munmap(mmap_ptr, page_size), "unmap_rw_prod");
/* data pages can be mapped RW, can't add +X with mprotect. */
mmap_ptr = mmap(NULL, page_size, PROT_WRITE, MAP_SHARED, rb_fd,
Annotation
- Immediate include surface: `linux/compiler.h`, `linux/ring_buffer.h`, `linux/build_bug.h`, `pthread.h`, `stdio.h`, `stdlib.h`, `sys/mman.h`, `sys/syscall.h`.
- Detected declarations: `function drain_current_samples`, `function write_samples`, `function test_user_ringbuf_mappings`, `function load_skel_create_ringbufs`, `function load_skel_create_user_ringbuf`, `function manually_write_test_invalid_sample`, `function test_user_ringbuf_post_misaligned`, `function test_user_ringbuf_post_producer_wrong_offset`, `function test_user_ringbuf_post_larger_than_ringbuf_sz`, `function test_user_ringbuf_basic`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.