Skip to contentSkip to conversation

rcu: Latch normal synchronize_rcu() path on flood

PATCH Superseded Uladzislau Rezki (Sony) 3 participants 9 messages Mar 2 – Mar 11

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Uladzislau Rezki (Sony)

author maintainer
#0 lore raw

PATCH v2 1/1 rcu: Latch normal synchronize_rcu() path on flood

Currently, rcu_normal_wake_from_gp is only enabled by default on small systems(<= 16 CPUs) or when a user explicitly set it enabled.

Introduce an adaptive latching mechanism:
* Track the number of in-flight synchronize_rcu() requests using a new rcu_sr_normal_count counter;

* If the count reaches/exceeds RCU_SR_NORMAL_LATCH_THR(64), it sets the rcu_sr_normal_latched, reverting new requests onto the scaled wait_rcu_gp() path;

* The latch is cleared only when the pending requests are fully drained(nr == 0);

* Enables rcu_normal_wake_from_gp by default for all systems, relying on this dynamic throttling instead of static CPU limits.

Testing(synthetic flood workload):
* Kernel version: 6.19.0-rc6
* Number of CPUs: 1536
* 60K concurrent synchronize_rcu() calls

Perf(cycles, system-wide):
total cycles: 932020263832 rcu_sr_normal_add_req(): 2650282811 cycles(~0.28%)

Perf report excerpt:
0.01% 0.01% sync_test/... [k] rcu_sr_normal_add_req

Measured overhead of rcu_sr_normal_add_req() remained ~0.28% of total CPU cycles in this synthetic stress test.

---
 .../admin-guide/kernel-parameters.txt         | 10 ++---
 kernel/rcu/tree.c                             | 41 +++++++++++++------
 2 files changed, 33 insertions(+), 18 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +5 -5
index cb850e5290c2..d0574a02510d 100644
@@ -5867,13 +5867,13 @@ Kernel parameters
 			use a call_rcu[_hurry]() path. Please note, this is for a
 			normal grace period.
 
-			How to enable it:
+			How to disable it:
 
-			echo 1 > /sys/module/rcutree/parameters/rcu_normal_wake_from_gp
-			or pass a boot parameter "rcutree.rcu_normal_wake_from_gp=1"
+			echo 0 > /sys/module/rcutree/parameters/rcu_normal_wake_from_gp
+			or pass a boot parameter "rcutree.rcu_normal_wake_from_gp=0"
 
-			Default is 1 if num_possible_cpus() <= 16 and it is not explicitly
-			disabled by the boot parameter passing 0.
+			Default is 1 if it is not explicitly disabled by the boot parameter
+			passing 0.
 
 	rcuscale.gp_async= [KNL]
 			Measure performance of asynchronous
kernel/rcu/tree.c +28 -13

94 lines not on this page · raw message

Signature · 1 line
2.47.3
maintainer
#1 lore raw
Quoted 4 lines · Uladzislau Rezki (Sony)

* The latch is cleared only when the pending requests are fully drained(nr == 0);

kernel/rcu/tree.c +1 -0
+static void rcu_sr_normal_add_req(struct rcu_synchronize *rs)

kernel/rcu/tree.c static bool rcu_sr_normal_gp_init(void)

Quoted 10 lines
kernel/rcu/tree.c +10 -0
+{
+	long nr;
+
+	llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
+	nr = atomic_long_inc_return(&rcu_sr_normal_count);
+
+	/* Latch: only when flooded and if unlatched. */
+	if (nr >= RCU_SR_NORMAL_LATCH_THR)
+		(void)atomic_cmpxchg(&rcu_sr_normal_latched, 0, 1);
+}

I think there is a stuck-latch race here. Once llist_add() places the entry in srs_next, the GP kthread can pick it up and fire rcu_sr_normal_complete() before the latching cmpxchg runs. If the last in-flight completion drains count to zero in that window, the unlatch cmpxchg(latched, 1, 0) fails (latched is still 0 at that moment), and then the latching cmpxchg(latched, 0, 1) fires anyway — with count=0:

CPU 0 (add_req, count just hit 64) GP kthread
---------------------------------- ----------
llist_add() <-- entry now in srs_next
inc_return() --> nr = 64
[preempted]

                                            rcu_sr_normal_complete() x64:
                                              dec_return -> count: 64..1..0
                                              count==0:
                                              cmpxchg(latched, 1, 0)
                                                --> FAILS (latched still 0)

[resumes]
cmpxchg(latched, 0, 1) --> latched = 1

Final state: count=0, latched=1 --> STUCK LATCH

All subsequent synchronize_rcu() callers see latched==1 and take the fallback path (not counted). With no new SR-normal callers, rcu_sr_normal_complete() is never reached again, so the unlatch cmpxchg(latched, 1, 0) never fires. The latch is permanently stuck.

This requires preemption for a full GP duration between llist_add() and the cmpxchg, which is probably more likely on PREEMPT_RT or heavily loaded systems.

The fix: move the cmpxchg *before* llist_add(), so the entry is not visible to the GP kthread until after the latch is already set.

That should fix it, thoughts?

thanks,

Signature · 1 line
Joel Fernandes
author maintainer
#2 lore raw
Quoted 44 lines · Joel Fernandes
Quoted 4 lines · Uladzislau Rezki (Sony)

* The latch is cleared only when the pending requests are fully drained(nr == 0);

(quoted diff) +1 -0
+static void rcu_sr_normal_add_req(struct rcu_synchronize *rs)

Review

Quoted 10 lines
(quoted diff) +10 -0
+{
+	long nr;
+
+	llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
+	nr = atomic_long_inc_return(&rcu_sr_normal_count);
+
+	/* Latch: only when flooded and if unlatched. */
+	if (nr >= RCU_SR_NORMAL_LATCH_THR)
+		(void)atomic_cmpxchg(&rcu_sr_normal_latched, 0, 1);
+}

I think there is a stuck-latch race here. Once llist_add() places the entry in srs_next, the GP kthread can pick it up and fire rcu_sr_normal_complete() before the latching cmpxchg runs. If the last in-flight completion drains count to zero in that window, the unlatch cmpxchg(latched, 1, 0) fails (latched is still 0 at that moment), and then the latching cmpxchg(latched, 0, 1) fires anyway — with count=0:

CPU 0 (add_req, count just hit 64) GP kthread
---------------------------------- ----------
llist_add() <-- entry now in srs_next
inc_return() --> nr = 64
[preempted]

                                            rcu_sr_normal_complete() x64:
                                              dec_return -> count: 64..1..0
                                              count==0:
                                              cmpxchg(latched, 1, 0)
                                                --> FAILS (latched still 0)

[resumes]
cmpxchg(latched, 0, 1) --> latched = 1

Final state: count=0, latched=1 --> STUCK LATCH

All subsequent synchronize_rcu() callers see latched==1 and take the fallback path (not counted). With no new SR-normal callers, rcu_sr_normal_complete() is never reached again, so the unlatch cmpxchg(latched, 1, 0) never fires. The latch is permanently stuck.

Quoted 9 lines

This requires preemption for a full GP duration between llist_add() and the cmpxchg, which is probably more likely on PREEMPT_RT or heavily loaded systems.

The fix: move the cmpxchg *before* llist_add(), so the entry is not visible to the GP kthread until after the latch is already set.

That should fix it, thoughts?

Yes and thank you!

We can improve it even more by removing atomic_cmpxchg() in the rcu_sr_normal_add_req() function, because only one context sees the (nr == RCU_SR_NORMAL_LATCH_THR) condition:

<snip>

kernel/rcu/tree.c +17 -9
index 86dc88a70fd0..72b340940e11 100644
@@ -1640,7 +1640,7 @@ static struct workqueue_struct *sync_wq;
 
 /* Number of in-flight synchronize_rcu() calls queued on srs_next. */
 static atomic_long_t rcu_sr_normal_count;
-static atomic_t rcu_sr_normal_latched;
+static int rcu_sr_normal_latched; /* 0/1 */
 
 static void rcu_sr_normal_complete(struct llist_node *node)
 {
@@ -1662,7 +1662,7 @@ static void rcu_sr_normal_complete(struct llist_node *node)
 	 * drained and if it has been latched.
 	 */
 	if (nr == 0)
-		(void)atomic_cmpxchg(&rcu_sr_normal_latched, 1, 0);
+		(void)cmpxchg(&rcu_sr_normal_latched, 1, 0);
 }
 
 static void rcu_sr_normal_gp_cleanup_work(struct work_struct *work)
@@ -1808,14 +1808,22 @@ static bool rcu_sr_normal_gp_init(void)
 
 static void rcu_sr_normal_add_req(struct rcu_synchronize *rs)
 {
-	long nr;
+	/*
+	 * Increment before publish to avoid a complete
+	 * vs enqueue race on latch.
+	 */
+	long nr = atomic_long_inc_return(&rcu_sr_normal_count);
 
-	llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
-	nr = atomic_long_inc_return(&rcu_sr_normal_count);
+	/*
+	 * Latch on threshold crossing. (nr == RCU_SR_NORMAL_LATCH_THR)
+	 * can be true only for one context, avoiding contention on the
+	 * write path.
+	 */
+	if (nr == RCU_SR_NORMAL_LATCH_THR)
+		WRITE_ONCE(rcu_sr_normal_latched, 1);
 
-	/* Latch: only when flooded and if unlatched. */
-	if (nr >= RCU_SR_NORMAL_LATCH_THR)
-		(void)atomic_cmpxchg(&rcu_sr_normal_latched, 0, 1);
+	/* Publish for the GP kthread/worker. */
+	llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
 }
 
 /*
@@ -3302,7 +3310,7 @@ static void synchronize_rcu_normal(void)
 	trace_rcu_sr_normal(rcu_state.name, &rs.head, TPS("request"));
 
 	if (READ_ONCE(rcu_normal_wake_from_gp) < 1 ||
-			atomic_read(&rcu_sr_normal_latched)) {
+			READ_ONCE(rcu_sr_normal_latched)) {
 		wait_rcu_gp(call_rcu_hurry);
 		goto trace_complete_out;
 	}

<snip>

--
Uladzislau Rezki

maintainer
#3 lore raw
Quoted 52 lines · Uladzislau Rezki
Quoted 51 lines · Joel Fernandes
Quoted 4 lines · Uladzislau Rezki (Sony)

* The latch is cleared only when the pending requests are fully drained(nr == 0);

(quoted diff) +1 -0
+static void rcu_sr_normal_add_req(struct rcu_synchronize *rs)

Review

Quoted 10 lines
(quoted diff) +10 -0
+{
+	long nr;
+
+	llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
+	nr = atomic_long_inc_return(&rcu_sr_normal_count);
+
+	/* Latch: only when flooded and if unlatched. */
+	if (nr >= RCU_SR_NORMAL_LATCH_THR)
+		(void)atomic_cmpxchg(&rcu_sr_normal_latched, 0, 1);
+}

I think there is a stuck-latch race here. Once llist_add() places the entry in srs_next, the GP kthread can pick it up and fire rcu_sr_normal_complete() before the latching cmpxchg runs. If the last in-flight completion drains count to zero in that window, the unlatch cmpxchg(latched, 1, 0) fails (latched is still 0 at that moment), and then the latching cmpxchg(latched, 0, 1) fires anyway — with count=0:

CPU 0 (add_req, count just hit 64) GP kthread
---------------------------------- ----------
llist_add() <-- entry now in srs_next
inc_return() --> nr = 64
[preempted]

                                            rcu_sr_normal_complete() x64:
                                              dec_return -> count: 64..1..0
                                              count==0:
                                              cmpxchg(latched, 1, 0)
                                                --> FAILS (latched still 0)

[resumes]
cmpxchg(latched, 0, 1) --> latched = 1

Final state: count=0, latched=1 --> STUCK LATCH

All subsequent synchronize_rcu() callers see latched==1 and take the fallback path (not counted). With no new SR-normal callers, rcu_sr_normal_complete() is never reached again, so the unlatch cmpxchg(latched, 1, 0) never fires. The latch is permanently stuck.

This requires preemption for a full GP duration between llist_add() and the cmpxchg, which is probably more likely on PREEMPT_RT or heavily loaded systems.

The fix: move the cmpxchg *before* llist_add(), so the entry is not visible to the GP kthread until after the latch is already set.

Quoted 7 lines
Quoted 2 lines

That should fix it, thoughts?

Yes and thank you!

We can improve it even more by removing atomic_cmpxchg() in the rcu_sr_normal_add_req() function, because only one context sees the (nr == RCU_SR_NORMAL_LATCH_THR) condition:

Sure, though you still need the atomic_long_inc_return.

But yes, the approach looks good. :-) Do you think we can have v3 ready for 7.1? I would like to shoot for that if possible.

thanks,

--
Joel Fernandes

Quoted 66 lines

<snip>

kernel/rcu/tree.c +17 -10
index 86dc88a70fd0..72b340940e11 100644
@@ -1640,7 +1640,7 @@ static struct workqueue_struct *sync_wq;
 
 /* Number of in-flight synchronize_rcu() calls queued on srs_next. */
 static atomic_long_t rcu_sr_normal_count;
-static atomic_t rcu_sr_normal_latched;
+static int rcu_sr_normal_latched; /* 0/1 */
 
 static void rcu_sr_normal_complete(struct llist_node *node)
 {
@@ -1662,7 +1662,7 @@ static void rcu_sr_normal_complete(struct llist_node *node)
 	 * drained and if it has been latched.
 	 */
 	if (nr == 0)
-		(void)atomic_cmpxchg(&rcu_sr_normal_latched, 1, 0);
+		(void)cmpxchg(&rcu_sr_normal_latched, 1, 0);
 }
 
 static void rcu_sr_normal_gp_cleanup_work(struct work_struct *work)
@@ -1808,14 +1808,22 @@ static bool rcu_sr_normal_gp_init(void)
 
 static void rcu_sr_normal_add_req(struct rcu_synchronize *rs)
 {
-	long nr;
+	/*
+	 * Increment before publish to avoid a complete
+	 * vs enqueue race on latch.
+	 */
+	long nr = atomic_long_inc_return(&rcu_sr_normal_count);
 
-	llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
-	nr = atomic_long_inc_return(&rcu_sr_normal_count);
+	/*
+	 * Latch on threshold crossing. (nr == RCU_SR_NORMAL_LATCH_THR)
+	 * can be true only for one context, avoiding contention on the
+	 * write path.
+	 */
+	if (nr == RCU_SR_NORMAL_LATCH_THR)
+		WRITE_ONCE(rcu_sr_normal_latched, 1);
 
-	/* Latch: only when flooded and if unlatched. */
-	if (nr >= RCU_SR_NORMAL_LATCH_THR)
-		(void)atomic_cmpxchg(&rcu_sr_normal_latched, 0, 1);
+	/* Publish for the GP kthread/worker. */
+	llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
 }
 
 /*
@@ -3302,7 +3310,7 @@ static void synchronize_rcu_normal(void)
 	trace_rcu_sr_normal(rcu_state.name, &rs.head, TPS("request"));
 
 	if (READ_ONCE(rcu_normal_wake_from_gp) < 1 ||
-			atomic_read(&rcu_sr_normal_latched)) {
+			READ_ONCE(rcu_sr_normal_latched)) {
 		wait_rcu_gp(call_rcu_hurry);
 		goto trace_complete_out;
 	}
<snip>
 
--

Uladzislau Rezki

Signature · 1 line
Joel Fernandes
author maintainer
#4 lore raw
Quoted 55 lines · Joel Fernandes
Quoted 52 lines · Uladzislau Rezki
Quoted 51 lines · Joel Fernandes
Quoted 4 lines · Uladzislau Rezki (Sony)

* The latch is cleared only when the pending requests are fully drained(nr == 0);

(quoted diff) +1 -0
+static void rcu_sr_normal_add_req(struct rcu_synchronize *rs)

Review

Quoted 10 lines
(quoted diff) +10 -0
+{
+	long nr;
+
+	llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
+	nr = atomic_long_inc_return(&rcu_sr_normal_count);
+
+	/* Latch: only when flooded and if unlatched. */
+	if (nr >= RCU_SR_NORMAL_LATCH_THR)
+		(void)atomic_cmpxchg(&rcu_sr_normal_latched, 0, 1);
+}

I think there is a stuck-latch race here. Once llist_add() places the entry in srs_next, the GP kthread can pick it up and fire rcu_sr_normal_complete() before the latching cmpxchg runs. If the last in-flight completion drains count to zero in that window, the unlatch cmpxchg(latched, 1, 0) fails (latched is still 0 at that moment), and then the latching cmpxchg(latched, 0, 1) fires anyway — with count=0:

CPU 0 (add_req, count just hit 64) GP kthread
---------------------------------- ----------
llist_add() <-- entry now in srs_next
inc_return() --> nr = 64
[preempted]

                                            rcu_sr_normal_complete() x64:
                                              dec_return -> count: 64..1..0
                                              count==0:
                                              cmpxchg(latched, 1, 0)
                                                --> FAILS (latched still 0)

[resumes]
cmpxchg(latched, 0, 1) --> latched = 1

Final state: count=0, latched=1 --> STUCK LATCH

All subsequent synchronize_rcu() callers see latched==1 and take the fallback path (not counted). With no new SR-normal callers, rcu_sr_normal_complete() is never reached again, so the unlatch cmpxchg(latched, 1, 0) never fires. The latch is permanently stuck.

This requires preemption for a full GP duration between llist_add() and the cmpxchg, which is probably more likely on PREEMPT_RT or heavily loaded systems.

The fix: move the cmpxchg *before* llist_add(), so the entry is not visible to the GP kthread until after the latch is already set.

Quoted 9 lines
Quoted 7 lines
Quoted 2 lines

That should fix it, thoughts?

Yes and thank you!

We can improve it even more by removing atomic_cmpxchg() in the rcu_sr_normal_add_req() function, because only one context sees the (nr == RCU_SR_NORMAL_LATCH_THR) condition:

Sure, though you still need the atomic_long_inc_return.

Yes :)

Quoted 4 lines

But yes, the approach looks good. :-) Do you think we can have v3 ready for 7.1? I would like to shoot for that if possible.

I will post v3 today.

--
Uladzislau Rezki

#5 lore raw
In reply to Uladzislau Rezki
Quoted 93 lines · Uladzislau Rezki
Quoted 44 lines · Joel Fernandes
Quoted 4 lines · Uladzislau Rezki (Sony)

* The latch is cleared only when the pending requests are fully drained(nr == 0);

(quoted diff) +1 -0
+static void rcu_sr_normal_add_req(struct rcu_synchronize *rs)

Review

Quoted 10 lines
(quoted diff) +10 -0
+{
+	long nr;
+
+	llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
+	nr = atomic_long_inc_return(&rcu_sr_normal_count);
+
+	/* Latch: only when flooded and if unlatched. */
+	if (nr >= RCU_SR_NORMAL_LATCH_THR)
+		(void)atomic_cmpxchg(&rcu_sr_normal_latched, 0, 1);
+}

I think there is a stuck-latch race here. Once llist_add() places the entry in srs_next, the GP kthread can pick it up and fire rcu_sr_normal_complete() before the latching cmpxchg runs. If the last in-flight completion drains count to zero in that window, the unlatch cmpxchg(latched, 1, 0) fails (latched is still 0 at that moment), and then the latching cmpxchg(latched, 0, 1) fires anyway — with count=0:

CPU 0 (add_req, count just hit 64) GP kthread
---------------------------------- ----------
llist_add() <-- entry now in srs_next
inc_return() --> nr = 64
[preempted]

                                            rcu_sr_normal_complete() x64:
                                              dec_return -> count: 64..1..0
                                              count==0:
                                              cmpxchg(latched, 1, 0)
                                                --> FAILS (latched still 0)

[resumes]
cmpxchg(latched, 0, 1) --> latched = 1

Final state: count=0, latched=1 --> STUCK LATCH

All subsequent synchronize_rcu() callers see latched==1 and take the fallback path (not counted). With no new SR-normal callers, rcu_sr_normal_complete() is never reached again, so the unlatch cmpxchg(latched, 1, 0) never fires. The latch is permanently stuck.

Quoted 9 lines

This requires preemption for a full GP duration between llist_add() and the cmpxchg, which is probably more likely on PREEMPT_RT or heavily loaded systems.

The fix: move the cmpxchg *before* llist_add(), so the entry is not visible to the GP kthread until after the latch is already set.

That should fix it, thoughts?

Yes and thank you!

We can improve it even more by removing atomic_cmpxchg() in the rcu_sr_normal_add_req() function, because only one context sees the (nr == RCU_SR_NORMAL_LATCH_THR) condition:

<snip>

kernel/rcu/tree.c +7 -3
index 86dc88a70fd0..72b340940e11 100644
@@ -1640,7 +1640,7 @@ static struct workqueue_struct *sync_wq;
 
 /* Number of in-flight synchronize_rcu() calls queued on srs_next. */
 static atomic_long_t rcu_sr_normal_count;
-static atomic_t rcu_sr_normal_latched;
+static int rcu_sr_normal_latched; /* 0/1 */
 
 static void rcu_sr_normal_complete(struct llist_node *node)
 {
@@ -1662,7 +1662,7 @@ static void rcu_sr_normal_complete(struct llist_node *node)
 	 * drained and if it has been latched.
 	 */
 	if (nr == 0)
-		(void)atomic_cmpxchg(&rcu_sr_normal_latched, 1, 0);
+		(void)cmpxchg(&rcu_sr_normal_latched, 1, 0);
 }
 
 static void rcu_sr_normal_gp_cleanup_work(struct work_struct *work)
@@ -1808,14 +1808,22 @@ static bool rcu_sr_normal_gp_init(void)
 
 static void rcu_sr_normal_add_req(struct rcu_synchronize *rs)
 {
-	long nr;
+	/*
+	 * Increment before publish to avoid a complete
+	 * vs enqueue race on latch.
+	 */
+	long nr = atomic_long_inc_return(&rcu_sr_normal_count);

kernel/rcu/tree.c static bool rcu_sr_normal_gp_init(void) · line 1823

Quoted 10 lines
kernel/rcu/tree.c +7 -2
 
-	llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
-	nr = atomic_long_inc_return(&rcu_sr_normal_count);
+	/*
+	 * Latch on threshold crossing. (nr == RCU_SR_NORMAL_LATCH_THR)
+	 * can be true only for one context, avoiding contention on the
+	 * write path.
+	 */
+	if (nr == RCU_SR_NORMAL_LATCH_THR)
+		WRITE_ONCE(rcu_sr_normal_latched, 1);

Isn't it still racy?

rcu_sr_normal_add_req rcu_sr_normal_complete
--------------------- ----------------------

                                                        nr = atomic_long_dec_return(&rcu_sr_normal_count);
                                                        // nr == 0
                                                        ======= PREEMPTION =======
// 64 tasks doing synchronize_rcu()
rcu_sr_normal_add_req()
   WRITE_ONCE(rcu_sr_normal_latched, 1);
                                                        cmpxchg(&rcu_sr_normal_latched, 1, 0);

Also more generally there is nothing that orders the WRITE_ONCE() with the cmpxchg.

Is it possible to remove rcu_sr_normal_latched and simply deal with comparisons between rcu_sr_normal_count and RCU_SR_NORMAL_LATCH_THR?

Thanks.

Signature · 2 lines
Frederic Weisbecker
SUSE Labs
author maintainer
#6 lore raw

Hello, Frederic!

Quoted 118 lines · Uladzislau Rezki
Quoted 93 lines · Joel Fernandes
Quoted 44 lines · Uladzislau Rezki (Sony)
Quoted 5 lines

* The latch is cleared only when the pending requests are fully drained(nr == 0);

(quoted diff) +1 -0
+static void rcu_sr_normal_add_req(struct rcu_synchronize *rs)

Review

Quoted 10 lines
(quoted diff) +10 -0
+{
+	long nr;
+
+	llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
+	nr = atomic_long_inc_return(&rcu_sr_normal_count);
+
+	/* Latch: only when flooded and if unlatched. */
+	if (nr >= RCU_SR_NORMAL_LATCH_THR)
+		(void)atomic_cmpxchg(&rcu_sr_normal_latched, 0, 1);
+}

I think there is a stuck-latch race here. Once llist_add() places the entry in srs_next, the GP kthread can pick it up and fire rcu_sr_normal_complete() before the latching cmpxchg runs. If the last in-flight completion drains count to zero in that window, the unlatch cmpxchg(latched, 1, 0) fails (latched is still 0 at that moment), and then the latching cmpxchg(latched, 0, 1) fires anyway — with count=0:

CPU 0 (add_req, count just hit 64) GP kthread
---------------------------------- ----------
llist_add() <-- entry now in srs_next
inc_return() --> nr = 64
[preempted]

                                            rcu_sr_normal_complete() x64:
                                              dec_return -> count: 64..1..0
                                              count==0:
                                              cmpxchg(latched, 1, 0)
                                                --> FAILS (latched still 0)

[resumes]
cmpxchg(latched, 0, 1) --> latched = 1

Final state: count=0, latched=1 --> STUCK LATCH

All subsequent synchronize_rcu() callers see latched==1 and take the fallback path (not counted). With no new SR-normal callers, rcu_sr_normal_complete() is never reached again, so the unlatch cmpxchg(latched, 1, 0) never fires. The latch is permanently stuck.

Quoted 9 lines

This requires preemption for a full GP duration between llist_add() and the cmpxchg, which is probably more likely on PREEMPT_RT or heavily loaded systems.

The fix: move the cmpxchg *before* llist_add(), so the entry is not visible to the GP kthread until after the latch is already set.

That should fix it, thoughts?

Yes and thank you!

We can improve it even more by removing atomic_cmpxchg() in the rcu_sr_normal_add_req() function, because only one context sees the (nr == RCU_SR_NORMAL_LATCH_THR) condition:

<snip>

kernel/rcu/tree.c +7 -3
index 86dc88a70fd0..72b340940e11 100644
@@ -1640,7 +1640,7 @@ static struct workqueue_struct *sync_wq;
 
 /* Number of in-flight synchronize_rcu() calls queued on srs_next. */
 static atomic_long_t rcu_sr_normal_count;
-static atomic_t rcu_sr_normal_latched;
+static int rcu_sr_normal_latched; /* 0/1 */
 
 static void rcu_sr_normal_complete(struct llist_node *node)
 {
@@ -1662,7 +1662,7 @@ static void rcu_sr_normal_complete(struct llist_node *node)
 	 * drained and if it has been latched.
 	 */
 	if (nr == 0)
-		(void)atomic_cmpxchg(&rcu_sr_normal_latched, 1, 0);
+		(void)cmpxchg(&rcu_sr_normal_latched, 1, 0);
 }
 
 static void rcu_sr_normal_gp_cleanup_work(struct work_struct *work)
@@ -1808,14 +1808,22 @@ static bool rcu_sr_normal_gp_init(void)
 
 static void rcu_sr_normal_add_req(struct rcu_synchronize *rs)
 {
-	long nr;
+	/*
+	 * Increment before publish to avoid a complete
+	 * vs enqueue race on latch.
+	 */
+	long nr = atomic_long_inc_return(&rcu_sr_normal_count);

kernel/rcu/tree.c static bool rcu_sr_normal_gp_init(void) · line 1823

Quoted 10 lines
kernel/rcu/tree.c +7 -2
 
-	llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
-	nr = atomic_long_inc_return(&rcu_sr_normal_count);
+	/*
+	 * Latch on threshold crossing. (nr == RCU_SR_NORMAL_LATCH_THR)
+	 * can be true only for one context, avoiding contention on the
+	 * write path.
+	 */
+	if (nr == RCU_SR_NORMAL_LATCH_THR)
+		WRITE_ONCE(rcu_sr_normal_latched, 1);

Isn't it still racy?

rcu_sr_normal_add_req rcu_sr_normal_complete
--------------------- ----------------------

                                                        nr = atomic_long_dec_return(&rcu_sr_normal_count);
                                                        // nr == 0
                                                        ======= PREEMPTION =======
// 64 tasks doing synchronize_rcu()
rcu_sr_normal_add_req()
   WRITE_ONCE(rcu_sr_normal_latched, 1);
                                                        cmpxchg(&rcu_sr_normal_latched, 1, 0);
Quoted 3 lines

Also more generally there is nothing that orders the WRITE_ONCE() with the cmpxchg.

Yep that i know. This is rather "relaxed" mechanism rather than a strictly ordered. The race you described can happen but i do not find it as a problem because as noted it is relaxed policy flag.

But WRITE_ONCE() i can replace by:

if (nr == RCU_SR_NORMAL_LATCH_THR)

    cmpxchg(&rcu_sr_normal_latched, 0, 1);
Quoted 4 lines

Is it possible to remove rcu_sr_normal_latched and simply deal with comparisons between rcu_sr_normal_count and RCU_SR_NORMAL_LATCH_THR?

It is. But the idea with latch is a bit different then just checking threshold. The main goal is to detect flood and lath the path until __all__ users are flushed. I.e. it implements hysteresis to prevent repeated switches around the threshold.

With your proposal behaviour becomes different.

Thoughts?

--
Uladzislau Rezki

#7 lore raw
Quoted 120 lines · Uladzislau Rezki

Hello, Frederic!

Quoted 118 lines · Uladzislau Rezki
Quoted 93 lines · Joel Fernandes
Quoted 44 lines · Uladzislau Rezki (Sony)
Quoted 5 lines

* The latch is cleared only when the pending requests are fully drained(nr == 0);

(quoted diff) +1 -0
+static void rcu_sr_normal_add_req(struct rcu_synchronize *rs)

Review

Quoted 10 lines
(quoted diff) +10 -0
+{
+	long nr;
+
+	llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
+	nr = atomic_long_inc_return(&rcu_sr_normal_count);
+
+	/* Latch: only when flooded and if unlatched. */
+	if (nr >= RCU_SR_NORMAL_LATCH_THR)
+		(void)atomic_cmpxchg(&rcu_sr_normal_latched, 0, 1);
+}

I think there is a stuck-latch race here. Once llist_add() places the entry in srs_next, the GP kthread can pick it up and fire rcu_sr_normal_complete() before the latching cmpxchg runs. If the last in-flight completion drains count to zero in that window, the unlatch cmpxchg(latched, 1, 0) fails (latched is still 0 at that moment), and then the latching cmpxchg(latched, 0, 1) fires anyway — with count=0:

CPU 0 (add_req, count just hit 64) GP kthread
---------------------------------- ----------
llist_add() <-- entry now in srs_next
inc_return() --> nr = 64
[preempted]

                                            rcu_sr_normal_complete() x64:
                                              dec_return -> count: 64..1..0
                                              count==0:
                                              cmpxchg(latched, 1, 0)
                                                --> FAILS (latched still 0)

[resumes]
cmpxchg(latched, 0, 1) --> latched = 1

Final state: count=0, latched=1 --> STUCK LATCH

All subsequent synchronize_rcu() callers see latched==1 and take the fallback path (not counted). With no new SR-normal callers, rcu_sr_normal_complete() is never reached again, so the unlatch cmpxchg(latched, 1, 0) never fires. The latch is permanently stuck.

Quoted 9 lines

This requires preemption for a full GP duration between llist_add() and the cmpxchg, which is probably more likely on PREEMPT_RT or heavily loaded systems.

The fix: move the cmpxchg *before* llist_add(), so the entry is not visible to the GP kthread until after the latch is already set.

That should fix it, thoughts?

Yes and thank you!

We can improve it even more by removing atomic_cmpxchg() in the rcu_sr_normal_add_req() function, because only one context sees the (nr == RCU_SR_NORMAL_LATCH_THR) condition:

<snip>

kernel/rcu/tree.c +7 -3
index 86dc88a70fd0..72b340940e11 100644
@@ -1640,7 +1640,7 @@ static struct workqueue_struct *sync_wq;
 
 /* Number of in-flight synchronize_rcu() calls queued on srs_next. */
 static atomic_long_t rcu_sr_normal_count;
-static atomic_t rcu_sr_normal_latched;
+static int rcu_sr_normal_latched; /* 0/1 */
 
 static void rcu_sr_normal_complete(struct llist_node *node)
 {
@@ -1662,7 +1662,7 @@ static void rcu_sr_normal_complete(struct llist_node *node)
 	 * drained and if it has been latched.
 	 */
 	if (nr == 0)
-		(void)atomic_cmpxchg(&rcu_sr_normal_latched, 1, 0);
+		(void)cmpxchg(&rcu_sr_normal_latched, 1, 0);
 }
 
 static void rcu_sr_normal_gp_cleanup_work(struct work_struct *work)
@@ -1808,14 +1808,22 @@ static bool rcu_sr_normal_gp_init(void)
 
 static void rcu_sr_normal_add_req(struct rcu_synchronize *rs)
 {
-	long nr;
+	/*
+	 * Increment before publish to avoid a complete
+	 * vs enqueue race on latch.
+	 */
+	long nr = atomic_long_inc_return(&rcu_sr_normal_count);

kernel/rcu/tree.c static bool rcu_sr_normal_gp_init(void) · line 1823

Quoted 10 lines
kernel/rcu/tree.c +7 -2
 
-	llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
-	nr = atomic_long_inc_return(&rcu_sr_normal_count);
+	/*
+	 * Latch on threshold crossing. (nr == RCU_SR_NORMAL_LATCH_THR)
+	 * can be true only for one context, avoiding contention on the
+	 * write path.
+	 */
+	if (nr == RCU_SR_NORMAL_LATCH_THR)
+		WRITE_ONCE(rcu_sr_normal_latched, 1);

Isn't it still racy?

rcu_sr_normal_add_req rcu_sr_normal_complete
--------------------- ----------------------

                                                        nr = atomic_long_dec_return(&rcu_sr_normal_count);
                                                        // nr == 0
                                                        ======= PREEMPTION =======
// 64 tasks doing synchronize_rcu()
rcu_sr_normal_add_req()
   WRITE_ONCE(rcu_sr_normal_latched, 1);
                                                        cmpxchg(&rcu_sr_normal_latched, 1, 0);
Quoted 6 lines
Quoted 3 lines

Also more generally there is nothing that orders the WRITE_ONCE() with the cmpxchg.

Yep that i know. This is rather "relaxed" mechanism rather than a strictly ordered. The race you described can happen but i do not find it as a problem because as noted it is relaxed policy flag.

Ok, that will need a comment explaining how and why we tolerate missed latches then.

Quoted 5 lines

But WRITE_ONCE() i can replace by:

if (nr == RCU_SR_NORMAL_LATCH_THR)

    cmpxchg(&rcu_sr_normal_latched, 0, 1);

Possibly yes, though I'm not sure that would help.

Quoted 7 lines
Quoted 3 lines

Is it possible to remove rcu_sr_normal_latched and simply deal with comparisons between rcu_sr_normal_count and RCU_SR_NORMAL_LATCH_THR?

It is. But the idea with latch is a bit different then just checking threshold. The main goal is to detect flood and lath the path until __all__ users are flushed. I.e. it implements hysteresis to prevent repeated switches around the threshold.

Good point!

Quoted 3 lines

With your proposal behaviour becomes different.

Thoughts?

Good thoughts! :-)

Signature · 2 lines
Frederic Weisbecker
SUSE Labs
author maintainer
#8 lore raw
Quoted 124 lines · Frederic Weisbecker
Quoted 123 lines · Uladzislau Rezki

Hello, Frederic!

Quoted 121 lines · Uladzislau Rezki
Quoted 93 lines · Joel Fernandes
Quoted 44 lines · Uladzislau Rezki (Sony)
Quoted 5 lines

* The latch is cleared only when the pending requests are fully drained(nr == 0);

(quoted diff) +1 -0
+static void rcu_sr_normal_add_req(struct rcu_synchronize *rs)

Review

Quoted 10 lines
(quoted diff) +10 -0
+{
+	long nr;
+
+	llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
+	nr = atomic_long_inc_return(&rcu_sr_normal_count);
+
+	/* Latch: only when flooded and if unlatched. */
+	if (nr >= RCU_SR_NORMAL_LATCH_THR)
+		(void)atomic_cmpxchg(&rcu_sr_normal_latched, 0, 1);
+}

I think there is a stuck-latch race here. Once llist_add() places the entry in srs_next, the GP kthread can pick it up and fire rcu_sr_normal_complete() before the latching cmpxchg runs. If the last in-flight completion drains count to zero in that window, the unlatch cmpxchg(latched, 1, 0) fails (latched is still 0 at that moment), and then the latching cmpxchg(latched, 0, 1) fires anyway — with count=0:

CPU 0 (add_req, count just hit 64) GP kthread
---------------------------------- ----------
llist_add() <-- entry now in srs_next
inc_return() --> nr = 64
[preempted]

                                            rcu_sr_normal_complete() x64:
                                              dec_return -> count: 64..1..0
                                              count==0:
                                              cmpxchg(latched, 1, 0)
                                                --> FAILS (latched still 0)

[resumes]
cmpxchg(latched, 0, 1) --> latched = 1

Final state: count=0, latched=1 --> STUCK LATCH

All subsequent synchronize_rcu() callers see latched==1 and take the fallback path (not counted). With no new SR-normal callers, rcu_sr_normal_complete() is never reached again, so the unlatch cmpxchg(latched, 1, 0) never fires. The latch is permanently stuck.

Quoted 9 lines

This requires preemption for a full GP duration between llist_add() and the cmpxchg, which is probably more likely on PREEMPT_RT or heavily loaded systems.

The fix: move the cmpxchg *before* llist_add(), so the entry is not visible to the GP kthread until after the latch is already set.

That should fix it, thoughts?

Yes and thank you!

We can improve it even more by removing atomic_cmpxchg() in the rcu_sr_normal_add_req() function, because only one context sees the (nr == RCU_SR_NORMAL_LATCH_THR) condition:

<snip>

kernel/rcu/tree.c +7 -3
index 86dc88a70fd0..72b340940e11 100644
@@ -1640,7 +1640,7 @@ static struct workqueue_struct *sync_wq;
 
 /* Number of in-flight synchronize_rcu() calls queued on srs_next. */
 static atomic_long_t rcu_sr_normal_count;
-static atomic_t rcu_sr_normal_latched;
+static int rcu_sr_normal_latched; /* 0/1 */
 
 static void rcu_sr_normal_complete(struct llist_node *node)
 {
@@ -1662,7 +1662,7 @@ static void rcu_sr_normal_complete(struct llist_node *node)
 	 * drained and if it has been latched.
 	 */
 	if (nr == 0)
-		(void)atomic_cmpxchg(&rcu_sr_normal_latched, 1, 0);
+		(void)cmpxchg(&rcu_sr_normal_latched, 1, 0);
 }
 
 static void rcu_sr_normal_gp_cleanup_work(struct work_struct *work)
@@ -1808,14 +1808,22 @@ static bool rcu_sr_normal_gp_init(void)
 
 static void rcu_sr_normal_add_req(struct rcu_synchronize *rs)
 {
-	long nr;
+	/*
+	 * Increment before publish to avoid a complete
+	 * vs enqueue race on latch.
+	 */
+	long nr = atomic_long_inc_return(&rcu_sr_normal_count);

kernel/rcu/tree.c static bool rcu_sr_normal_gp_init(void) · line 1823

Quoted 10 lines
kernel/rcu/tree.c +7 -2
 
-	llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
-	nr = atomic_long_inc_return(&rcu_sr_normal_count);
+	/*
+	 * Latch on threshold crossing. (nr == RCU_SR_NORMAL_LATCH_THR)
+	 * can be true only for one context, avoiding contention on the
+	 * write path.
+	 */
+	if (nr == RCU_SR_NORMAL_LATCH_THR)
+		WRITE_ONCE(rcu_sr_normal_latched, 1);

Isn't it still racy?

rcu_sr_normal_add_req rcu_sr_normal_complete
--------------------- ----------------------

                                                        nr = atomic_long_dec_return(&rcu_sr_normal_count);
                                                        // nr == 0
                                                        ======= PREEMPTION =======
// 64 tasks doing synchronize_rcu()
rcu_sr_normal_add_req()
   WRITE_ONCE(rcu_sr_normal_latched, 1);
                                                        cmpxchg(&rcu_sr_normal_latched, 1, 0);

Also more generally there is nothing that orders the WRITE_ONCE() with the cmpxchg.

Quoted 7 lines
Quoted 3 lines

Yep that i know. This is rather "relaxed" mechanism rather than a strictly ordered. The race you described can happen but i do not find it as a problem because as noted it is relaxed policy flag.

Ok, that will need a comment explaining how and why we tolerate missed latches then.

Makes sense!

Quoted 8 lines
Quoted 5 lines

But WRITE_ONCE() i can replace by:

if (nr == RCU_SR_NORMAL_LATCH_THR)

    cmpxchg(&rcu_sr_normal_latched, 0, 1);

Possibly yes, though I'm not sure that would help.

Just to align with second place where we drop rcu_sr_normal_latched.

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

Is it possible to remove rcu_sr_normal_latched and simply deal with comparisons between rcu_sr_normal_count and RCU_SR_NORMAL_LATCH_THR?

It is. But the idea with latch is a bit different then just checking threshold. The main goal is to detect flood and lath the path until __all__ users are flushed. I.e. it implements hysteresis to prevent repeated switches around the threshold.

Quoted 8 lines

Good point!

Quoted 3 lines

With your proposal behaviour becomes different.

Thoughts?

Good thoughts! :-)

Thank you Frederic :)

--
Uladzislau Rezki