Skip to contentSkip to conversation

fault-inject: use a shared initialization helper for the random state

PATCH In review Sang-Heon Jeon 2 participants 3 messages Nov 12 – Nov 13

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fault-inject: use a shared initialization helper for the random state

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.

---
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/[email protected]/#t

---
 lib/fault-inject.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
lib/fault-inject.c +16 -9
index 9182d457baf5..599bd06fe453 100644
@@ -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

Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 7 lines · On Thu, 12 Nov 2026 23:46:34 +0900 Sang-Heon Jeon <[email protected]> wrote:

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.

Thanks, that looks nice.

Generally, fault-injection doesn't see to get used very much, and the huge number of AI-generated you-messed-up-error-handling reports we're receiving show the cost of that.

syskaller can use it and those people have shown some interesting/useful results from its use.

I have a side-project going where I get the LLM to drive patch testing on a local test machine. The AI looks at the patches, figures out test plans then controls their execution. One of the many things I'm telling it do is to fault-inject every error path which the patchset(s) add. It seems to do this quite well. So I'm a big user of fault-injection now.

I'm hoping that I can also make it use kcov: use coverage instrumentation to verify that all code which the patchset added or altered has been runtime testing.

But oh my, it's soooooooooo slooooooooooooow. It does really impressive testing, but I need to make it run 10x faster :(

#2 lore raw

2026년 11월 13일 (금) 오전 11:13, Andrew Morton <[email protected]>님이 작성:

Quoted 11 lines · On Thu, 12 Nov 2026 23:46:34 +0900 Sang-Heon Jeon <[email protected]> wrote:
Quoted 8 lines

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.

Quoted 6 lines

Thanks, that looks nice.

Generally, fault-injection doesn't see to get used very much, and the huge number of AI-generated you-messed-up-error-handling reports we're receiving show the cost of that.

Considering boot cmdline broken period, I agree

Quoted 2 lines

syskaller can use it and those people have shown some interesting/useful results from its use.

It is very useful to set up a test environment as well.

Quoted 10 lines

I have a side-project going where I get the LLM to drive patch testing on a local test machine. The AI looks at the patches, figures out test plans then controls their execution. One of the many things I'm telling it do is to fault-inject every error path which the patchset(s) add. It seems to do this quite well. So I'm a big user of fault-injection now.

I'm hoping that I can also make it use kcov: use coverage instrumentation to verify that all code which the patchset added or altered has been runtime testing.

Sounds really cool. I could be really helpful.

Quoted 3 lines

But oh my, it's soooooooooo slooooooooooooow. It does really impressive testing, but I need to make it run 10x faster :(

We will find a way as we always have. Let me know if there's anything I can help with :)

Best regards,
Sang-Heon Jeon