* [PATCH] fault-inject: use a shared initialization helper for the random state
@ 2026-11-12 14:46 Sang-Heon Jeon
0 siblings, 0 replies; only message in thread
From: Sang-Heon Jeon @ 2026-11-12 14:46 UTC (permalink / raw)
To: Akinobu Mita, Andrew Morton; +Cc: linux-kernel
fault_rnd_state can be seeded from setup_fault_attr(),
fault_create_debugfs_attr() and fault_config_init(), and each of them
uses its own guard. Configuring fault injection in more than one way
leads to useless reseeding.
To prevent this, use a shared initialization helper fault_random_init()
in all three places. So fault_rnd_state is seeded only once per boot.
Signed-off-by: Sang-Heon Jeon <ekffu200098@gmail.com>
---
As discussed in [1], I didn't add any tags other than my S-o-b.
But feel free to add tags if needed :)
[1] https://lore.kernel.org/all/20261105160209.1504867-1-ekffu200098@gmail.com/#t
---
lib/fault-inject.c | 25 ++++++++++++++++---------
1 file changed, 16 insertions(+), 9 deletions(-)
diff --git a/lib/fault-inject.c b/lib/fault-inject.c
index 9182d457baf5..599bd06fe453 100644
--- a/lib/fault-inject.c
+++ b/lib/fault-inject.c
@@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <linux/kernel.h>
#include <linux/init.h>
-#include <linux/once_lite.h>
#include <linux/prandom.h>
#include <linux/debugfs.h>
#include <linux/sched.h>
@@ -13,12 +12,25 @@
#include <linux/interrupt.h>
#include <linux/stacktrace.h>
#include <linux/fault-inject.h>
+#include <linux/spinlock.h>
/*
* The should_fail() functions use prandom instead of the normal Linux RNG
* since they don't need cryptographically secure random numbers.
*/
static DEFINE_PER_CPU(struct rnd_state, fault_rnd_state);
+static DEFINE_SPINLOCK(fault_rnd_lock);
+static bool fault_rnd_initialized;
+
+static void fault_random_init(void)
+{
+ spin_lock(&fault_rnd_lock);
+ if (!fault_rnd_initialized) {
+ prandom_seed_full_state(&fault_rnd_state);
+ fault_rnd_initialized = true;
+ }
+ spin_unlock(&fault_rnd_lock);
+}
static u32 fault_prandom_u32_below_100(void)
{
@@ -51,12 +63,7 @@ int setup_fault_attr(struct fault_attr *attr, char *str)
return 0;
}
- /*
- * __setup handlers run before slab and workqueues are initialized,
- * but prandom_init_once() calls kmalloc() and schedule_work(). So use
- * DO_ONCE_LITE() instead.
- */
- DO_ONCE_LITE(prandom_seed_full_state, &fault_rnd_state);
+ fault_random_init();
attr->probability = probability;
attr->interval = interval;
@@ -246,7 +253,7 @@ struct dentry *fault_create_debugfs_attr(const char *name,
if (IS_ERR(dir))
return dir;
- prandom_init_once(&fault_rnd_state);
+ fault_random_init();
debugfs_create_ul("probability", mode, dir, &attr->probability);
debugfs_create_ul("interval", mode, dir, &attr->interval);
@@ -462,7 +469,7 @@ static const struct config_item_type fault_config_type = {
void fault_config_init(struct fault_config *config, const char *name)
{
- prandom_init_once(&fault_rnd_state);
+ fault_random_init();
config_group_init_type_name(&config->group, name, &fault_config_type);
}
base-commit: e6514b988537d8577052c88b2a44c79610e8821a
--
2.53.0
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-12 14:46 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-12 14:46 [PATCH] fault-inject: use a shared initialization helper for the random state Sang-Heon Jeon
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®