This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: ebpf-ringbuf-backpressure-invariant

Kernel eBPF RingBuffer Allocation and Consumer Backpressure Bounding

Enforces strict memory isolation, zero-allocation safety, and non-blocking backpressure contracts across kernel-to-userspace eBPF RingBuffer pipelines. Prohibits silent telemetry drops by mandating atomic per-CPU counter accounting on buffer starvation and enforces adaptive userspace wakeup thresholds to prevent CPU exhaustion.

Prohibited Anti-Pattern: Unchecked RingBuffer Reservations and Silent Drop Telemetry Starvation Under Consumer Lag

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Kernel eBPF RingBuffer Allocation and Consumer Backpressure Bounding - Enforces strict memory isolation, zero-allocation safety, and non-blocking backpressure contracts across kernel-to-userspace eBPF RingBuffer pipelines. Prohibits silent telemetry drops by mandating atomic per-CPU counter accounting on buffer starvation and enforces adaptive userspace wakeup thresholds to prevent CPU exhaustion.
globs: **/*.bpf.c, **/*.bpf.h, src/bpf/**/*.c, pkg/ebpf/**/*.go, src/ebpf/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/ebpf-ringbuf-backpressure-invariant"
---

# Kernel eBPF RingBuffer Allocation and Consumer Backpressure Bounding

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Unchecked RingBuffer Reservations and Silent Drop Telemetry Starvation Under Consumer Lag

## System Constraints
You are a Principal Linux Kernel & Systems Architect enforcing zero-leak, bounded-memory eBPF telemetry pipelines. All kernel-side eBPF code interacting with BPF_MAP_TYPE_RINGBUF must strictly check allocation invariants, manage userspace wakeup pressure adaptively, and ensure observable drop telemetry via per-CPU counters without blocking hot kernel probe paths.

## Strict Guidelines
- Engineers MUST ALWAYS verify the pointer returned by `bpf_ringbuf_reserve` against NULL before dereferencing or writing event data.
- Engineers MUST ALWAYS record telemetry drops using a dedicated `BPF_MAP_TYPE_PERCPU_ARRAY` atomic counter when `bpf_ringbuf_reserve` or `bpf_ringbuf_output` yields NULL or -ENOSPC.
- Engineers MUST NOT invoke `bpf_ringbuf_submit` with uninitialized or partially written memory structures; zero or fully populate structs prior to submission.
- Kernel probes MUST NOT use `BPF_RB_FORCE_WAKEUP` on high-throughput event paths; engineers MUST employ `BPF_RB_NO_WAKEUP` or evaluate watermark batching thresholds to avert kernel-to-userspace context switch storms.
- Userspace consumers MUST configure `ring_buffer__poll` with explicit bounded timeout budgets and drain loops to prevent upstream buffer fill states.
- Components NEVER block, spin-wait, or attempt synchronous retries inside probe contexts when a ring-buffer reservation fails.

## Bad vs Good Implementations
### Unsafe RingBuffer Reservation vs. Resilient Non-Blocking Backpressure Pattern

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Dereferencing the reservation pointer without checking for NULL triggers kernel verifier rejections or kernel null-pointer panics on older kernels. Forcing wakeups on every event creates kernel scheduler thrashing, while silent drops during consumer starvation leave operators unaware of missing critical telemetry.
```c
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>

struct event {
    __u32 pid;
    char comm[16];
};

struct {
    __uint(type, BPF_MAP_TYPE_RINGBUF);
    __uint(max_entries, 256 * 1024);
} events SEC(".maps");

SEC("tp/sched/sched_process_exec")
int handle_exec(void *ctx) {
    struct event *e;

    // ANTI-PATTERN: No NULL check, silent telemetry drop, unthrottled wakeup storm
    e = bpf_ringbuf_reserve(&events, sizeof(*e), 0);
    e->pid = bpf_get_current_pid_tgid() >> 32;
    bpf_get_current_comm(&e->comm, sizeof(e->comm));
    bpf_ringbuf_submit(e, BPF_RB_FORCE_WAKEUP);

    return 0;
}
```

#### Production Standard (Correct)
> **Standard**: Validates the allocation pointer to satisfy verifier constraints and isolate execution. Records dropped events into a per-CPU array to provide immediate observability into consumer lag, and utilizes BPF_RB_NO_WAKEUP to let the userspace epoll/poll loop consume events in high-throughput batches without context switch overhead.
```c
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>

struct event {
    __u32 pid;
    char comm[16];
};

struct {
    __uint(type, BPF_MAP_TYPE_RINGBUF);
    __uint(max_entries, 256 * 1024);
} events SEC(".maps");

struct {
    __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
    __uint(max_entries, 1);
    __type(key, __u32);
    __type(value, __u64);
} dropped_events SEC(".maps");

SEC("tp/sched/sched_process_exec")
int handle_exec(void *ctx) {
    struct event *e;
    __u32 drop_key = 0;

    // Bound backpressure: reserve memory locklessly without blocking
    e = bpf_ringbuf_reserve(&events, sizeof(*e), 0);
    if (!e) {
        // Invariant: Account for every backpressure drop atomically per-CPU
        __u64 *drops = bpf_map_lookup_elem(&dropped_events, &drop_key);
        if (drops) {
            (*drops)++;
        }
        return 0; // Drop silently avoided; drop metrics preserved
    }

    e->pid = bpf_get_current_pid_tgid() >> 32;
    bpf_get_current_comm(&e->comm, sizeof(e->comm));

    // Adaptive userspace notification: suppress forced context switches
    bpf_ringbuf_submit(e, BPF_RB_NO_WAKEUP);
    return 0;
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Kernel eBPF RingBuffer Allocation and Consumer Backpressure Bounding

Kernel eBPF RingBuffer Allocation and Consumer Backpressure Bounding defines an industrial-grade behavioral standard for devops engineering. Enforces strict memory isolation, zero-allocation safety, and non-blocking backpressure contracts across kernel-to-userspace eBPF RingBuffer pipelines. Prohibits silent telemetry drops by mandating atomic per-CPU counter accounting on buffer starvation and enforces adaptive userspace wakeup thresholds to prevent CPU exhaustion. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Unchecked RingBuffer Reservations and Silent Drop Telemetry Starvation Under Consumer Lag Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftUnchecked RingBuffer Reservations and Silent Drop Telemetry Starvation Under Consumer LagCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS verify the pointer returned by `bpf_ringbuf_reserve` against NULL before dereferencing or writing event data.

Engineers MUST ALWAYS verify the pointer returned by `bpf_ringbuf_reserve` against NULL before dereferencing or writing event data.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST ALWAYS record telemetry drops using a dedicated `BPF_MAP_TYPE_PERCPU_ARRAY` atomic counter when `bpf_ringbuf_reserve` or `bpf_ringbuf_output` yields NULL or -ENOSPC.

Engineers MUST ALWAYS record telemetry drops using a dedicated `BPF_MAP_TYPE_PERCPU_ARRAY` atomic counter when `bpf_ringbuf_reserve` or `bpf_ringbuf_output` yields NULL or -ENOSPC.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Engineers MUST NOT invoke `bpf_ringbuf_submit` with uninitialized or partially written memory structures

Engineers MUST NOT invoke `bpf_ringbuf_submit` with uninitialized or partially written memory structures; zero or fully populate structs prior to submission.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Kernel probes MUST NOT use `BPF_RB_FORCE_WAKEUP` on high-throughput event paths

Kernel probes MUST NOT use `BPF_RB_FORCE_WAKEUP` on high-throughput event paths; engineers MUST employ `BPF_RB_NO_WAKEUP` or evaluate watermark batching thresholds to avert kernel-to-userspace context switch storms.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Userspace consumers MUST configure `ring_buffer__poll` with explicit bounded timeout budgets and drain loops to prevent upstream buffer fill states.

Userspace consumers MUST configure `ring_buffer__poll` with explicit bounded timeout budgets and drain loops to prevent upstream buffer fill states.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
6

Components NEVER block, spin-wait, or attempt synchronous retries inside probe contexts when a ring-buffer reservation fails.

Components NEVER block, spin-wait, or attempt synchronous retries inside probe contexts when a ring-buffer reservation fails.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Kernel eBPF RingBuffer Allocation and Consumer Backpressure Bounding critical for AI coding assistants?

Enforces strict memory isolation, zero-allocation safety, and non-blocking backpressure contracts across kernel-to-userspace eBPF RingBuffer pipelines. Prohibits silent telemetry drops by mandating atomic per-CPU counter accounting on buffer starvation and enforces adaptive userspace wakeup thresholds to prevent CPU exhaustion. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Unchecked RingBuffer Reservations and Silent Drop Telemetry Starvation Under Consumer Lag) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

Kernel