[PATCH 14/17] ratelimit: assert static storage for DEFINE_RATELIMIT_STATE()
From: Yury Norov
Date: Mon Sep 14 2026 - 23:21:58 EST
DEFINE_RATELIMIT_STATE() uses a static raw spinlock initializer, which
requires a persistent object address for lockdep. Automatic objects must
use ratelimit_state_init().
Add ASSERT_STATIC_STORAGE() to the declaration and align the virtio tools
stub. The virtio tools use a private compiler.h, so provide the same
assertion helper and its unused attribute there as well.
Assisted-by: OpenAI Codex
Signed-off-by: Yury Norov <ynorov@xxxxxxxxxx>
---
include/linux/ratelimit_types.h | 4 +++-
tools/virtio/linux/compiler.h | 9 +++++++++
tools/virtio/linux/ratelimit.h | 6 +++++-
3 files changed, 17 insertions(+), 2 deletions(-)
diff --git a/include/linux/ratelimit_types.h b/include/linux/ratelimit_types.h
index b19c4354540a..977fdfff5926 100644
--- a/include/linux/ratelimit_types.h
+++ b/include/linux/ratelimit_types.h
@@ -2,6 +2,7 @@
#ifndef _LINUX_RATELIMIT_TYPES_H
#define _LINUX_RATELIMIT_TYPES_H
+#include <linux/compiler.h>
#include <linux/bits.h>
#include <linux/param.h>
#include <linux/spinlock_types_raw.h>
@@ -40,7 +41,8 @@ struct ratelimit_state {
#define DEFINE_RATELIMIT_STATE(name, interval_init, burst_init) \
\
struct ratelimit_state name = \
- RATELIMIT_STATE_INIT(name, interval_init, burst_init) \
+ RATELIMIT_STATE_INIT(name, interval_init, burst_init); \
+ ASSERT_STATIC_STORAGE(name)
extern int ___ratelimit(struct ratelimit_state *rs, const char *func);
#define __ratelimit(state) ___ratelimit(state, __func__)
diff --git a/tools/virtio/linux/compiler.h b/tools/virtio/linux/compiler.h
index 0f25db473f55..3c1eca4a4380 100644
--- a/tools/virtio/linux/compiler.h
+++ b/tools/virtio/linux/compiler.h
@@ -15,6 +15,15 @@
#define __aligned(x) __attribute((__aligned__(x)))
+#ifndef __always_unused
+#define __always_unused __attribute__((__unused__))
+#endif
+
+/* Enforce static storage duration. */
+#define ASSERT_STATIC_STORAGE(name) \
+ static typeof(name) * const __always_unused \
+ name##_storage_check = &(name)
+
/**
* data_race - mark an expression as containing intentional data races
*
diff --git a/tools/virtio/linux/ratelimit.h b/tools/virtio/linux/ratelimit.h
index dcce1725f90d..c15c82a5ca21 100644
--- a/tools/virtio/linux/ratelimit.h
+++ b/tools/virtio/linux/ratelimit.h
@@ -1,4 +1,8 @@
-#define DEFINE_RATELIMIT_STATE(name, interval_init, burst_init) int name = 0
+#include <linux/compiler.h>
+
+#define DEFINE_RATELIMIT_STATE(name, interval_init, burst_init) \
+ int name = 0; \
+ ASSERT_STATIC_STORAGE(name)
#define __ratelimit(x) (*(x))
--
2.53.0