tools/lib/bpf/ringbuf.c
Source file repositories/reference/linux-study-clean/tools/lib/bpf/ringbuf.c
File Facts
- System
- Linux kernel
- Corpus path
tools/lib/bpf/ringbuf.c- Extension
.c- Size
- 16320 bytes
- Lines
- 685
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
stdlib.hstdio.herrno.hunistd.hlinux/err.hlinux/bpf.hasm/barrier.hsys/mman.hsys/epoll.htime.hlibbpf.hlibbpf_internal.hbpf.h
Detected Declarations
struct ringstruct ring_bufferstruct user_ring_bufferstruct ringbuf_hdrfunction ringbuf_free_ringfunction ring_buffer__addfunction ring_buffer__freefunction ring_buffer__newfunction roundup_lenfunction ringbuf_process_ringfunction buffersfunction buffersfunction consumedfunction ring_buffer__epoll_fdfunction ring__consumer_posfunction ring__producer_posfunction ring__avail_data_sizefunction ring__sizefunction ring__map_fdfunction ring__consume_nfunction ring__consumefunction user_ringbuf_unmap_ringfunction user_ring_buffer__freefunction user_ringbuf_mapfunction user_ring_buffer__newfunction user_ringbuf_commitfunction user_ring_buffer__discardfunction user_ring_buffer__submitfunction ns_elapsed_timespec
Annotated Snippet
struct ring {
ring_buffer_sample_fn sample_cb;
void *ctx;
void *data;
unsigned long *consumer_pos;
unsigned long *producer_pos;
unsigned long mask;
int map_fd;
};
struct ring_buffer {
struct epoll_event *events;
struct ring **rings;
size_t page_size;
int epoll_fd;
int ring_cnt;
};
struct user_ring_buffer {
struct epoll_event event;
unsigned long *consumer_pos;
unsigned long *producer_pos;
void *data;
unsigned long mask;
size_t page_size;
int map_fd;
int epoll_fd;
};
/* 8-byte ring buffer header structure */
struct ringbuf_hdr {
__u32 len;
__u32 pad;
};
static void ringbuf_free_ring(struct ring_buffer *rb, struct ring *r)
{
if (r->consumer_pos) {
munmap(r->consumer_pos, rb->page_size);
r->consumer_pos = NULL;
}
if (r->producer_pos) {
munmap(r->producer_pos, rb->page_size + 2 * (r->mask + 1));
r->producer_pos = NULL;
}
free(r);
}
/* Add extra RINGBUF maps to this ring buffer manager */
int ring_buffer__add(struct ring_buffer *rb, int map_fd,
ring_buffer_sample_fn sample_cb, void *ctx)
{
struct bpf_map_info info;
__u32 len = sizeof(info);
struct epoll_event *e;
struct ring *r;
__u64 mmap_sz;
void *tmp;
int err;
memset(&info, 0, sizeof(info));
err = bpf_map_get_info_by_fd(map_fd, &info, &len);
if (err) {
err = -errno;
pr_warn("ringbuf: failed to get map info for fd=%d: %s\n",
map_fd, errstr(err));
return libbpf_err(err);
}
if (info.type != BPF_MAP_TYPE_RINGBUF) {
pr_warn("ringbuf: map fd=%d is not BPF_MAP_TYPE_RINGBUF\n",
map_fd);
return libbpf_err(-EINVAL);
}
tmp = libbpf_reallocarray(rb->rings, rb->ring_cnt + 1, sizeof(*rb->rings));
if (!tmp)
return libbpf_err(-ENOMEM);
rb->rings = tmp;
tmp = libbpf_reallocarray(rb->events, rb->ring_cnt + 1, sizeof(*rb->events));
if (!tmp)
return libbpf_err(-ENOMEM);
rb->events = tmp;
r = calloc(1, sizeof(*r));
if (!r)
return libbpf_err(-ENOMEM);
Annotation
- Immediate include surface: `stdlib.h`, `stdio.h`, `errno.h`, `unistd.h`, `linux/err.h`, `linux/bpf.h`, `asm/barrier.h`, `sys/mman.h`.
- Detected declarations: `struct ring`, `struct ring_buffer`, `struct user_ring_buffer`, `struct ringbuf_hdr`, `function ringbuf_free_ring`, `function ring_buffer__add`, `function ring_buffer__free`, `function ring_buffer__new`, `function roundup_len`, `function ringbuf_process_ring`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.