[PATCH v2 72/83] block: rust: add a debug assert for refcounts

From: Andreas Hindborg

Date: Tue Jun 09 2026 - 15:26:00 EST


Add a debug assertion in `ARef<Request>::dismiss` to verify that the
request refcount is at least two when an `ARef<Request>` exists. This
helps catch reference counting bugs during development.

Signed-off-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
---
rust/kernel/block/mq/request.rs | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/rust/kernel/block/mq/request.rs b/rust/kernel/block/mq/request.rs
index 9c451583e75d..05b167dfc6c6 100644
--- a/rust/kernel/block/mq/request.rs
+++ b/rust/kernel/block/mq/request.rs
@@ -619,9 +619,20 @@ impl<T> RequestTimerHandle<T>
pub fn dismiss(mut self) {
let inner = core::ptr::from_mut(&mut self.inner);

+ debug_assert!(
+ self.inner
+ .wrapper_ref()
+ .refcount()
+ .as_atomic()
+ .load(ordering::Relaxed)
+ >= 2,
+ "Request refcount must be at least two when an ARef<Request> exist"
+ );
+
// SAFETY: `inner` is valid for reads and writes, is properly aligned and nonnull. We have
// exclusive access to `inner` and we do not access `inner` after this call.
unsafe { core::ptr::drop_in_place(inner) };
+
core::mem::forget(self);
}
}

--
2.51.2