Skip to content

Commit 45c3e2f

Browse files
committed
Rust: Add pr_*_ratelimited
This commit adds equivalent pr_*_ratelimited to kernel crate. They are macros that define a static local `ratelimit_state` and uses Atomic and CAS to guard its initialization. That `ratelimit_state` will be used later by `___ratelimit()` to decide if to suppress the `printk` or not. Because we use Atomic & CAS to help initialize `ratelimit_state`, in concurrent situation some thread may reach `__ratelimit()` before `ratelimit_state` is initialized. If that happens, we call `printk` anyway. This commit does not implement pr_debug_ratelimited since it's special and way more complicated and deserves another dedicated commit. `printk_ratelimit` is also implemented in this commit Signed-off-by: Fox Chen <[email protected]>
1 parent b9ed1ad commit 45c3e2f

File tree

4 files changed

+376
-1
lines changed

4 files changed

+376
-1
lines changed

rust/helpers.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/platform_device.h>
1313
#include <linux/security.h>
1414
#include <asm/io.h>
15+
#include <linux/printk.h>
1516

1617
__noreturn void rust_helper_BUG(void)
1718
{
@@ -278,6 +279,13 @@ void *rust_helper_dev_get_drvdata(struct device *dev)
278279
}
279280
EXPORT_SYMBOL_GPL(rust_helper_dev_get_drvdata);
280281

282+
void rust_helper_ratelimit_state_init(struct ratelimit_state *rs)
283+
{
284+
static DEFINE_RATELIMIT_STATE(rs_tmpl, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST);
285+
*rs = rs_tmpl;
286+
}
287+
EXPORT_SYMBOL_GPL(rust_helper_ratelimit_state_init);
288+
281289
/* We use bindgen's --size_t-is-usize option to bind the C size_t type
282290
* as the Rust usize type, so we can use it in contexts where Rust
283291
* expects a usize like slice (array) indices. usize is defined to be

rust/kernel/prelude.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ pub use macros::{module, module_misc_device};
1919

2020
pub use super::build_assert;
2121

22-
pub use super::{dbg, pr_alert, pr_crit, pr_debug, pr_emerg, pr_err, pr_info, pr_notice, pr_warn};
22+
pub use super::{
23+
dbg, pr_alert, pr_alert_ratelimited, pr_crit, pr_crit_ratelimited, pr_debug, pr_emerg,
24+
pr_emerg_ratelimited, pr_err, pr_err_ratelimited, pr_info, pr_info_ratelimited, pr_notice,
25+
pr_notice_ratelimited, pr_warn, pr_warn_ratelimited, printk_ratelimit,
26+
};
2327

2428
pub use super::static_assert;
2529

0 commit comments

Comments
 (0)