Skip to content

Commit 726b9c4

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 <foxhlchen@gmail.com>
1 parent 08061bd commit 726b9c4

File tree

4 files changed

+357
-1
lines changed

4 files changed

+357
-1
lines changed

rust/helpers.c

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <linux/mutex.h>
1212
#include <linux/platform_device.h>
1313
#include <linux/security.h>
14+
#include <linux/printk.h>
15+
1416

1517
void rust_helper_BUG(void)
1618
{
@@ -218,6 +220,13 @@ void *rust_helper_dev_get_drvdata(struct device *dev)
218220
}
219221
EXPORT_SYMBOL_GPL(rust_helper_dev_get_drvdata);
220222

223+
void rust_helper_ratelimit_state_init(struct ratelimit_state *rs)
224+
{
225+
static DEFINE_RATELIMIT_STATE(rs_tmpl, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST);
226+
*rs = rs_tmpl;
227+
}
228+
EXPORT_SYMBOL_GPL(rust_helper_ratelimit_state_init);
229+
221230
/* We use bindgen's --size_t-is-usize option to bind the C size_t type
222231
* as the Rust usize type, so we can use it in contexts where Rust
223232
* expects a usize like slice (array) indices. usize is defined to be

rust/kernel/prelude.rs

+5-1
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::{pr_alert, pr_crit, pr_debug, pr_emerg, pr_err, pr_info, pr_notice, pr_warn};
22+
pub use super::{
23+
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)