Skip to contentSkip to conversation

rcu: Fix the start_poll_synchronize_rcu_expedited() be invoked very early

PATCH In review Zqiang 4 participants 10 messages Jan 12, 2023 – Jan 13, 2023 RCU

lore
Share & tools
lore

Zqiang

author
#0 lore raw

PATCH v2 1/1 rcu: Fix the start_poll_synchronize_rcu_expedited() be invoked very early

Currently, the start_poll_synchronize_rcu_expedited() can be invoked very early. before rcu_init(), the rcu_data structure's->mynode is not initialized, if invoke start_poll_synchronize_rcu_expedited() before rcu_init(), will trigger a null rcu_node structure's->exp_seq_poll access.

This commit add boot_exp_seq_poll_rq member to rcu_state structure to store seq number return by invoke start_poll_synchronize_rcu_expedited() very early.

Fixes d96c52fe4907 ("rcu: Add polled expedited grace-period primitives")
---
 kernel/rcu/tree.c     | 3 ++-
 kernel/rcu/tree.h     | 1 +
 kernel/rcu/tree_exp.h | 6 ++++--
 3 files changed, 7 insertions(+), 3 deletions(-)
kernel/rcu/tree.c +2 -1
index 63545d79da51..12f0891ce7f4 100644
@@ -92,6 +92,7 @@ static struct rcu_state rcu_state = {
 	.exp_mutex = __MUTEX_INITIALIZER(rcu_state.exp_mutex),
 	.exp_wake_mutex = __MUTEX_INITIALIZER(rcu_state.exp_wake_mutex),
 	.ofl_lock = __ARCH_SPIN_LOCK_UNLOCKED,
+	.boot_exp_seq_poll_rq = RCU_GET_STATE_COMPLETED,
 };
 
 /* Dump rcu_node combining tree at boot to verify correct setup. */
@@ -4938,7 +4939,7 @@ void __init rcu_init(void)
 		qovld_calc = qovld;
 
 	// Kick-start any polled grace periods that started early.
-	if (!(per_cpu_ptr(&rcu_data, cpu)->mynode->exp_seq_poll_rq & 0x1))
+	if (!(rcu_state.boot_exp_seq_poll_rq & 0x1))
 		(void)start_poll_synchronize_rcu_expedited();
 
 	rcu_test_sync_prims();
kernel/rcu/tree.h +1 -0
index 192536916f9a..ae50ca6853ad 100644
@@ -397,6 +397,7 @@ struct rcu_state {
 						/* Synchronize offline with */
 						/*  GP pre-initialization. */
 	int nocb_is_setup;			/* nocb is setup from boot */
+	unsigned long boot_exp_seq_poll_rq;
 };
 
 /* Values for rcu_state structure's gp_flags field. */
kernel/rcu/tree_exp.h +4 -2
index 956cd459ba7f..1b35a1e233d9 100644
@@ -1068,9 +1068,11 @@ unsigned long start_poll_synchronize_rcu_expedited(void)
 	if (rcu_init_invoked())
 		raw_spin_lock_irqsave(&rnp->exp_poll_lock, flags);
 	if (!poll_state_synchronize_rcu(s)) {
-		rnp->exp_seq_poll_rq = s;
-		if (rcu_init_invoked())
+		if (rcu_init_invoked()) {
+			rnp->exp_seq_poll_rq = s;
 			queue_work(rcu_gp_wq, &rnp->exp_poll_wq);
+		} else
+			rcu_state.boot_exp_seq_poll_rq = s;
 	}
 	if (rcu_init_invoked())
 		raw_spin_unlock_irqrestore(&rnp->exp_poll_lock, flags);
Signature · 1 line
2.25.1
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 11 lines · Zqiang

Currently, the start_poll_synchronize_rcu_expedited() can be invoked very early. before rcu_init(), the rcu_data structure's->mynode is not initialized, if invoke start_poll_synchronize_rcu_expedited() before rcu_init(), will trigger a null rcu_node structure's->exp_seq_poll access.

This commit add boot_exp_seq_poll_rq member to rcu_state structure to store seq number return by invoke start_poll_synchronize_rcu_expedited() very early.

Fixes d96c52fe4907 ("rcu: Add polled expedited grace-period primitives")

Just a nit below:

Quoted 19 lines
---
 kernel/rcu/tree.c     | 3 ++-
 kernel/rcu/tree.h     | 1 +
 kernel/rcu/tree_exp.h | 6 ++++--
 3 files changed, 7 insertions(+), 3 deletions(-)
kernel/rcu/tree.c +1 -0
index 63545d79da51..12f0891ce7f4 100644
@@ -92,6 +92,7 @@ static struct rcu_state rcu_state = {
 	.exp_mutex = __MUTEX_INITIALIZER(rcu_state.exp_mutex),
 	.exp_wake_mutex = __MUTEX_INITIALIZER(rcu_state.exp_wake_mutex),
 	.ofl_lock = __ARCH_SPIN_LOCK_UNLOCKED,
+	.boot_exp_seq_poll_rq = RCU_GET_STATE_COMPLETED,
 };
 
 /* Dump rcu_node combining tree at boot to verify correct setup. */

kernel/rcu/tree.c void __init rcu_init(void) · line 4942

Quoted 5 lines
kernel/rcu/tree.c +1 -1
@@ -4938,7 +4939,7 @@ void __init rcu_init(void)
 		qovld_calc = qovld;
 
 	// Kick-start any polled grace periods that started early.
-	if (!(per_cpu_ptr(&rcu_data, cpu)->mynode->exp_seq_poll_rq & 0x1))
+	if (!(rcu_state.boot_exp_seq_poll_rq & 0x1))

This can be "if (!(rcu_state.boot_exp_seq_poll_rq == RCU_GET_STATE_COMPLETED))"

kernel/rcu/tree.h struct rcu_state { · line 400

Quoted 12 lines
kernel/rcu/tree.c +0 -0
 		(void)start_poll_synchronize_rcu_expedited();
 
 	rcu_test_sync_prims();
kernel/rcu/tree.h +1 -0
index 192536916f9a..ae50ca6853ad 100644
@@ -397,6 +397,7 @@ struct rcu_state {
 						/* Synchronize offline with */
 						/*  GP pre-initialization. */
 	int nocb_is_setup;			/* nocb is setup from boot */
+	unsigned long boot_exp_seq_poll_rq;

A comment on the right can mention: "/* exp seq poll request before rcu_init() */"

Thanks!

Quoted 24 lines
kernel/rcu/tree.h +0 -0
 };
 
 /* Values for rcu_state structure's gp_flags field. */
kernel/rcu/tree_exp.h +4 -2
index 956cd459ba7f..1b35a1e233d9 100644
@@ -1068,9 +1068,11 @@ unsigned long start_poll_synchronize_rcu_expedited(void)
 	if (rcu_init_invoked())
 		raw_spin_lock_irqsave(&rnp->exp_poll_lock, flags);
 	if (!poll_state_synchronize_rcu(s)) {
-		rnp->exp_seq_poll_rq = s;
-		if (rcu_init_invoked())
+		if (rcu_init_invoked()) {
+			rnp->exp_seq_poll_rq = s;
 			queue_work(rcu_gp_wq, &rnp->exp_poll_wq);
+		} else
+			rcu_state.boot_exp_seq_poll_rq = s;
 	}
 	if (rcu_init_invoked())
 		raw_spin_unlock_irqrestore(&rnp->exp_poll_lock, flags);
Signature · 1 line
2.25.1
Reviewed-by on v2 1/1
#2 lore raw
In reply to Zqiang
Quoted 8 lines · Zqiang

Currently, the start_poll_synchronize_rcu_expedited() can be invoked very early. before rcu_init(), the rcu_data structure's->mynode is not initialized, if invoke start_poll_synchronize_rcu_expedited() before rcu_init(), will trigger a null rcu_node structure's->exp_seq_poll access.

Quoted 6 lines

This commit add boot_exp_seq_poll_rq member to rcu_state structure to store seq number return by invoke start_poll_synchronize_rcu_expedited() very early.

Fixes d96c52fe4907 ("rcu: Add polled expedited grace-period primitives")

Thanks.

Quoted 60 lines

---

kernel/rcu/tree.c     | 3 ++-
kernel/rcu/tree.h     | 1 +
kernel/rcu/tree_exp.h | 6 ++++--
3 files changed, 7 insertions(+), 3 deletions(-)
kernel/rcu/tree.c +2 -1
index 63545d79da51..12f0891ce7f4 100644
@@ -92,6 +92,7 @@ static struct rcu_state rcu_state = {
   .exp_mutex = __MUTEX_INITIALIZER(rcu_state.exp_mutex),
   .exp_wake_mutex = __MUTEX_INITIALIZER(rcu_state.exp_wake_mutex),
   .ofl_lock = __ARCH_SPIN_LOCK_UNLOCKED,
+    .boot_exp_seq_poll_rq = RCU_GET_STATE_COMPLETED,
 };
 
 /* Dump rcu_node combining tree at boot to verify correct setup. */
@@ -4938,7 +4939,7 @@ void __init rcu_init(void)
       qovld_calc = qovld;
 
   // Kick-start any polled grace periods that started early.
-    if (!(per_cpu_ptr(&rcu_data, cpu)->mynode->exp_seq_poll_rq & 0x1))
+    if (!(rcu_state.boot_exp_seq_poll_rq & 0x1))
       (void)start_poll_synchronize_rcu_expedited();
 
   rcu_test_sync_prims();
kernel/rcu/tree.h +1 -0
index 192536916f9a..ae50ca6853ad 100644
@@ -397,6 +397,7 @@ struct rcu_state {
                       /* Synchronize offline with */
                       /*  GP pre-initialization. */
   int nocb_is_setup;            /* nocb is setup from boot */
+    unsigned long boot_exp_seq_poll_rq;
 };
 
 /* Values for rcu_state structure's gp_flags field. */
kernel/rcu/tree_exp.h +4 -2
index 956cd459ba7f..1b35a1e233d9 100644
@@ -1068,9 +1068,11 @@ unsigned long start_poll_synchronize_rcu_expedited(void)
   if (rcu_init_invoked())
       raw_spin_lock_irqsave(&rnp->exp_poll_lock, flags);
   if (!poll_state_synchronize_rcu(s)) {
-        rnp->exp_seq_poll_rq = s;
-        if (rcu_init_invoked())
+        if (rcu_init_invoked()) {
+            rnp->exp_seq_poll_rq = s;
           queue_work(rcu_gp_wq, &rnp->exp_poll_wq);
+        } else
+            rcu_state.boot_exp_seq_poll_rq = s;
   }
   if (rcu_init_invoked())
       raw_spin_unlock_irqrestore(&rnp->exp_poll_lock, flags);
Signature · 1 line
2.25.1
maintainer Reviewed-by Frederic Weisbecker Reviewed-by Joel Fernandes (Google) on v2 1/1
#3 lore raw
In reply to Zqiang
Quoted 11 lines · Zqiang

Currently, the start_poll_synchronize_rcu_expedited() can be invoked very early. before rcu_init(), the rcu_data structure's->mynode is not initialized, if invoke start_poll_synchronize_rcu_expedited() before rcu_init(), will trigger a null rcu_node structure's->exp_seq_poll access.

This commit add boot_exp_seq_poll_rq member to rcu_state structure to store seq number return by invoke start_poll_synchronize_rcu_expedited() very early.

Fixes d96c52fe4907 ("rcu: Add polled expedited grace-period primitives")

First off, excellent catch, Zqiang!!!

And thank you for Frederic and Joel for your reviews.

But I believe that this can be simplified, for example, as shown in the (untested) patch below.

Thoughts?

And yes, I did presumptuously add Frederic's and Joel's reviews. Please let me know if you disagree, and if so what different approach you would prefer. (Though of course simple disagreement is sufficient for me to remove your tag. Not holding you hostage for improvements, not yet, anyway!)

Thanx, Paul

------------------------------------------------------------------------

commit e05af5cb3858e669c9e6b70e0aca708cc70457da

Author: Zqiang <[email protected]>
Date:   Thu Jan 12 10:48:29 2023 -0800

rcu: Permit start_poll_synchronize_rcu_expedited() to be invoked early

According to the commit log of the patch that added it to the kernel, start_poll_synchronize_rcu_expedited() can be invoked very early, as in long before rcu_init() has been invoked. But before rcu_init(), the rcu_data structure's ->mynode field has not yet been initialized. This means that the start_poll_synchronize_rcu_expedited() function's attempt to set the CPU's leaf rcu_node structure's ->exp_seq_poll_rq field will result in a segmentation fault.

This commit therefore causes start_poll_synchronize_rcu_expedited() to set ->exp_seq_poll_rq only after rcu_init() has initialized all CPUs' rcu_data structures' ->mynode fields. It also removes the check from the rcu_init() function so that start_poll_synchronize_rcu_expedited( is unconditionally invoked. Yes, this might result in an unnecessary boot-time grace period, but this is down in the noise. Besides, there only has to be one call_rcu() invoked prior to scheduler initialization to make this boot-time grace period necessary.

kernel/rcu/tree.c +2 -3
index 63545d79da51c..f2e3a23778c06 100644
@@ -4937,9 +4937,8 @@ void __init rcu_init(void)
 	else
 		qovld_calc = qovld;
 
-	// Kick-start any polled grace periods that started early.
-	if (!(per_cpu_ptr(&rcu_data, cpu)->mynode->exp_seq_poll_rq & 0x1))
-		(void)start_poll_synchronize_rcu_expedited();
+	// Kick-start in case any polled grace periods started early.
+	(void)start_poll_synchronize_rcu_expedited();
 
 	rcu_test_sync_prims();
 }
kernel/rcu/tree_exp.h +3 -2
index 956cd459ba7f3..3b7abb58157df 100644
@@ -1068,9 +1068,10 @@ unsigned long start_poll_synchronize_rcu_expedited(void)
 	if (rcu_init_invoked())
 		raw_spin_lock_irqsave(&rnp->exp_poll_lock, flags);
 	if (!poll_state_synchronize_rcu(s)) {
-		rnp->exp_seq_poll_rq = s;
-		if (rcu_init_invoked())
+		if (rcu_init_invoked()) {
+			rnp->exp_seq_poll_rq = s;
 			queue_work(rcu_gp_wq, &rnp->exp_poll_wq);
+		}
 	}
 	if (rcu_init_invoked())
 		raw_spin_unlock_irqrestore(&rnp->exp_poll_lock, flags);
#4 lore raw
Quoted 12 lines · Zqiang

Currently, the start_poll_synchronize_rcu_expedited() can be invoked very early. before rcu_init(), the rcu_data structure's->mynode is not initialized, if invoke start_poll_synchronize_rcu_expedited() before rcu_init(), will trigger a null rcu_node structure's->exp_seq_poll access.

This commit add boot_exp_seq_poll_rq member to rcu_state structure to store seq number return by invoke start_poll_synchronize_rcu_expedited() very early.

Fixes d96c52fe4907 ("rcu: Add polled expedited grace-period primitives")
Quoted 8 lines

First off, excellent catch, Zqiang!!!

And thank you for Frederic and Joel for your reviews.

But I believe that this can be simplified, for example, as shown in the (untested) patch below.

Thoughts?

Agree, thanks for wordsmithed 😊.

Quoted 72 lines

And yes, I did presumptuously add Frederic's and Joel's reviews. Please let me know if you disagree, and if so what different approach you would prefer. (Though of course simple disagreement is sufficient for me to remove your tag. Not holding you hostage for improvements, not yet, anyway!)

Thanx, Paul

------------------------------------------------------------------------

commit e05af5cb3858e669c9e6b70e0aca708cc70457da

Author: Zqiang <[email protected]>
Date:   Thu Jan 12 10:48:29 2023 -0800

rcu: Permit start_poll_synchronize_rcu_expedited() to be invoked early

According to the commit log of the patch that added it to the kernel, start_poll_synchronize_rcu_expedited() can be invoked very early, as in long before rcu_init() has been invoked. But before rcu_init(), the rcu_data structure's ->mynode field has not yet been initialized. This means that the start_poll_synchronize_rcu_expedited() function's attempt to set the CPU's leaf rcu_node structure's ->exp_seq_poll_rq field will result in a segmentation fault.

This commit therefore causes start_poll_synchronize_rcu_expedited() to set ->exp_seq_poll_rq only after rcu_init() has initialized all CPUs' rcu_data structures' ->mynode fields. It also removes the check from the rcu_init() function so that start_poll_synchronize_rcu_expedited( is unconditionally invoked. Yes, this might result in an unnecessary boot-time grace period, but this is down in the noise. Besides, there only has to be one call_rcu() invoked prior to scheduler initialization to make this boot-time grace period necessary.

kernel/rcu/tree.c +2 -3
index 63545d79da51c..f2e3a23778c06 100644
@@ -4937,9 +4937,8 @@ void __init rcu_init(void)
 	else
 		qovld_calc = qovld;
 
-	// Kick-start any polled grace periods that started early.
-	if (!(per_cpu_ptr(&rcu_data, cpu)->mynode->exp_seq_poll_rq & 0x1))
-		(void)start_poll_synchronize_rcu_expedited();
+	// Kick-start in case any polled grace periods started early.
+	(void)start_poll_synchronize_rcu_expedited();
 
 	rcu_test_sync_prims();
 }
kernel/rcu/tree_exp.h +3 -2
index 956cd459ba7f3..3b7abb58157df 100644
@@ -1068,9 +1068,10 @@ unsigned long start_poll_synchronize_rcu_expedited(void)
 	if (rcu_init_invoked())
 		raw_spin_lock_irqsave(&rnp->exp_poll_lock, flags);
 	if (!poll_state_synchronize_rcu(s)) {
-		rnp->exp_seq_poll_rq = s;
-		if (rcu_init_invoked())
+		if (rcu_init_invoked()) {
+			rnp->exp_seq_poll_rq = s;
 			queue_work(rcu_gp_wq, &rnp->exp_poll_wq);
+		}
 	}
 	if (rcu_init_invoked())
 		raw_spin_unlock_irqrestore(&rnp->exp_poll_lock, flags);
#5 lore raw
Quoted 49 lines

Currently, the start_poll_synchronize_rcu_expedited() can be invoked very early. before rcu_init(), the rcu_data structure's->mynode is not initialized, if invoke start_poll_synchronize_rcu_expedited() before rcu_init(), will trigger a null rcu_node structure's->exp_seq_poll access.

This commit add boot_exp_seq_poll_rq member to rcu_state structure to store seq number return by invoke start_poll_synchronize_rcu_expedited() very early.

Fixes d96c52fe4907 ("rcu: Add polled expedited grace-period primitives")

First off, excellent catch, Zqiang!!!

And thank you for Frederic and Joel for your reviews.

But I believe that this can be simplified, for example, as shown in the (untested) patch below.

Thoughts?

Agree, thanks for wordsmithed 😊.

And yes, I did presumptuously add Frederic's and Joel's reviews. Please let me know if you disagree, and if so what different approach you would prefer. (Though of course simple disagreement is sufficient for me to remove your tag. Not holding you hostage for improvements, not yet, anyway!)

Thanx, Paul

------------------------------------------------------------------------

commit e05af5cb3858e669c9e6b70e0aca708cc70457da

Author: Zqiang <[email protected]>
Date:   Thu Jan 12 10:48:29 2023 -0800

rcu: Permit start_poll_synchronize_rcu_expedited() to be invoked early

According to the commit log of the patch that added it to the kernel, start_poll_synchronize_rcu_expedited() can be invoked very early, as in long before rcu_init() has been invoked. But before rcu_init(), the rcu_data structure's ->mynode field has not yet been initialized. This means that the start_poll_synchronize_rcu_expedited() function's attempt to set the CPU's leaf rcu_node structure's ->exp_seq_poll_rq field will result in a segmentation fault.

Quoted 8 lines

This commit therefore causes start_poll_synchronize_rcu_expedited() to set ->exp_seq_poll_rq only after rcu_init() has initialized all CPUs' rcu_data structures' ->mynode fields. It also removes the check from the rcu_init() function so that start_poll_synchronize_rcu_expedited( is unconditionally invoked. Yes, this might result in an unnecessary boot-time grace period, but this is down in the noise. Besides, there only has to be one call_rcu() invoked prior to scheduler initialization to make this boot-time grace period necessary.

A little confused, why call_rcu() invoked prior to scheduler initialization to make this boot-time grace period necessary 😊?

Thanks
Zqiang

Quoted 39 lines
kernel/rcu/tree.c +2 -3
index 63545d79da51c..f2e3a23778c06 100644
@@ -4937,9 +4937,8 @@ void __init rcu_init(void)
 	else
 		qovld_calc = qovld;
 
-	// Kick-start any polled grace periods that started early.
-	if (!(per_cpu_ptr(&rcu_data, cpu)->mynode->exp_seq_poll_rq & 0x1))
-		(void)start_poll_synchronize_rcu_expedited();
+	// Kick-start in case any polled grace periods started early.
+	(void)start_poll_synchronize_rcu_expedited();
 
 	rcu_test_sync_prims();
 }
kernel/rcu/tree_exp.h +3 -2
index 956cd459ba7f3..3b7abb58157df 100644
@@ -1068,9 +1068,10 @@ unsigned long start_poll_synchronize_rcu_expedited(void)
 	if (rcu_init_invoked())
 		raw_spin_lock_irqsave(&rnp->exp_poll_lock, flags);
 	if (!poll_state_synchronize_rcu(s)) {
-		rnp->exp_seq_poll_rq = s;
-		if (rcu_init_invoked())
+		if (rcu_init_invoked()) {
+			rnp->exp_seq_poll_rq = s;
 			queue_work(rcu_gp_wq, &rnp->exp_poll_wq);
+		}
 	}
 	if (rcu_init_invoked())
 		raw_spin_unlock_irqrestore(&rnp->exp_poll_lock, flags);
#6 lore raw
Quoted 49 lines

Currently, the start_poll_synchronize_rcu_expedited() can be invoked very early. before rcu_init(), the rcu_data structure's->mynode is not initialized, if invoke start_poll_synchronize_rcu_expedited() before rcu_init(), will trigger a null rcu_node structure's->exp_seq_poll access.

This commit add boot_exp_seq_poll_rq member to rcu_state structure to store seq number return by invoke start_poll_synchronize_rcu_expedited() very early.

Fixes d96c52fe4907 ("rcu: Add polled expedited grace-period primitives")

First off, excellent catch, Zqiang!!!

And thank you for Frederic and Joel for your reviews.

But I believe that this can be simplified, for example, as shown in the (untested) patch below.

Thoughts?

Agree, thanks for wordsmithed 😊.

And yes, I did presumptuously add Frederic's and Joel's reviews. Please let me know if you disagree, and if so what different approach you would prefer. (Though of course simple disagreement is sufficient for me to remove your tag. Not holding you hostage for improvements, not yet, anyway!)

Thanx, Paul

------------------------------------------------------------------------

commit e05af5cb3858e669c9e6b70e0aca708cc70457da

Author: Zqiang <[email protected]>
Date:   Thu Jan 12 10:48:29 2023 -0800

rcu: Permit start_poll_synchronize_rcu_expedited() to be invoked early

According to the commit log of the patch that added it to the kernel, start_poll_synchronize_rcu_expedited() can be invoked very early, as in long before rcu_init() has been invoked. But before rcu_init(), the rcu_data structure's ->mynode field has not yet been initialized. This means that the start_poll_synchronize_rcu_expedited() function's attempt to set the CPU's leaf rcu_node structure's ->exp_seq_poll_rq field will result in a segmentation fault.

Quoted 8 lines

This commit therefore causes start_poll_synchronize_rcu_expedited() to set ->exp_seq_poll_rq only after rcu_init() has initialized all CPUs' rcu_data structures' ->mynode fields. It also removes the check from the rcu_init() function so that start_poll_synchronize_rcu_expedited( is unconditionally invoked. Yes, this might result in an unnecessary boot-time grace period, but this is down in the noise. Besides, there only has to be one call_rcu() invoked prior to scheduler initialization to make this boot-time grace period necessary.

A little confused, why call_rcu() invoked prior to scheduler initialization to make this boot-time grace period necessary 😊? affects call_rcu() is the normal grace period, but we are polling is expedited grace period.

Thanks
Zqiang

Quoted 39 lines
kernel/rcu/tree.c +2 -3
index 63545d79da51c..f2e3a23778c06 100644
@@ -4937,9 +4937,8 @@ void __init rcu_init(void)
 	else
 		qovld_calc = qovld;
 
-	// Kick-start any polled grace periods that started early.
-	if (!(per_cpu_ptr(&rcu_data, cpu)->mynode->exp_seq_poll_rq & 0x1))
-		(void)start_poll_synchronize_rcu_expedited();
+	// Kick-start in case any polled grace periods started early.
+	(void)start_poll_synchronize_rcu_expedited();
 
 	rcu_test_sync_prims();
 }
kernel/rcu/tree_exp.h +3 -2
index 956cd459ba7f3..3b7abb58157df 100644
@@ -1068,9 +1068,10 @@ unsigned long start_poll_synchronize_rcu_expedited(void)
 	if (rcu_init_invoked())
 		raw_spin_lock_irqsave(&rnp->exp_poll_lock, flags);
 	if (!poll_state_synchronize_rcu(s)) {
-		rnp->exp_seq_poll_rq = s;
-		if (rcu_init_invoked())
+		if (rcu_init_invoked()) {
+			rnp->exp_seq_poll_rq = s;
 			queue_work(rcu_gp_wq, &rnp->exp_poll_wq);
+		}
 	}
 	if (rcu_init_invoked())
 		raw_spin_unlock_irqrestore(&rnp->exp_poll_lock, flags);
maintainer
#7 lore raw
In reply to Zhang, Qiang1
Quoted 58 lines · On Fri, Jan 13, 2023 at 12:10:47PM +0000, Zhang, Qiang1 wrote:
Quoted 49 lines

Currently, the start_poll_synchronize_rcu_expedited() can be invoked very early. before rcu_init(), the rcu_data structure's->mynode is not initialized, if invoke start_poll_synchronize_rcu_expedited() before rcu_init(), will trigger a null rcu_node structure's->exp_seq_poll access.

This commit add boot_exp_seq_poll_rq member to rcu_state structure to store seq number return by invoke start_poll_synchronize_rcu_expedited() very early.

Fixes d96c52fe4907 ("rcu: Add polled expedited grace-period primitives")

First off, excellent catch, Zqiang!!!

And thank you for Frederic and Joel for your reviews.

But I believe that this can be simplified, for example, as shown in the (untested) patch below.

Thoughts?

Agree, thanks for wordsmithed 😊.

And yes, I did presumptuously add Frederic's and Joel's reviews. Please let me know if you disagree, and if so what different approach you would prefer. (Though of course simple disagreement is sufficient for me to remove your tag. Not holding you hostage for improvements, not yet, anyway!)

Thanx, Paul

------------------------------------------------------------------------

commit e05af5cb3858e669c9e6b70e0aca708cc70457da

Author: Zqiang <[email protected]>
Date:   Thu Jan 12 10:48:29 2023 -0800

rcu: Permit start_poll_synchronize_rcu_expedited() to be invoked early

According to the commit log of the patch that added it to the kernel, start_poll_synchronize_rcu_expedited() can be invoked very early, as in long before rcu_init() has been invoked. But before rcu_init(), the rcu_data structure's ->mynode field has not yet been initialized. This means that the start_poll_synchronize_rcu_expedited() function's attempt to set the CPU's leaf rcu_node structure's ->exp_seq_poll_rq field will result in a segmentation fault.

Quoted 8 lines

This commit therefore causes start_poll_synchronize_rcu_expedited() to set ->exp_seq_poll_rq only after rcu_init() has initialized all CPUs' rcu_data structures' ->mynode fields. It also removes the check from the rcu_init() function so that start_poll_synchronize_rcu_expedited( is unconditionally invoked. Yes, this might result in an unnecessary boot-time grace period, but this is down in the noise. Besides, there only has to be one call_rcu() invoked prior to scheduler initialization to make this boot-time grace period necessary.

Quoted 2 lines

A little confused, why call_rcu() invoked prior to scheduler initialization to make this boot-time grace period necessary 😊?

Because then there will be a callback queued that will require a grace period to run anyway.

Or maybe you are asking if those callbacks will really be able to use that first grace period?

Thanx, Paul

Quoted 43 lines

Thanks
Zqiang

Quoted 39 lines
kernel/rcu/tree.c +2 -3
index 63545d79da51c..f2e3a23778c06 100644
@@ -4937,9 +4937,8 @@ void __init rcu_init(void)
 	else
 		qovld_calc = qovld;
 
-	// Kick-start any polled grace periods that started early.
-	if (!(per_cpu_ptr(&rcu_data, cpu)->mynode->exp_seq_poll_rq & 0x1))
-		(void)start_poll_synchronize_rcu_expedited();
+	// Kick-start in case any polled grace periods started early.
+	(void)start_poll_synchronize_rcu_expedited();
 
 	rcu_test_sync_prims();
 }
kernel/rcu/tree_exp.h +3 -2
index 956cd459ba7f3..3b7abb58157df 100644
@@ -1068,9 +1068,10 @@ unsigned long start_poll_synchronize_rcu_expedited(void)
 	if (rcu_init_invoked())
 		raw_spin_lock_irqsave(&rnp->exp_poll_lock, flags);
 	if (!poll_state_synchronize_rcu(s)) {
-		rnp->exp_seq_poll_rq = s;
-		if (rcu_init_invoked())
+		if (rcu_init_invoked()) {
+			rnp->exp_seq_poll_rq = s;
 			queue_work(rcu_gp_wq, &rnp->exp_poll_wq);
+		}
 	}
 	if (rcu_init_invoked())
 		raw_spin_unlock_irqrestore(&rnp->exp_poll_lock, flags);
#8 lore raw
Quoted 58 lines · On Fri, Jan 13, 2023 at 12:10:47PM +0000, Zhang, Qiang1 wrote:
Quoted 49 lines

Currently, the start_poll_synchronize_rcu_expedited() can be invoked very early. before rcu_init(), the rcu_data structure's->mynode is not initialized, if invoke start_poll_synchronize_rcu_expedited() before rcu_init(), will trigger a null rcu_node structure's->exp_seq_poll access.

This commit add boot_exp_seq_poll_rq member to rcu_state structure to store seq number return by invoke start_poll_synchronize_rcu_expedited() very early.

Fixes d96c52fe4907 ("rcu: Add polled expedited grace-period primitives")

First off, excellent catch, Zqiang!!!

And thank you for Frederic and Joel for your reviews.

But I believe that this can be simplified, for example, as shown in the (untested) patch below.

Thoughts?

Agree, thanks for wordsmithed 😊.

And yes, I did presumptuously add Frederic's and Joel's reviews. Please let me know if you disagree, and if so what different approach you would prefer. (Though of course simple disagreement is sufficient for me to remove your tag. Not holding you hostage for improvements, not yet, anyway!)

Thanx, Paul

------------------------------------------------------------------------

commit e05af5cb3858e669c9e6b70e0aca708cc70457da

Author: Zqiang <[email protected]>
Date:   Thu Jan 12 10:48:29 2023 -0800

rcu: Permit start_poll_synchronize_rcu_expedited() to be invoked early

According to the commit log of the patch that added it to the kernel, start_poll_synchronize_rcu_expedited() can be invoked very early, as in long before rcu_init() has been invoked. But before rcu_init(), the rcu_data structure's ->mynode field has not yet been initialized. This means that the start_poll_synchronize_rcu_expedited() function's attempt to set the CPU's leaf rcu_node structure's ->exp_seq_poll_rq field will result in a segmentation fault.

Quoted 8 lines

This commit therefore causes start_poll_synchronize_rcu_expedited() to set ->exp_seq_poll_rq only after rcu_init() has initialized all CPUs' rcu_data structures' ->mynode fields. It also removes the check from the rcu_init() function so that start_poll_synchronize_rcu_expedited( is unconditionally invoked. Yes, this might result in an unnecessary boot-time grace period, but this is down in the noise. Besides, there only has to be one call_rcu() invoked prior to scheduler initialization to make this boot-time grace period necessary.

Quoted 8 lines

A little confused, why call_rcu() invoked prior to scheduler initialization to make this boot-time grace period necessary 😊?

Because then there will be a callback queued that will require a grace period to run anyway.

Or maybe you are asking if those callbacks will really be able to use that first grace period?

Yes, because even if we queue work to rcu_gp_wq workqueue, this also requires us to wait until the workqueue_init() is invoked, our work can be execute. and also need to wait for the rcu_gp kthread to be created, after this, our first grace period can begin, the callbacks has the opportunity to be called. the call_rcu() require a grace period, but we require is expedited grace period.

Thanks
Zqiang

Quoted 46 lines

Thanx, Paul

Thanks
Zqiang

Quoted 39 lines
kernel/rcu/tree.c +2 -3
index 63545d79da51c..f2e3a23778c06 100644
@@ -4937,9 +4937,8 @@ void __init rcu_init(void)
 	else
 		qovld_calc = qovld;
 
-	// Kick-start any polled grace periods that started early.
-	if (!(per_cpu_ptr(&rcu_data, cpu)->mynode->exp_seq_poll_rq & 0x1))
-		(void)start_poll_synchronize_rcu_expedited();
+	// Kick-start in case any polled grace periods started early.
+	(void)start_poll_synchronize_rcu_expedited();
 
 	rcu_test_sync_prims();
 }
kernel/rcu/tree_exp.h +3 -2
index 956cd459ba7f3..3b7abb58157df 100644
@@ -1068,9 +1068,10 @@ unsigned long start_poll_synchronize_rcu_expedited(void)
 	if (rcu_init_invoked())
 		raw_spin_lock_irqsave(&rnp->exp_poll_lock, flags);
 	if (!poll_state_synchronize_rcu(s)) {
-		rnp->exp_seq_poll_rq = s;
-		if (rcu_init_invoked())
+		if (rcu_init_invoked()) {
+			rnp->exp_seq_poll_rq = s;
 			queue_work(rcu_gp_wq, &rnp->exp_poll_wq);
+		}
 	}
 	if (rcu_init_invoked())
 		raw_spin_unlock_irqrestore(&rnp->exp_poll_lock, flags);
maintainer
#9 lore raw
Quoted 65 lines · On Fri, Jan 13, 2023 at 03:02:54PM +0000, Zhang, Qiang1 wrote:
Quoted 64 lines · On Fri, Jan 13, 2023 at 12:10:47PM +0000, Zhang, Qiang1 wrote:
Quoted 49 lines

Currently, the start_poll_synchronize_rcu_expedited() can be invoked very early. before rcu_init(), the rcu_data structure's->mynode is not initialized, if invoke start_poll_synchronize_rcu_expedited() before rcu_init(), will trigger a null rcu_node structure's->exp_seq_poll access.

This commit add boot_exp_seq_poll_rq member to rcu_state structure to store seq number return by invoke start_poll_synchronize_rcu_expedited() very early.

Fixes d96c52fe4907 ("rcu: Add polled expedited grace-period primitives")

First off, excellent catch, Zqiang!!!

And thank you for Frederic and Joel for your reviews.

But I believe that this can be simplified, for example, as shown in the (untested) patch below.

Thoughts?

Agree, thanks for wordsmithed 😊.

And yes, I did presumptuously add Frederic's and Joel's reviews. Please let me know if you disagree, and if so what different approach you would prefer. (Though of course simple disagreement is sufficient for me to remove your tag. Not holding you hostage for improvements, not yet, anyway!)

Thanx, Paul

------------------------------------------------------------------------

commit e05af5cb3858e669c9e6b70e0aca708cc70457da

Author: Zqiang <[email protected]>
Date:   Thu Jan 12 10:48:29 2023 -0800

rcu: Permit start_poll_synchronize_rcu_expedited() to be invoked early

According to the commit log of the patch that added it to the kernel, start_poll_synchronize_rcu_expedited() can be invoked very early, as in long before rcu_init() has been invoked. But before rcu_init(), the rcu_data structure's ->mynode field has not yet been initialized. This means that the start_poll_synchronize_rcu_expedited() function's attempt to set the CPU's leaf rcu_node structure's ->exp_seq_poll_rq field will result in a segmentation fault.

Quoted 8 lines

This commit therefore causes start_poll_synchronize_rcu_expedited() to set ->exp_seq_poll_rq only after rcu_init() has initialized all CPUs' rcu_data structures' ->mynode fields. It also removes the check from the rcu_init() function so that start_poll_synchronize_rcu_expedited( is unconditionally invoked. Yes, this might result in an unnecessary boot-time grace period, but this is down in the noise. Besides, there only has to be one call_rcu() invoked prior to scheduler initialization to make this boot-time grace period necessary.

A little confused, why call_rcu() invoked prior to scheduler initialization to make this boot-time grace period necessary 😊?

Because then there will be a callback queued that will require a grace period to run anyway.

Quoted 7 lines
Quoted 2 lines

Or maybe you are asking if those callbacks will really be able to use that first grace period?

Yes, because even if we queue work to rcu_gp_wq workqueue, this also requires us to wait until the workqueue_init() is invoked, our work can be execute. and also need to wait for the rcu_gp kthread to be created, after this, our first grace period can begin, the callbacks has the opportunity to be called. the call_rcu() require a grace period, but we require is expedited grace period.

Good catch, thank you! I will update the commit log accordingly.

Thanx, Paul

Quoted 49 lines

Thanks
Zqiang

Quoted 46 lines

Thanx, Paul

Thanks
Zqiang

Quoted 39 lines
kernel/rcu/tree.c +2 -3
index 63545d79da51c..f2e3a23778c06 100644
@@ -4937,9 +4937,8 @@ void __init rcu_init(void)
 	else
 		qovld_calc = qovld;
 
-	// Kick-start any polled grace periods that started early.
-	if (!(per_cpu_ptr(&rcu_data, cpu)->mynode->exp_seq_poll_rq & 0x1))
-		(void)start_poll_synchronize_rcu_expedited();
+	// Kick-start in case any polled grace periods started early.
+	(void)start_poll_synchronize_rcu_expedited();
 
 	rcu_test_sync_prims();
 }
kernel/rcu/tree_exp.h +3 -2
index 956cd459ba7f3..3b7abb58157df 100644
@@ -1068,9 +1068,10 @@ unsigned long start_poll_synchronize_rcu_expedited(void)
 	if (rcu_init_invoked())
 		raw_spin_lock_irqsave(&rnp->exp_poll_lock, flags);
 	if (!poll_state_synchronize_rcu(s)) {
-		rnp->exp_seq_poll_rq = s;
-		if (rcu_init_invoked())
+		if (rcu_init_invoked()) {
+			rnp->exp_seq_poll_rq = s;
 			queue_work(rcu_gp_wq, &rnp->exp_poll_wq);
+		}
 	}
 	if (rcu_init_invoked())
 		raw_spin_unlock_irqrestore(&rnp->exp_poll_lock, flags);