Skip to contentSkip to conversation

rust: alloc: add a ring buffer implementation

PATCH Applied Andreas Hindborg 2 participants 2 messages Dec 2

Subsystems (2)
lore
Share & tools
lore

Andreas Hindborg

author reviewer
#0 lore raw

PATCH v5 1/1 rust: alloc: add a ring buffer implementation

Add a fixed-capacity FIFO ring buffer. The implementation uses a circular buffer with head and tail pointers, providing constant-time push and pop operations.

The module includes a few tests covering basic operations, wrap-around behavior, interleaved push/pop sequences, and edge cases such as single-capacity buffers.

---
Changes in v5:
- Add the alloc: prefix to the patch subject (Danilo).
- Populate capacity + 1 slots in RingBuffer::new instead of nodes.capacity() slots, which is usize::MAX when Option<T> is zero-sized (Danilo).
- Guard the KUnit tests with CONFIG_RUST_RINGBUFFER_KUNIT_TEST (Danilo).
- Link to v4: https://msgid.link/[email protected]

Changes in v4:
- Add an // INVARIANT: comment to RingBuffer::new and populate the slots before constructing the buffer (Sashiko, Miguel).
- Add // INVARIANT: comments to push_head and pop_tail.
- Describe slots as occupied and vacant instead of full and empty.
- Add a module-level example (Miguel).
- Add examples to new, push_head and pop_tail that show both the success and the failure path (Miguel).
- Use intra-doc links for true and None (Miguel).
- Link to v3: https://msgid.link/[email protected]

Changes in v3:
- Lay out the imports vertically, as the coding guidelines require (Onur).
- Mark the trivial accessors #[inline] (Onur).
- Rebase on v7.4-rc1.
- Link to v2: https://msgid.link/[email protected]

Changes in v2:
- Move the module into rust/kernel/alloc/, next to Vec (Alice).
- Drop the redundant size field; use nodes.len() instead (Alice).
- Return ENOMEM when capacity + 1 overflows usize instead of panicking inside with_capacity (Alice).
- Simplify the head < tail branch of free_count to tail - head (Alice).
- Remove the manual Drop impl; the backing KVec already drops the stored slots (Alice).
- Make RingBuffer generic over the backing allocator. Add KRingBuffer, VRingBuffer and KVRingBuffer aliases and re-export them from kernel::alloc. The constructor now takes an additional Flags argument (Daniel, Danilo).
- Link to v1: https://msgid.link/[email protected]

---
 rust/kernel/Kconfig.test        |  10 +
 rust/kernel/alloc.rs            |   6 +
 rust/kernel/alloc/ringbuffer.rs | 453 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 469 insertions(+)
rust/kernel/Kconfig.test +10 -0
index 3bf4194f28c7..5d9b5711e3bf 100644
@@ -33,6 +33,16 @@ config RUST_KVEC_KUNIT_TEST
 
 	  If unsure, say N.
 
+config RUST_RINGBUFFER_KUNIT_TEST
+	bool "KUnit tests for the Rust ring buffer data structure" if !KUNIT_ALL_TESTS
+	default KUNIT_ALL_TESTS
+	help
+	  This option enables KUnit tests for the Rust ring buffer data
+	  structure. These are only for development and testing, not for
+	  regular kernel use cases.
+
+	  If unsure, say N.
+
 config RUST_BITMAP_KUNIT_TEST
 	bool "KUnit tests for Rust bitmap API" if !KUNIT_ALL_TESTS
 	default KUNIT_ALL_TESTS
rust/kernel/alloc.rs +6 -0
index 68d80f3fa6c8..c27eda267317 100644
@@ -6,6 +6,7 @@
 pub mod kbox;
 pub mod kvec;
 pub mod layout;
+pub mod ringbuffer;
 
 pub use self::kbox::Box;
 pub use self::kbox::KBox;
@@ -18,6 +19,11 @@
 pub use self::kvec::VVec;
 pub use self::kvec::Vec;
 
+pub use self::ringbuffer::KRingBuffer;
+pub use self::ringbuffer::KVRingBuffer;
+pub use self::ringbuffer::RingBuffer;
+pub use self::ringbuffer::VRingBuffer;
+
 /// Indicates an allocation error.
 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
 pub struct AllocError;
rust/kernel/alloc/ringbuffer.rs new file +453 -0

453 lines not on this page · raw message

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20260215-ringbuffer-42455964aaf2

Best regards,

Signature · 1 line
Andreas Hindborg <[email protected]>
maintainer Applied
#1 lore raw
Quoted 1 line · Andreas Hindborg

[PATCH v5] rust: alloc: add a ring buffer implementation

Applied, thanks!

Branch: alloc-next
Tree: [email protected]:Rust-for-Linux/linux.git

[1/1] rust: alloc: add a ring buffer implementation

      commit: 0af3f653e155

The patch will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patch is queued up for the upcoming merge window for the next major kernel release.