[RFC 1/2] NOT FOR MERGE: A selftest shows that re-entrance can happen

From: Boqun Feng
Date: Fri Oct 08 2021 - 06:06:42 EST


Re-entrance can be confirmed when PREEMPT=y as the racy output below:

[ 1.498285] second queue succeeds
[ 1.500679] result of i is 1031665
[ 1.501069] result of i is 1221348

Signed-off-by: Boqun Feng <boqun.feng@xxxxxxxxx>
---
kernel/workqueue.c | 41 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 33a6b4a2443d..1418710bffcd 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -6099,3 +6099,44 @@ void __init workqueue_init(void)
wq_online = true;
wq_watchdog_init();
}
+
+struct temp_work {
+ struct work_struct work;
+ int i;
+};
+
+#include <linux/delay.h>
+static void __init work_func(struct work_struct *work)
+{
+ struct temp_work *tmp = (struct temp_work *)work;
+ int p = 0;
+ int q;
+
+ for (p = 0; p < 1000000; p++) {
+ q = READ_ONCE(tmp->i);
+ WRITE_ONCE(tmp->i, q + 1);
+ }
+
+ printk("result of i is %d\n", tmp->i);
+}
+
+static int __init work_reentry(void)
+{
+ struct temp_work tmp;
+
+ tmp.i = 0;
+
+ INIT_WORK_ONSTACK(&tmp.work, work_func);
+
+ queue_work_on(1, system_wq, &tmp.work);
+
+ while (!queue_work_on(2, system_unbound_wq, &tmp.work)) { }
+
+ printk("second queue succeeds\n");
+
+ flush_work(&tmp.work);
+
+ return 0;
+}
+
+late_initcall(work_reentry);
--
2.32.0