mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] fault-inject: use a shared initialization helper for the random state
@ 2026-11-12 14:46 Sang-Heon Jeon
  2026-11-13  2:13 ` Andrew Morton
  0 siblings, 1 reply; 2+ messages 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] 2+ messages in thread

* Re: [PATCH] fault-inject: use a shared initialization helper for the random state
  2026-11-12 14:46 [PATCH] fault-inject: use a shared initialization helper for the random state Sang-Heon Jeon
@ 2026-11-13  2:13 ` Andrew Morton
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Morton @ 2026-11-13  2:13 UTC (permalink / raw)
  To: Sang-Heon Jeon; +Cc: Akinobu Mita, linux-kernel

On Thu, 12 Nov 2026 23:46:34 +0900 Sang-Heon Jeon <ekffu200098@gmail.com> 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 :(


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-13  2:13 UTC | newest]

Thread overview: 2+ messages (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
2026-11-13  2:13 ` Andrew Morton

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®