diff mbox series

[v2] random: Fix the issue of '_might_sleep' function running in an atomic contex

Message ID 20240419105453.5440-1-guoyong.wang@mediatek.com (mailing list archive)
State New, archived
Headers show
Series [v2] random: Fix the issue of '_might_sleep' function running in an atomic contex | expand

Commit Message

Guoyong Wang April 19, 2024, 10:54 a.m. UTC
In the case that a delay is acceptable for 'crng_set_ready', it can be
deferred to a workqueue in order to accommodate different contexts.

Signed-off-by: Guoyong Wang <guoyong.wang@mediatek.com>
---
v2: Compared to version 1, version 2 has removed the definition of
'execute_in_non_atomic_context' and always uses a workqueue to execute 
'crng_set_ready'.

  Send out the patch again for further discussion.

[1]: https://patchwork.kernel.org/patch/13595066

---
 drivers/char/random.c     | 10 +++++-----
 include/linux/workqueue.h |  1 -
 kernel/workqueue.c        | 26 --------------------------
 3 files changed, 5 insertions(+), 32 deletions(-)

Comments

Jason A. Donenfeld April 19, 2024, 12:05 p.m. UTC | #1
On Fri, Apr 19, 2024 at 06:54:53PM +0800, Guoyong Wang wrote:
> In the case that a delay is acceptable for 'crng_set_ready', it can be
> deferred to a workqueue in order to accommodate different contexts.
> 
> Signed-off-by: Guoyong Wang <guoyong.wang@mediatek.com>
> ---
> v2: Compared to version 1, version 2 has removed the definition of
> 'execute_in_non_atomic_context' and always uses a workqueue to execute 
> 'crng_set_ready'.
> 
>   Send out the patch again for further discussion.
> 
> [1]: https://patchwork.kernel.org/patch/13595066

Sorry, but this doesn't make sense in terms of how kernel development is
typically done:

1) Before you sent this, I told you there was no need for a v2.
2) The removal snippet at the bottom applies to your previous v1, not to
   upstream code.
3) The other snippet is taken verbatim from a patch that I posted.
4) That snippet is already upstream:
   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e871abcda3b67d0820b4182ebe93435624e9c6a4

There is no further work to do on this. Thank you for reporting the bug
- very appreciated - and for discussing the fix. But now we have a fix.
So everything is all set and there's nothing else to do and everyone can
be happy.

Jason
diff mbox series

Patch

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 00be9426a6fc..2597cb43f438 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -702,7 +702,7 @@  static void extract_entropy(void *buf, size_t len)
 
 static void __cold _credit_init_bits(size_t bits)
 {
-	static struct execute_work set_ready;
+	static DECLARE_WORK(set_ready, crng_set_ready);
 	unsigned int new, orig, add;
 	unsigned long flags;
 
@@ -718,8 +718,8 @@  static void __cold _credit_init_bits(size_t bits)
 
 	if (orig < POOL_READY_BITS && new >= POOL_READY_BITS) {
 		crng_reseed(NULL); /* Sets crng_init to CRNG_READY under base_crng.lock. */
-		if (static_key_initialized)
-			execute_in_non_atomic_context(crng_set_ready, &set_ready);
+		if (static_key_initialized && system_unbound_wq)
+			queue_work(system_unbound_wq, &set_ready);
 		atomic_notifier_call_chain(&random_ready_notifier, 0, NULL);
 		wake_up_interruptible(&crng_init_wait);
 		kill_fasync(&fasync, SIGIO, POLL_IN);
@@ -890,8 +890,8 @@  void __init random_init(void)
 
 	/*
 	 * If we were initialized by the cpu or bootloader before jump labels
-	 * are initialized, then we should enable the static branch here, where
-	 * it's guaranteed that jump labels have been initialized.
+	 * or workqueues are initialized, then we should enable the static
+	 * branch here, where it's guaranteed that these have been initialized.
 	 */
 	if (!static_branch_likely(&crng_is_ready) && crng_init >= CRNG_READY)
 		crng_set_ready(NULL);
diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h
index eb17c62d23aa..158784dd189a 100644
--- a/include/linux/workqueue.h
+++ b/include/linux/workqueue.h
@@ -550,7 +550,6 @@  extern void drain_workqueue(struct workqueue_struct *wq);
 extern int schedule_on_each_cpu(work_func_t func);
 
 int execute_in_process_context(work_func_t fn, struct execute_work *);
-int execute_in_non_atomic_context(work_func_t fn, struct execute_work *ew);
 
 extern bool flush_work(struct work_struct *work);
 extern bool cancel_work(struct work_struct *work);
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 8f212346da7a..bf2bdac46843 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -4449,32 +4449,6 @@  int execute_in_process_context(work_func_t fn, struct execute_work *ew)
 }
 EXPORT_SYMBOL_GPL(execute_in_process_context);
 
-/**
- * execute_in_non_atomic_context - reliably execute the routine with user context
- * @fn:		the function to execute
- * @ew:		guaranteed storage for the execute work structure (must
- *		be available when the work executes)
- *
- * Schedules the function for delayed execution if atomic context is available,
- * otherwise executes the function immediately .
- *
- * Return:	0 - function was executed
- *		1 - function was scheduled for execution
- */
-int execute_in_non_atomic_context(work_func_t fn, struct execute_work *ew)
-{
-	if (!in_atomic()) {
-		fn(&ew->work);
-		return 0;
-	}
-
-	INIT_WORK(&ew->work, fn);
-	schedule_work(&ew->work);
-
-	return 1;
-}
-EXPORT_SYMBOL_GPL(execute_in_non_atomic_context);
-
 /**
  * free_workqueue_attrs - free a workqueue_attrs
  * @attrs: workqueue_attrs to free